Search in sources :

Example 66 with JsonGenerator

use of com.fasterxml.jackson.core.JsonGenerator in project ig-json-parser by Instagram.

the class SerializeTest method serializeWithGetterTest.

@Test
public void serializeWithGetterTest() throws IOException {
    GetterUUT source = new GetterUUT();
    source.intField = 5;
    StringWriter stringWriter = new StringWriter();
    JsonGenerator jsonGenerator = new JsonFactory().createGenerator(stringWriter);
    GetterUUT__JsonHelper.serializeToJson(jsonGenerator, source, true);
    jsonGenerator.close();
    String inputString = stringWriter.toString();
    GetterUUT parsed = GetterUUT__JsonHelper.parseFromJson(inputString);
    assertEquals(10, parsed.intField);
}
Also used : GetterUUT(com.instagram.common.json.annotation.processor.uut.GetterUUT) StringWriter(java.io.StringWriter) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) Test(org.junit.Test)

Example 67 with JsonGenerator

use of com.fasterxml.jackson.core.JsonGenerator in project ig-json-parser by Instagram.

the class SerializeTest method simpleSerializeTest.

@Test
public void simpleSerializeTest() throws IOException, JSONException {
    final int intValue = 25;
    final int integerValue = 37;
    final String stringValue = "hello world\r\n\'\"";
    final List<Integer> integerList = Lists.newArrayList(1, 2, 3, 4);
    final Queue<Integer> integerQueue = Queues.newArrayDeque(Arrays.asList(1, 2, 3, 4));
    final Set<Integer> integerSet = Sets.newHashSet(1, 2, 3, 4);
    final int subIntValue = 30;
    final SimpleParseUUT.SubenumUUT subEnum = SimpleParseUUT.SubenumUUT.A;
    final List<SimpleParseUUT.SubenumUUT> subEnumList = Lists.newArrayList(SimpleParseUUT.SubenumUUT.A, SimpleParseUUT.SubenumUUT.B);
    SimpleParseUUT source = new SimpleParseUUT();
    source.intField = intValue;
    source.integerField = integerValue;
    source.stringField = stringValue;
    source.integerListField = integerList;
    source.integerQueueField = integerQueue;
    source.integerSetField = integerSet;
    source.subobjectField = new SimpleParseUUT.SubobjectParseUUT();
    source.subobjectField.intField = subIntValue;
    source.subenumField = subEnum;
    source.subenumFieldList = subEnumList;
    StringWriter stringWriter = new StringWriter();
    JsonGenerator jsonGenerator = new JsonFactory().createGenerator(stringWriter);
    SimpleParseUUT__JsonHelper.serializeToJson(jsonGenerator, source, true);
    jsonGenerator.close();
    String inputString = stringWriter.toString();
    JsonParser jp = new JsonFactory().createParser(inputString);
    jp.nextToken();
    SimpleParseUUT parsed = SimpleParseUUT__JsonHelper.parseFromJson(jp);
    assertSame(source.intField, parsed.intField);
    assertEquals(source.integerField, parsed.integerField);
    assertEquals(source.stringField, parsed.stringField);
    assertEquals(source.integerListField, parsed.integerListField);
    // NOTE: this is because ArrayDeque hilariously does not implement .equals()/.hashcode().
    assertEquals(Lists.newArrayList(source.integerQueueField), Lists.newArrayList(parsed.integerQueueField));
    assertEquals(source.integerSetField, parsed.integerSetField);
    assertSame(source.subobjectField.intField, parsed.subobjectField.intField);
    assertSame(source.subenumField, parsed.subenumField);
    assertEquals(source.subenumFieldList, parsed.subenumFieldList);
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) SimpleParseUUT(com.instagram.common.json.annotation.processor.uut.SimpleParseUUT) StringWriter(java.io.StringWriter) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test)

Example 68 with JsonGenerator

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

the class SerializationFeaturesTest method testFlushingNotAutomatic.

