use of com.fasterxml.jackson.databind.ObjectWriter in project jvm-serializers by eishay.
the class JacksonAvroDatabind method register.
public static void register(TestGroups groups) {
ObjectMapper mapper = new ObjectMapper(new AvroFactory());
mapper.enable(SerializationFeature.WRITE_ENUMS_USING_INDEX);
JavaType type = mapper.constructType(MediaContent.class);
AvroSchema schema = new AvroSchema(Avro.Media.sMediaContent);
ObjectReader reader = mapper.readerFor(type).with(schema);
ObjectWriter writer = mapper.writerFor(type).with(schema);
groups.media.add(JavaBuiltIn.mediaTransformer, new StdJacksonDataBind<MediaContent>("avro/jackson/databind", type, mapper, reader, writer), new SerFeatures(SerFormat.JSON, SerGraph.FLAT_TREE, SerClass.ZERO_KNOWLEDGE, ""));
}
use of com.fasterxml.jackson.databind.ObjectWriter in project buck by facebook.
the class AutodepsWriter method writeSignedFile.
/**
* Writes the file only if the contents are different to avoid creating noise for Watchman/buckd.
* @param deps Keys must be sorted so the output is generated consistently.
* @param includeSignature Whether to insert a signature for the contents of the file.
* @param generatedFile Where to write the generated output.
* @param mapper To aid in JSON serialization.
* @return whether the file was written
*/
private static boolean writeSignedFile(SortedMap<String, SortedMap<String, Iterable<String>>> deps, boolean includeSignature, Path generatedFile, ObjectMapper mapper) throws IOException {
try (ByteArrayOutputStream bytes = new ByteArrayOutputStream();
HashingOutputStream hashingOutputStream = new HashingOutputStream(Hashing.sha1(), bytes)) {
ObjectWriter jsonWriter = mapper.writer(PRETTY_PRINTER.get());
jsonWriter.writeValue(includeSignature ? hashingOutputStream : bytes, deps);
// Flush a trailing newline through the HashingOutputStream so it is included both the
// output and the signature calculation.
hashingOutputStream.write('\n');
String serializedJson = bytes.toString(Charsets.UTF_8.name());
String contentsToWrite;
if (includeSignature) {
HashCode hash = hashingOutputStream.hash();
contentsToWrite = String.format(AUTODEPS_CONTENTS_FORMAT_STRING, hash, serializedJson);
} else {
contentsToWrite = serializedJson;
}
// to indiscriminately invalidate any cached build rules for the associated build file.
if (generatedFile.toFile().isFile()) {
String existingContents = com.google.common.io.Files.toString(generatedFile.toFile(), Charsets.UTF_8);
if (contentsToWrite.equals(existingContents)) {
return false;
}
}
try (Writer writer = Files.newBufferedWriter(generatedFile, Charsets.UTF_8)) {
writer.write(contentsToWrite);
}
return true;
}
}
use of com.fasterxml.jackson.databind.ObjectWriter in project torodb by torodb.
the class ConfigUtils method printXmlConfig.
public static <T> void printXmlConfig(T config, Console console) throws IOException, JsonGenerationException, JsonMappingException {
ObjectMapper objectMapper = xmlMapper();
ObjectWriter objectWriter = objectMapper.writer();
objectWriter = objectWriter.withRootName("config");
printConfig(config, console, objectWriter);
}
use of com.fasterxml.jackson.databind.ObjectWriter in project jackson-databind by FasterXML.
the class TestJDKSerialization method testEnumHandlers.
// for [databind#899]
public void testEnumHandlers() throws IOException {
ObjectMapper mapper = new ObjectMapper();
// ensure we have serializers and/or deserializers, first
String json = mapper.writerFor(EnumPOJO.class).writeValueAsString(new EnumPOJO());
EnumPOJO result = mapper.readerFor(EnumPOJO.class).readValue(json);
assertNotNull(result);
// and then use JDK serialization to freeze/thaw objects
byte[] bytes = jdkSerialize(mapper);
ObjectMapper mapper2 = jdkDeserialize(bytes);
assertNotNull(mapper2);
bytes = jdkSerialize(mapper.readerFor(EnumPOJO.class));
ObjectReader r = jdkDeserialize(bytes);
assertNotNull(r);
/* 14-Aug-2015, tatu: Looks like pre-loading JsonSerializer is problematic
* at this point; comment out for now. Try to fix later on.
*/
bytes = jdkSerialize(mapper.writerFor(EnumPOJO.class));
ObjectWriter w = jdkDeserialize(bytes);
assertNotNull(w);
// plus, ensure objects are usable:
String json2 = w.writeValueAsString(new EnumPOJO());
assertEquals(json, json2);
EnumPOJO result2 = r.readValue(json2);
assertNotNull(result2);
}
use of com.fasterxml.jackson.databind.ObjectWriter in project retrofit by square.
the class JacksonConverterFactory method requestBodyConverter.
@Override
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
JavaType javaType = mapper.getTypeFactory().constructType(type);
ObjectWriter writer = mapper.writerFor(javaType);
return new JacksonRequestBodyConverter<>(writer);
}
Aggregations