Search in sources :

Example 26 with ObjectWriter

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, ""));
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) AvroFactory(com.fasterxml.jackson.dataformat.avro.AvroFactory) AvroSchema(com.fasterxml.jackson.dataformat.avro.AvroSchema) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) MediaContent(data.media.MediaContent) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 27 with ObjectWriter

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;
    }
}
Also used : HashCode(com.google.common.hash.HashCode) HashingOutputStream(com.google.common.hash.HashingOutputStream) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) Writer(java.io.Writer)

Example 28 with ObjectWriter

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);
}
Also used : ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 29 with ObjectWriter

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<>();
}
Also used : HashMap(java.util.HashMap) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) TypeReference(com.fasterxml.jackson.core.type.TypeReference)

Example 30 with ObjectWriter

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);
}
Also used : ApplicationTimeHistogram(com.navercorp.pinpoint.web.applicationmap.histogram.ApplicationTimeHistogram) ApplicationTimeHistogramBuilder(com.navercorp.pinpoint.web.applicationmap.histogram.ApplicationTimeHistogramBuilder) ResponseTimeViewModel(com.navercorp.pinpoint.web.view.ResponseTimeViewModel) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ResponseTime(com.navercorp.pinpoint.web.vo.ResponseTime) Range(com.navercorp.pinpoint.web.vo.Range) Application(com.navercorp.pinpoint.web.vo.Application) Test(org.junit.Test)

Aggregations

ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)42 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)18 Test (org.junit.Test)12 JavaType (com.fasterxml.jackson.databind.JavaType)7 IOException (java.io.IOException)7 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)4 FilterProvider (com.fasterxml.jackson.databind.ser.FilterProvider)4 SimpleFilterProvider (com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)4 OutputStream (java.io.OutputStream)4 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)3 PluginTestVerifier (com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier)3 FileOutputStream (java.io.FileOutputStream)3 Writer (java.io.Writer)3 JCommander (com.beust.jcommander.JCommander)2 ParameterException (com.beust.jcommander.ParameterException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 SerializationConfig (com.fasterxml.jackson.databind.SerializationConfig)2 RateLimiter (com.google.common.util.concurrent.RateLimiter)2 Application (com.navercorp.pinpoint.web.vo.Application)2 Range (com.navercorp.pinpoint.web.vo.Range)2