public void testFlushingNotAutomatic() throws IOException {
    // but should not occur if configured otherwise
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.FLUSH_AFTER_WRITE_VALUE, false);
    StringWriter sw = new StringWriter();
    JsonGenerator g = mapper.getFactory().createGenerator(sw);
    mapper.writeValue(g, Integer.valueOf(13));
    // no flushing now:
    assertEquals("", sw.toString());
    // except when actually flushing
    g.flush();
    assertEquals("13", sw.toString());
    g.close();
    // Also, same should happen with ObjectWriter
    sw = new StringWriter();
    g = mapper.getFactory().createGenerator(sw);
    ObjectWriter ow = mapper.writer();
    ow.writeValue(g, Integer.valueOf(99));
    assertEquals("", sw.toString());
    // except when actually flushing
    g.flush();
    assertEquals("99", sw.toString());
    g.close();
}
Also used : JsonGenerator(com.fasterxml.jackson.core.JsonGenerator)

Example 69 with JsonGenerator

use of com.fasterxml.jackson.core.JsonGenerator in project opentsdb by OpenTSDB.

the class Annotation method getStorageJSON.

/**
   * Serializes the object in a uniform matter for storage. Needed for 
   * successful CAS calls
   * @return The serialized object as a byte array
   */
@VisibleForTesting
byte[] getStorageJSON() {
    // TODO - precalculate size
    final ByteArrayOutputStream output = new ByteArrayOutputStream();
    try {
        final JsonGenerator json = JSON.getFactory().createGenerator(output);
        json.writeStartObject();
        if (tsuid != null && !tsuid.isEmpty()) {
            json.writeStringField("tsuid", tsuid);
        }
        json.writeNumberField("startTime", start_time);
        json.writeNumberField("endTime", end_time);
        json.writeStringField("description", description);
        json.writeStringField("notes", notes);
        if (custom == null) {
            json.writeNullField("custom");
        } else {
            final TreeMap<String, String> sorted_custom = new TreeMap<String, String>(custom);
            json.writeObjectField("custom", sorted_custom);
        }
        json.writeEndObject();
        json.close();
        return output.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException("Unable to serialize Annotation", e);
    }
}
Also used : JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) TreeMap(java.util.TreeMap) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 70 with JsonGenerator

use of com.fasterxml.jackson.core.JsonGenerator in project opentsdb by OpenTSDB.

the class TSMeta method getStorageJSON.

/**
   * Formats the JSON output for writing to storage. It drops objects we don't
   * need or want to store (such as the UIDMeta objects or the total dps) to
   * save space. It also serializes in order so that we can make a proper CAS
   * call. Otherwise the POJO serializer may place the fields in any order
   * and CAS calls would fail all the time.
   * @return A byte array to write to storage
   */
private byte[] getStorageJSON() {
    // 256 bytes is a good starting value, assumes default info
    final ByteArrayOutputStream output = new ByteArrayOutputStream(256);
    try {
        final JsonGenerator json = JSON.getFactory().createGenerator(output);
        json.writeStartObject();
        json.writeStringField("tsuid", tsuid);
        json.writeStringField("displayName", display_name);
        json.writeStringField("description", description);
        json.writeStringField("notes", notes);
        json.writeNumberField("created", created);
        if (custom == null) {
            json.writeNullField("custom");
        } else {
            json.writeObjectFieldStart("custom");
            for (Map.Entry<String, String> entry : custom.entrySet()) {
                json.writeStringField(entry.getKey(), entry.getValue());
            }
            json.writeEndObject();
        }
        json.writeStringField("units", units);
        json.writeStringField("dataType", data_type);
        json.writeNumberField("retention", retention);
        json.writeNumberField("max", max);
        json.writeNumberField("min", min);
        json.writeEndObject();
        json.close();
        return output.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException("Unable to serialize TSMeta", e);
    }
}
Also used : JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)187 StringWriter (java.io.StringWriter)81 IOException (java.io.IOException)52 JsonFactory (com.fasterxml.jackson.core.JsonFactory)44 ByteArrayOutputStream (java.io.ByteArrayOutputStream)25 Map (java.util.Map)17 OutputStream (java.io.OutputStream)14 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)13 JsonParser (com.fasterxml.jackson.core.JsonParser)11 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)10 Test (org.junit.Test)10 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)8 File (java.io.File)7 OutputStreamWriter (java.io.OutputStreamWriter)6 ServletOutputStream (javax.servlet.ServletOutputStream)6 HeapDataOutputStream (org.apache.geode.internal.HeapDataOutputStream)6 List (java.util.List)5 AccessExecutionVertex (org.apache.flink.runtime.executiongraph.AccessExecutionVertex)5 JsonEncoding (com.fasterxml.jackson.core.JsonEncoding)4