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 camel by apache.
the class SObjectTreeTest method shouldSerializeToJson.
@Test
public void shouldSerializeToJson() throws JsonProcessingException {
final ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
final ObjectWriter writer = mapper.writerFor(SObjectTree.class);
final SObjectTree tree = new SObjectTree();
final SObjectNode account1 = new SObjectNode(tree, simpleAccount);
account1.addChild("Contacts", smith);
account1.addChild("Contacts", evans);
tree.addNode(account1);
final SObjectNode account2 = new SObjectNode(tree, simpleAccount2);
tree.addNode(account2);
final String json = writer.writeValueAsString(tree);
assertEquals("Should serialize to JSON as in Salesforce example", //
"{\"records\":[" + //
"{" + //
"\"attributes\":{\"referenceId\":\"ref1\",\"type\":\"Account\"}," + //
"\"Industry\":\"Banking\"," + //
"\"Name\":\"SampleAccount\"," + //
"\"NumberOfEmployees\":100," + //
"\"Phone\":\"1234567890\"," + //
"\"Website\":\"www.salesforce.com\"," + //
"\"Contacts\":{" + //
"\"records\":[" + //
"{" + //
"\"attributes\":{\"referenceId\":\"ref2\",\"type\":\"Contact\"}," + //
"\"Email\":\"sample@salesforce.com\"," + //
"\"LastName\":\"Smith\"," + //
"\"Title\":\"President\"" + //
"}," + //
"{" + //
"\"attributes\":{\"referenceId\":\"ref3\",\"type\":\"Contact\"}," + //
"\"Email\":\"sample@salesforce.com\"," + //
"\"LastName\":\"Evans\"," + //
"\"Title\":\"Vice President\"" + //
"}" + //
"]" + //
"}" + //
"}," + //
"{" + //
"\"attributes\":{\"referenceId\":\"ref4\",\"type\":\"Account\"}," + //
"\"Industry\":\"Banking\"," + //
"\"Name\":\"SampleAccount2\"," + //
"\"NumberOfEmployees\":100," + //
"\"Phone\":\"1234567890\"," + //
"\"Website\":\"www.salesforce2.com\"" + //
"}" + //
"]" + "}", json);
}
use of com.fasterxml.jackson.databind.ObjectWriter in project Fast-Android-Networking by amitshekhariitbhu.
the class JacksonParserFactory method getStringMap.
@Override
public HashMap<String, String> getStringMap(Object object) {
try {
TypeReference<HashMap<String, String>> typeRef = new TypeReference<HashMap<String, String>>() {
};
ObjectWriter objectWriter = mapper.writerFor(object.getClass());
return mapper.readValue(objectWriter.writeValueAsString(object), typeRef);
} catch (Exception e) {
e.printStackTrace();
}
return new HashMap<>();
}
use of com.fasterxml.jackson.databind.ObjectWriter in project pinpoint by naver.
the class ApplicationTimeHistogramTest method testViewModel.
@Test
public void testViewModel() throws IOException {
Application app = new Application("test", ServiceType.STAND_ALONE);
ApplicationTimeHistogramBuilder builder = new ApplicationTimeHistogramBuilder(app, new Range(0, 10 * 6000));
List<ResponseTime> responseHistogramList = createResponseTime(app);
ApplicationTimeHistogram histogram = builder.build(responseHistogramList);
List<ResponseTimeViewModel> viewModel = histogram.createViewModel();
logger.debug("{}", viewModel);
ObjectWriter writer = mapper.writer();
String s = writer.writeValueAsString(viewModel);
logger.debug(s);
}
Aggregations