Search in sources :

Example 1 with ObjectWriter

use of com.fasterxml.jackson.databind.ObjectWriter in project buck by facebook.

the class Project method writeJsonConfig.

private void writeJsonConfig(File jsonTempFile, List<SerializableModule> modules, List<SerializablePrebuiltJarRule> libraries, List<SerializableAndroidAar> aars) throws IOException {
    Map<String, Object> config = ImmutableMap.of("modules", modules, "libraries", libraries, "aars", aars, "java", createSerializableIntellijSettings(intellijConfig));
    // Write out the JSON config to be consumed by the Python.
    try (Writer writer = new FileWriter(jsonTempFile)) {
        if (executionContext.getVerbosity().shouldPrintOutput()) {
            ObjectWriter objectWriter = objectMapper.writerWithDefaultPrettyPrinter();
            objectWriter.writeValue(writer, config);
        } else {
            objectMapper.writeValue(writer, config);
        }
    }
}
Also used : FileWriter(java.io.FileWriter) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) Writer(java.io.Writer) FileWriter(java.io.FileWriter)

Example 2 with ObjectWriter

use of com.fasterxml.jackson.databind.ObjectWriter in project hadoop by apache.

the class RumenToSLSConverter method generateSLSLoadFile.

private static void generateSLSLoadFile(String inputFile, String outputFile) throws IOException {
    try (Reader input = new InputStreamReader(new FileInputStream(inputFile), "UTF-8")) {
        try (Writer output = new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8")) {
            ObjectMapper mapper = new ObjectMapper();
            ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter();
            Iterator<Map> i = mapper.readValues(new JsonFactory().createParser(input), Map.class);
            while (i.hasNext()) {
                Map m = i.next();
                output.write(writer.writeValueAsString(createSLSJob(m)) + EOL);
            }
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) FileOutputStream(java.io.FileOutputStream) JsonFactory(com.fasterxml.jackson.core.JsonFactory) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) OutputStreamWriter(java.io.OutputStreamWriter) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) FileInputStream(java.io.FileInputStream) OutputStreamWriter(java.io.OutputStreamWriter) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) Writer(java.io.Writer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with ObjectWriter

use of com.fasterxml.jackson.databind.ObjectWriter in project hadoop by apache.

the class RumenToSLSConverter method generateSLSNodeFile.

@SuppressWarnings("unchecked")
private static void generateSLSNodeFile(String outputFile) throws IOException {
    try (Writer output = new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8")) {
        ObjectMapper mapper = new ObjectMapper();
        ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter();
        for (Map.Entry<String, Set<String>> entry : rackNodeMap.entrySet()) {
            Map rack = new LinkedHashMap();
            rack.put("rack", entry.getKey());
            List nodes = new ArrayList();
            for (String name : entry.getValue()) {
                Map node = new LinkedHashMap();
                node.put("node", name);
                nodes.add(node);
            }
            rack.put("nodes", nodes);
            output.write(writer.writeValueAsString(rack) + EOL);
        }
    }
}
Also used : TreeSet(java.util.TreeSet) Set(java.util.Set) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) OutputStreamWriter(java.io.OutputStreamWriter) ArrayList(java.util.ArrayList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) OutputStreamWriter(java.io.OutputStreamWriter) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) Writer(java.io.Writer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with ObjectWriter

use of com.fasterxml.jackson.databind.ObjectWriter in project jackson-databind by FasterXML.

the class TestJDKSerialization method testObjectWriter.

public void testObjectWriter() throws IOException {
    ObjectWriter origWriter = MAPPER.writer();
    final String EXP_JSON = "{\"x\":2,\"y\":3}";
    final MyPojo p = new MyPojo(2, 3);
    assertEquals(EXP_JSON, origWriter.writeValueAsString(p));
    String json = origWriter.writeValueAsString(new AnyBean().addEntry("a", "b"));
    assertNotNull(json);
    byte[] bytes = jdkSerialize(origWriter);
    ObjectWriter writer2 = jdkDeserialize(bytes);
    assertEquals(EXP_JSON, writer2.writeValueAsString(p));
}
Also used : ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter)

Example 5 with ObjectWriter

use of com.fasterxml.jackson.databind.ObjectWriter in project jackson-databind by FasterXML.

the class ObjectWriterTestBase method test.

protected void test(ObjectMapper mapper, String desc1, T1 inputValue1, Class<? extends T1> inputClass1, String desc2, T2 inputValue2, Class<? extends T2> inputClass2) throws Exception {
    final int REPS;
    {
        final byte[] input1 = mapper.writeValueAsBytes(inputValue1);
        final byte[] input2 = mapper.writeValueAsBytes(inputValue2);
        // Let's try to guestimate suitable size, N megs of output
        REPS = (int) ((double) (targetSizeMegs() * 1000 * 1000) / (double) input1.length);
        System.out.printf("Read %d bytes to bind (%d as array); will do %d repetitions\n", input1.length, input2.length, REPS);
    }
    final ObjectWriter writer0 = mapper.writer().with(SerializationFeature.EAGER_SERIALIZER_FETCH);
    final ObjectWriter writer1 = writer0.forType(inputClass1);
    final ObjectWriter writer2 = writer0.forType(inputClass2);
    int i = 0;
    int roundsDone = 0;
    final int TYPES = 2;
    // Skip first 5 seconds
    long startMeasure = System.currentTimeMillis() + 5000L;
    System.out.print("Warming up");
    final double[] timesMsec = new double[TYPES];
    while (true) {
        final int round = (i % TYPES);
        final boolean lf = (++i % TYPES) == 0;
        String msg;
        ObjectWriter writer;
        Object value;
        switch(round) {
            case 0:
                msg = desc1;
                writer = writer1;
                value = inputValue1;
                break;
            case 1:
                msg = desc2;
                writer = writer2;
                value = inputValue2;
                break;
            default:
                throw new Error();
        }
        double msecs = testSer(REPS, value, writer);
        // skip first N seconds to let results stabilize
        if (startMeasure > 0L) {
            if ((round != 0) || (System.currentTimeMillis() < startMeasure)) {
                System.out.print(".");
                continue;
            }
            startMeasure = 0L;
            System.out.println();
            System.out.println("Starting measurements...");
            Thread.sleep(250L);
            System.out.println();
        }
        timesMsec[round] += msecs;
        if ((i % 17) == 0) {
            System.out.println("[GC]");
            Thread.sleep(100L);
            System.gc();
            Thread.sleep(100L);
        }
        System.out.printf("Test '%s' [hash: 0x%s] -> %.1f msecs\n", msg, hash, msecs);
        Thread.sleep(50L);
        if (!lf) {
            continue;
        }
        if ((++roundsDone % 3) == 0) {
            double den = (double) roundsDone;
            System.out.printf("Averages after %d rounds (%s/%s): %.1f / %.1f msecs\n", roundsDone, desc1, desc2, timesMsec[0] / den, timesMsec[1] / den);
        }
        System.out.println();
    }
}
Also used : 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