Search in sources :

Example 71 with JsonFactory

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory in project ig-json-parser by Instagram.

the class SerializeTest method serializeInterfaceNoFormattersTest.

@Test
public void serializeInterfaceNoFormattersTest() throws IOException {
    StringWriter stringWriter = new StringWriter();
    JsonGenerator jsonGenerator = new JsonFactory().createGenerator(stringWriter);
    ;
    InterfaceImplementationUUT obj = new InterfaceImplementationUUT();
    obj.mStringField = "testValue";
    WrapperInterfaceUUT wrapper = new WrapperInterfaceUUT();
    wrapper.mInterfaceParentNoFormatters = obj;
    WrapperInterfaceUUT__JsonHelper.serializeToJson(jsonGenerator, wrapper, true);
    jsonGenerator.close();
    String serialized = stringWriter.toString();
    WrapperInterfaceUUT parsed = WrapperInterfaceUUT__JsonHelper.parseFromJson(serialized);
    assertNotNull(parsed);
    assertTrue(parsed.mInterfaceParentNoFormatters instanceof InterfaceImplementationUUT);
    InterfaceImplementationUUT parsedObj = (InterfaceImplementationUUT) parsed.mInterfaceParentNoFormatters;
    assertEquals(obj.mStringField, parsedObj.mStringField);
}
Also used : StringWriter(java.io.StringWriter) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) InterfaceImplementationUUT(com.instagram.common.json.annotation.processor.parent.InterfaceImplementationUUT) Test(org.junit.Test)

Example 72 with JsonFactory

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory in project ig-json-parser by Instagram.

the class SerializeTest method serializeInterfaceDynamicTest.

@Test
public void serializeInterfaceDynamicTest() throws IOException {
    StringWriter stringWriter = new StringWriter();
    JsonGenerator jsonGenerator = new JsonFactory().createGenerator(stringWriter);
    ;
    InterfaceImplementationUUT obj = new InterfaceImplementationUUT();
    obj.mStringField = "testValue";
    InterfaceParentDynamicUUTHelper.DISPATCHER.register(InterfaceImplementationUUT.TYPE_NAME, InterfaceImplementationUUT.ADAPTER);
    WrapperInterfaceUUT wrapper = new WrapperInterfaceUUT();
    wrapper.mInterfaceParentDynamic = obj;
    WrapperInterfaceUUT__JsonHelper.serializeToJson(jsonGenerator, wrapper, true);
    jsonGenerator.close();
    String serialized = stringWriter.toString();
    WrapperInterfaceUUT parsed = WrapperInterfaceUUT__JsonHelper.parseFromJson(serialized);
    assertNotNull(parsed);
    assertTrue(parsed.mInterfaceParentDynamic instanceof InterfaceImplementationUUT);
    InterfaceImplementationUUT parsedObj = (InterfaceImplementationUUT) parsed.mInterfaceParentDynamic;
    assertEquals(obj.mStringField, parsedObj.mStringField);
}
Also used : StringWriter(java.io.StringWriter) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) InterfaceImplementationUUT(com.instagram.common.json.annotation.processor.parent.InterfaceImplementationUUT) Test(org.junit.Test)

Example 73 with JsonFactory

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory 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 74 with JsonFactory

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory in project ig-json-parser by Instagram.

the class SerializeTest method serializeInterfaceTest.

@Test
public void serializeInterfaceTest() throws IOException {
    StringWriter stringWriter = new StringWriter();
    JsonGenerator jsonGenerator = new JsonFactory().createGenerator(stringWriter);
    ;
    InterfaceImplementationUUT obj = new InterfaceImplementationUUT();
    obj.mStringField = "testValue";
    WrapperInterfaceUUT wrapper = new WrapperInterfaceUUT();
    wrapper.mInterfaceParent = obj;
    WrapperInterfaceUUT__JsonHelper.serializeToJson(jsonGenerator, wrapper, true);
    jsonGenerator.close();
    String serialized = stringWriter.toString();
    WrapperInterfaceUUT parsed = WrapperInterfaceUUT__JsonHelper.parseFromJson(serialized);
    assertNotNull(parsed);
    assertTrue(parsed.mInterfaceParent instanceof InterfaceImplementationUUT);
    InterfaceImplementationUUT parsedObj = (InterfaceImplementationUUT) parsed.mInterfaceParent;
    assertEquals(obj.mStringField, parsedObj.mStringField);
}
Also used : StringWriter(java.io.StringWriter) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) InterfaceImplementationUUT(com.instagram.common.json.annotation.processor.parent.InterfaceImplementationUUT) Test(org.junit.Test)

Example 75 with JsonFactory

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory in project ig-json-parser by Instagram.

the class DeserializeTest method fieldAssignmentTest.

@Test
public void fieldAssignmentTest() throws IOException, JSONException {
    final int encodedValue = 25;
    final int deserializedValue = -encodedValue;
    StringWriter stringWriter = new StringWriter();
    JSONWriter writer = new JSONWriter(stringWriter);
    writer.object().key(FormatterUUT.FIELD_ASSIGNMENT_FIELD_NAME).value(encodedValue).endObject();
    String inputString = stringWriter.toString();
    JsonParser jp = new JsonFactory().createParser(inputString);
    jp.nextToken();
    FormatterUUT uut = FormatterUUT__JsonHelper.parseFromJson(jp);
    assertSame(deserializedValue, uut.getFieldAssignmentFormatter());
}
Also used : JSONWriter(org.json.JSONWriter) ExtensibleJSONWriter(com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter) StringWriter(java.io.StringWriter) FormatterUUT(com.instagram.common.json.annotation.processor.uut.FormatterUUT) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test)

Aggregations

JsonFactory (com.fasterxml.jackson.core.JsonFactory)266 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)102 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)91 JsonParser (com.fasterxml.jackson.core.JsonParser)78 Test (org.junit.Test)65 IOException (java.io.IOException)62 StringWriter (java.io.StringWriter)60 Map (java.util.Map)27 HashMap (java.util.HashMap)26 ArrayList (java.util.ArrayList)25 JsonNode (com.fasterxml.jackson.databind.JsonNode)21 List (java.util.List)18 ExtensibleJSONWriter (com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter)16 JsonToken (com.fasterxml.jackson.core.JsonToken)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 File (java.io.File)14 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)9 InputStreamReader (java.io.InputStreamReader)9 TypeReference (com.fasterxml.jackson.core.type.TypeReference)8 SimpleParseUUT (com.instagram.common.json.annotation.processor.uut.SimpleParseUUT)8