本文整理汇总了Java中org.codehaus.jackson.JsonGenerator.close方法的典型用法代码示例。如果您正苦于以下问题:Java JsonGenerator.close方法的具体用法?Java JsonGenerator.close怎么用?Java JsonGenerator.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.codehaus.jackson.JsonGenerator
的用法示例。
在下文中一共展示了JsonGenerator.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: write
import org.codehaus.jackson.JsonGenerator; //导入方法依赖的package包/类
private void write(DataOutput out) throws IOException {
// This is just a JSON experiment
System.out.println("Dumping the StatePool's in JSON format.");
ObjectMapper outMapper = new ObjectMapper();
outMapper.configure(
SerializationConfig.Feature.CAN_OVERRIDE_ACCESS_MODIFIERS, true);
// define a module
SimpleModule module = new SimpleModule("State Serializer",
new Version(0, 1, 1, "FINAL"));
// add the state serializer
//module.addSerializer(State.class, new StateSerializer());
// register the module with the object-mapper
outMapper.registerModule(module);
JsonFactory outFactory = outMapper.getJsonFactory();
JsonGenerator jGen =
outFactory.createJsonGenerator((DataOutputStream)out, JsonEncoding.UTF8);
jGen.useDefaultPrettyPrinter();
jGen.writeObject(this);
jGen.close();
}
示例2: main
import org.codehaus.jackson.JsonGenerator; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
final Configuration conf = new Configuration();
final FileSystem lfs = FileSystem.getLocal(conf);
for (String arg : args) {
Path filePath = new Path(arg).makeQualified(lfs);
String fileName = filePath.getName();
if (fileName.startsWith("input")) {
LoggedDiscreteCDF newResult = histogramFileToCDF(filePath, lfs);
String testName = fileName.substring("input".length());
Path goldFilePath = new Path(filePath.getParent(), "gold"+testName);
ObjectMapper mapper = new ObjectMapper();
JsonFactory factory = mapper.getJsonFactory();
FSDataOutputStream ostream = lfs.create(goldFilePath, true);
JsonGenerator gen = factory.createJsonGenerator(ostream,
JsonEncoding.UTF8);
gen.useDefaultPrettyPrinter();
gen.writeObject(newResult);
gen.close();
} else {
System.err.println("Input file not started with \"input\". File "+fileName+" skipped.");
}
}
}
示例3: next
import org.codehaus.jackson.JsonGenerator; //导入方法依赖的package包/类
@Override
public String next()
{
CSVRecord record = inner.next();
StringWriter json = new StringWriter();
try {
JsonGenerator gen = jsonFactory.createJsonGenerator(json);
gen.writeStartObject();
for (CSVHeaderMap.Entry entry : headerMap.entries()) {
String name = entry.getName();
String value = record.get(entry.getIndex());
gen.writeFieldName(name);
entry.getWriter().write(gen, value);
}
gen.writeEndObject();
gen.close();
}
catch (IOException e) {
throw new RuntimeException(e);
}
return json.toString();
}
示例4: writeToResponse
import org.codehaus.jackson.JsonGenerator; //导入方法依赖的package包/类
/**
* Writes the response values back to the http response. This allows the calling code to
* parse the response values for display to the user.
*
* @param responseMap the response params to write to the http response
* @param response the http response
* @throws IOException
*/
private static void writeToResponse(Map<String, String> responseMap, HttpServletResponse response) throws IOException {
// Note: setting the content-type to text/html because otherwise IE prompt the user to download
// the result rather than handing it off to the GWT form response handler.
// See JIRA issue https://issues.jboss.org/browse/SRAMPUI-103
response.setContentType("text/html; charset=UTF8"); //$NON-NLS-1$
JsonFactory f = new JsonFactory();
JsonGenerator g = f.createJsonGenerator(response.getOutputStream(), JsonEncoding.UTF8);
g.useDefaultPrettyPrinter();
g.writeStartObject();
for (java.util.Map.Entry<String, String> entry : responseMap.entrySet()) {
String key = entry.getKey();
String val = entry.getValue();
g.writeStringField(key, val);
}
g.writeEndObject();
g.flush();
g.close();
}
示例5: toJson
import org.codehaus.jackson.JsonGenerator; //导入方法依赖的package包/类
/**
* Build a JSON entry from the parameters. This is public for testing.
*
* @param writer destination
* @param loggerName logger name
* @param timeStamp time_t value
* @param level level string
* @param threadName name of the thread
* @param message rendered message
* @param ti nullable thrown information
* @return the writer
* @throws IOException on any problem
*/
public Writer toJson(final Writer writer,
final String loggerName,
final long timeStamp,
final String level,
final String threadName,
final String message,
final ThrowableInformation ti) throws IOException {
JsonGenerator json = factory.createJsonGenerator(writer);
json.writeStartObject();
json.writeStringField(NAME, loggerName);
json.writeNumberField(TIME, timeStamp);
Date date = new Date(timeStamp);
json.writeStringField(DATE, dateFormat.format(date));
json.writeStringField(LEVEL, level);
json.writeStringField(THREAD, threadName);
json.writeStringField(MESSAGE, message);
if (ti != null) {
//there is some throwable info, but if the log event has been sent over the wire,
//there may not be a throwable inside it, just a summary.
Throwable thrown = ti.getThrowable();
String eclass = (thrown != null) ?
thrown.getClass().getName()
: "";
json.writeStringField(EXCEPTION_CLASS, eclass);
String[] stackTrace = ti.getThrowableStrRep();
json.writeArrayFieldStart(STACK);
for (String row : stackTrace) {
json.writeString(row);
}
json.writeEndArray();
}
json.writeEndObject();
json.flush();
json.close();
return writer;
}
示例6: beanToJson
import org.codehaus.jackson.JsonGenerator; //导入方法依赖的package包/类
public static String beanToJson(Object bean) {
StringWriter sw = new StringWriter();
try {
JsonGenerator jsongenerator = objmapper.getJsonFactory().createJsonGenerator(sw);
objmapper.writeValue(jsongenerator, bean);
jsongenerator.close();
} catch (IOException e) {
LOG.error("", e);
return "";
}
return sw.toString();
}
示例7: anonymizeTrace
import org.codehaus.jackson.JsonGenerator; //导入方法依赖的package包/类
private void anonymizeTrace() throws Exception {
if (anonymizeTrace) {
System.out.println("Anonymizing trace file: " + inputTracePath);
JobTraceReader reader = null;
JsonGenerator outGen = null;
Configuration conf = getConf();
try {
// create a generator
outGen = createJsonGenerator(conf, outputTracePath);
// define the input trace reader
reader = new JobTraceReader(inputTracePath, conf);
// read the plain unanonymized logged job
LoggedJob job = reader.getNext();
while (job != null) {
// write it via an anonymizing channel
outGen.writeObject(job);
// read the next job
job = reader.getNext();
}
System.out.println("Anonymized trace file: " + outputTracePath);
} finally {
if (outGen != null) {
outGen.close();
}
if (reader != null) {
reader.close();
}
}
}
}
示例8: anonymizeTopology
import org.codehaus.jackson.JsonGenerator; //导入方法依赖的package包/类
private void anonymizeTopology() throws Exception {
if (anonymizeTopology) {
System.out.println("Anonymizing topology file: " + inputTopologyPath);
ClusterTopologyReader reader = null;
JsonGenerator outGen = null;
Configuration conf = getConf();
try {
// create a generator
outGen = createJsonGenerator(conf, outputTopologyPath);
// define the input cluster topology reader
reader = new ClusterTopologyReader(inputTopologyPath, conf);
// read the plain unanonymized logged job
LoggedNetworkTopology job = reader.get();
// write it via an anonymizing channel
outGen.writeObject(job);
System.out.println("Anonymized topology file: " + outputTopologyPath);
} finally {
if (outGen != null) {
outGen.close();
}
}
}
}
示例9: open
import org.codehaus.jackson.JsonGenerator; //导入方法依赖的package包/类
public Writer open(final PrintWriter writer) throws IOException {
final JsonGenerator jg = jsonFactory.createJsonGenerator(writer);
jg.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
jg.useDefaultPrettyPrinter();
jg.writeStartObject();
return new Writer() {
@Override
public void flush() throws IOException {
jg.flush();
}
@Override
public void close() throws IOException {
jg.close();
}
@Override
public void write(String key, String value) throws JsonGenerationException, IOException {
jg.writeStringField(key, value);
}
@Override
public int write(MBeanServer mBeanServer, ObjectName qry, String attribute,
boolean description)
throws IOException {
return JSONBean.write(jg, mBeanServer, qry, attribute, description);
}
};
}
示例10: encodeDatabaseOperation
import org.codehaus.jackson.JsonGenerator; //导入方法依赖的package包/类
/**
* Encode the DownstreamOperation as a JSON record.
*
* @param op the operatoin we are encoding
* @return an instance of EventData
* @throws IOException if an encoding error occurs
*/
public EventData encodeDatabaseOperation(DownstreamOperation op) throws IOException {
JsonGenerator jg;
eventHeader.setHeaderInfo(op);
/*
* need a new one of these with each call because we pass the byte array
* from baos along with the event as it is passed to the publisher.
*/
baos = new ByteArrayOutputStream();
jg = factory.createJsonGenerator(baos);
jg.writeStartObject();
setTxInfo(jg, op);
// loop through operatons with calls to appropriate jg.write() methods
for (DownstreamColumnData col : op.getColumns()) {
if (textOnly == true) {
jg.writeStringField(col.getBDName(), col.asString());
} else {
// Encode the data appropriately: handle numbers as numbers, etc.
int jdbcType;
jdbcType = op.getTableMeta().getColumn(col.getOrigName()).getJdbcType();
encodeColumn(col, jdbcType, jg);
}
}
if (includeBefores) {
setBeforeInfo(jg, op);
}
jg.writeEndObject();
jg.flush();
jg.close();
return new EventData(encoderType, eventHeader.getEventHeader(), baos.toByteArray());
}
示例11: convertHarRequestToString
import org.codehaus.jackson.JsonGenerator; //导入方法依赖的package包/类
public static String convertHarRequestToString(HarRequest request)
throws ClientApiException {
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
JsonGenerator g = new JsonFactory().createJsonGenerator(os);
g.writeStartObject();
request.writeHar(g);
g.close();
return os.toString("UTF-8");
} catch (IOException e) {
throw new ClientApiException(e);
}
}
示例12: writeValueWithConf
import org.codehaus.jackson.JsonGenerator; //导入方法依赖的package包/类
private void writeValueWithConf(JsonGenerator jgen, Object value,
JsonSerialize.Inclusion inc) throws IOException,
JsonGenerationException, JsonMappingException {
SerializationConfig cfg = copySerializationConfig();
cfg = cfg.withSerializationInclusion(inc);
// [JACKSON-96]: allow enabling pretty printing for ObjectMapper
// directly
if (cfg.isEnabled(SerializationConfig.Feature.INDENT_OUTPUT)) {
jgen.useDefaultPrettyPrinter();
}
// [JACKSON-282]: consider Closeable
if (cfg.isEnabled(SerializationConfig.Feature.CLOSE_CLOSEABLE)
&& (value instanceof Closeable)) {
configAndWriteCloseable(jgen, value, cfg);
return;
}
boolean closed = false;
try {
_serializerProvider.serializeValue(cfg, jgen, value,
_serializerFactory);
closed = true;
jgen.close();
} finally {
/*
* won't try to close twice; also, must catch exception (so it
* will not mask exception that is pending)
*/
if (!closed) {
try {
jgen.close();
} catch (IOException ioe) {
}
}
}
}
示例13: persistState
import org.codehaus.jackson.JsonGenerator; //导入方法依赖的package包/类
/**
* This function saves the state of the ClusterManager to disk.
* @return A boolean. True if saving the state succeeded, false otherwise.
*/
@Override
public boolean persistState() {
if (!safeMode) {
LOG.info(
"Cannot persist state because ClusterManager is not in Safe Mode");
return false;
}
try {
JsonGenerator jsonGenerator = CoronaSerializer.createJsonGenerator(conf);
jsonGenerator.writeStartObject();
jsonGenerator.writeFieldName("startTime");
jsonGenerator.writeNumber(startTime);
jsonGenerator.writeFieldName("nodeManager");
nodeManager.write(jsonGenerator);
jsonGenerator.writeFieldName("sessionManager");
sessionManager.write(jsonGenerator);
jsonGenerator.writeFieldName("sessionNotifier");
sessionNotifier.write(jsonGenerator);
jsonGenerator.writeEndObject();
jsonGenerator.close();
} catch (IOException e) {
LOG.info("Could not persist the state: ", e);
return false;
}
return true;
}
示例14: close
import org.codehaus.jackson.JsonGenerator; //导入方法依赖的package包/类
public void close() {
// close the json file and write filters
JsonGenerator generator;
try {
generator = jFactory.createJsonGenerator(new FileWriter(jsonFile));
mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true);
generator.setCodec(mapper);
generator.useDefaultPrettyPrinter();
generator.writeTree(rootNode);
generator.close();
//now generate the dot file
File extractDir = new File(docDir, extract);
File xPathDotFile = new File(extractDir, "xpath.dot");
xPathDotWriter = new DotWriter();
xPathDotWriter.open(xPathDotFile);
xPathDotWriter.setOdfFilename(srcsArray.get(0).get("source").getTextValue());
xPathDotWriter.setIncludeLegend(false);
xPathDotWriter.writePaths(xPathRoot);
xPathDotWriter.close();
} catch (IOException e) {
LOGGER.severe(jsonFile.getName() + " IO Error");
e.printStackTrace();
}
}
示例15: formatMap2JSON
import org.codehaus.jackson.JsonGenerator; //导入方法依赖的package包/类
/**
* 将map转化为json
*
* @param map
* @return
*/
public String formatMap2JSON(Map<String, Object> map) {
StringWriter stringWriter = new StringWriter();
String json = "";
try {
JsonGenerator gen = new JsonFactory()
.createJsonGenerator(stringWriter);
MAPPER.writeValue(gen, map);
gen.close();
json = stringWriter.toString();
} catch (Exception e) {
LOGGER.error("", e);
}
return json;
}