下面列出了com.fasterxml.jackson.core.JsonGenerator#setCodec ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Persist an object to disk. This will most likely be an area or region, but in theory you could
* write anything that serializes properly.
* @param object
* @param filename
*/
public void persist(Object object, String filename) {
String path = commander.savePath();
if (commander.config.isPersistentAreas()) {
File dir = new File(path);
dir.mkdirs();
log.info("saving file " + path + filename);
File file = new File(path + filename);
try (
FileOutputStream stream = new FileOutputStream(file);
GZIPOutputStream gzip = new GZIPOutputStream(stream)
) {
JsonFactory jfactory = new JsonFactory();
JsonGenerator jGenerator = jfactory
.createGenerator(gzip, JsonEncoding.UTF8);
jGenerator.setCodec(objectMapper);
jGenerator.writeObject(object);
jGenerator.close();
} catch (IOException e) {
throw new RuntimeException("Couldn't persist object " + object.toString(), e);
}
}
}
public void persist(String absoluteFilepath) {
File file = new File(absoluteFilepath);
try (
FileOutputStream stream = new FileOutputStream(file);
//GZIPOutputStream gzip = new GZIPOutputStream(stream)
) {
JsonFactory jfactory = new JsonFactory();
JsonGenerator jGenerator = jfactory
.createGenerator(stream, JsonEncoding.UTF8);
jGenerator.setCodec(new ObjectMapper());
jGenerator.writeObject(this);
jGenerator.close();
} catch (IOException e) {
throw new RuntimeException("Couldn't persist object " + toString(), e);
}
}
public void persistPlayer() {
if (player.area().getLabel().equals("vaulted"))
return;
log.debug("Persisting player " + player.getName() + "...");
player.saveStateData();
String path = savePath();
File file = new File(path + "player");
try (
FileOutputStream stream = new FileOutputStream(file);
//GZIPOutputStream gzip = new GZIPOutputStream(stream)
) {
JsonFactory jfactory = new JsonFactory();
JsonGenerator jGenerator = jfactory.createGenerator(stream, JsonEncoding.UTF8);
jGenerator.setCodec(objectMapper);
jGenerator.writeObject(player);
jGenerator.close();
} catch (IOException e) {
e.printStackTrace();
}
}
void saveScaper(ULandscaper scaper, String filename) {
String path = commander.savePath();
File file = new File(path + filename);
try (
FileOutputStream stream = new FileOutputStream(file);
) {
JsonFactory jfactory = new JsonFactory();
JsonGenerator jGenerator = jfactory
.createGenerator(stream, JsonEncoding.UTF8);
jGenerator.setCodec(objectMapper);
jGenerator.writeObject(scaper);
jGenerator.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String toString(PartitionStrategy strategy, boolean pretty) {
StringWriter writer = new StringWriter();
JsonGenerator gen;
try {
gen = new JsonFactory().createGenerator(writer);
if (pretty) {
gen.useDefaultPrettyPrinter();
}
gen.setCodec(new ObjectMapper());
gen.writeTree(toJson(strategy));
gen.close();
} catch (IOException e) {
throw new DatasetIOException("Cannot write to JSON generator", e);
}
return writer.toString();
}
public static String toString(ColumnMapping mapping, boolean pretty) {
StringWriter writer = new StringWriter();
JsonGenerator gen;
try {
gen = new JsonFactory().createGenerator(writer);
if (pretty) {
gen.useDefaultPrettyPrinter();
}
gen.setCodec(new ObjectMapper());
gen.writeTree(toJson(mapping));
gen.close();
} catch (IOException e) {
throw new DatasetIOException("Cannot write to JSON generator", e);
}
return writer.toString();
}
void writeJson(ArrayList<Icon> icons, String filename) {
log.info("writing " + filename);
File file = new File(commander.config.getResourcePath() + "icons/" + filename);
try (FileOutputStream stream = new FileOutputStream(file);) {
JsonFactory jfactory = new JsonFactory();
JsonGenerator jGenerator = jfactory.createGenerator(stream, JsonEncoding.UTF8);
jGenerator.setCodec(objectMapper);
jGenerator.writeObject(icons);
jGenerator.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
void givenSecurityScheme_whenItIsSerializingToJson_thenEnumTypesShouldPrintedLowercase() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
Writer jsonWriter = new StringWriter();
JsonGenerator jsonGenerator = new JsonFactory().createGenerator(jsonWriter);
jsonGenerator.setCodec(objectMapper);
SerializerProvider provider = objectMapper.getSerializerProvider();
new SecuritySchemeSerializer().serialize(getDummyScheme(), jsonGenerator, provider);
jsonGenerator.flush();
assertEquals("{\"type\":\"http\",\"description\":\"desc\",\"name\":\"name\",\"$ref\":\"#/components/securitySchemes/ref\",\"in\":\"cookie\",\"scheme\":\"scheme\",\"bearerFormat\":\"format\",\"flows\":{},\"openIdConnectUrl\":\"url\",\"extensions\":{}}", jsonWriter.toString());
}
@Override
public void serialize(Part value, JsonGenerator gen, SerializerProvider provider) throws IOException {
ObjectCodec preservedCodec = ((TokenBuffer) gen).asParser().getCodec();
// set codec as null to avoid recursive dead loop
// JsonGenerator is instantiated for each serialization, so there should be no thread safe issue
gen.setCodec(null);
gen.writeObject(value);
gen.setCodec(preservedCodec);
}
/**
* Formats the {@code node} as a string using the pretty printer.
*/
public static String jsonPretty(JsonNode node) throws IOException {
StringBuilder buf = new StringBuilder();
JsonGenerator gen = JSON_FACTORY.createGenerator(new StringBuilderWriter(buf));
gen.useDefaultPrettyPrinter();
gen.setCodec(JsonUtils.getMapper());
gen.writeTree(node);
return buf.toString();
}
@Override
public void serialize(final Object entity, final OutputStream out) throws IOException {
final JsonFactory factory = new JsonFactory();
final JsonGenerator generator = factory.createGenerator(out);
generator.setCodec(jsonCodec);
generator.writeObject(entity);
generator.flush();
}
protected JsonGenerator getJsonGenerator( File ephermal ) throws IOException {
//TODO:shouldn't the below be UTF-16?
JsonGenerator jg = jsonFactory.createJsonGenerator( ephermal, JsonEncoding.UTF8 );
jg.setPrettyPrinter( new DefaultPrettyPrinter( ) );
jg.setCodec( new ObjectMapper() );
return jg;
}
protected JsonGenerator getJsonGenerator( File outFile ) throws IOException {
PrintWriter out = new PrintWriter( outFile, "UTF-8" );
JsonGenerator jg = jsonFactory.createJsonGenerator( out );
jg.setPrettyPrinter( new DefaultPrettyPrinter() );
jg.setCodec( new ObjectMapper() );
return jg;
}
public static String toString(FieldMapping mapping) {
StringWriter writer = new StringWriter();
JsonGenerator gen;
try {
gen = new JsonFactory().createGenerator(writer);
gen.setCodec(new ObjectMapper());
gen.writeTree(toJson(mapping));
gen.close();
} catch (IOException e) {
throw new DatasetIOException("Cannot write to JSON generator", e);
}
return writer.toString();
}
/**
* Describe the supported error codes.
*
* @param output the http response.
*/
@RequestMapping(value = "/api/errors", method = RequestMethod.GET, produces = APPLICATION_JSON_VALUE)
@ApiOperation(value = "Get all supported errors.", notes = "Returns the list of all supported errors.")
@Timed
public void listErrors(final OutputStream output) throws IOException {
LOG.debug("Listing supported error codes");
JsonFactory factory = new JsonFactory();
JsonGenerator generator = factory.createGenerator(output);
generator.setCodec(mapper);
// start the errors array
generator.writeStartArray();
// write the direct known errors
writeErrorsFromEnum(generator, CommonErrorCodes.values());
writeErrorsFromEnum(generator, APIErrorCodes.values());
// get dataset api errors
HystrixCommand<InputStream> datasetErrors = getCommand(ErrorList.class, GenericCommand.DATASET_GROUP, DATASET);
try (InputStream errorsInput = datasetErrors.execute()) {
writeErrorsFromApi(generator, errorsInput);
}
// get preparation api errors
HystrixCommand<InputStream> preparationErrors =
getCommand(ErrorList.class, GenericCommand.PREPARATION_GROUP, PREPARATION);
try (InputStream errorsInput = preparationErrors.execute()) {
writeErrorsFromApi(generator, errorsInput);
}
// get transformation api errors
HystrixCommand<InputStream> transformationErrors =
getCommand(ErrorList.class, GenericCommand.TRANSFORM_GROUP, TRANSFORMATION);
try (InputStream errorsInput = transformationErrors.execute()) {
writeErrorsFromApi(generator, errorsInput);
}
// close the errors array
generator.writeEndArray();
generator.flush();
}