Search in sources :

Example 61 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 62 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 63 with ObjectWriter

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

Example 64 with 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);
}
Also used : ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 65 with ObjectWriter

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

Aggregations

ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)124 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)53 IOException (java.io.IOException)28 Test (org.junit.Test)27 File (java.io.File)13 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)12 JavaType (com.fasterxml.jackson.databind.JavaType)10 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)10 ArrayList (java.util.ArrayList)10 FileOutputStream (java.io.FileOutputStream)7 Map (java.util.Map)7 JCommander (com.beust.jcommander.JCommander)6 ParameterException (com.beust.jcommander.ParameterException)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 RateLimiter (com.google.common.util.concurrent.RateLimiter)6 FileInputStream (java.io.FileInputStream)6 OutputStream (java.io.OutputStream)6 Writer (java.io.Writer)6 HashMap (java.util.HashMap)6 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)5