Search in sources :

Example 81 with JsonFactory

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

the class NobodiesTest method serializeIsNoOp.

@Test
public void serializeIsNoOp() throws IOException {
    StringWriter stringWriter = new StringWriter();
    JsonGenerator jsonGenerator = new JsonFactory().createGenerator(stringWriter);
    NoBodyUUT obj = new NoBodyUUT();
    obj.mValue = "some-value";
    NoBodyUUT__JsonHelper.serializeToJson(jsonGenerator, obj, true);
    jsonGenerator.close();
    String serialized = stringWriter.toString();
    assertEquals("{}", serialized);
}
Also used : StringWriter(java.io.StringWriter) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) NoBodyUUT(com.instagram.common.json.annotation.processor.nobodies.NoBodyUUT) Test(org.junit.Test)

Example 82 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 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 83 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 serializeInterfaceWithWrapperTest.

@Test
public void serializeInterfaceWithWrapperTest() throws IOException {
    StringWriter stringWriter;
    JsonGenerator jsonGenerator;
    InterfaceImplementationUUT obj = new InterfaceImplementationUUT();
    obj.mStringField = "testValue";
    InterfaceImplementation2UUT obj2 = new InterfaceImplementation2UUT();
    obj2.mIntegerField = 5;
    WrapperInterfaceUUT wrapper = new WrapperInterfaceUUT();
    stringWriter = new StringWriter();
    jsonGenerator = new JsonFactory().createGenerator(stringWriter);
    wrapper.mInterfaceParentWithWrapper = obj;
    WrapperInterfaceUUT__JsonHelper.serializeToJson(jsonGenerator, wrapper, true);
    jsonGenerator.close();
    String serialized = stringWriter.toString();
    WrapperInterfaceUUT parsed = WrapperInterfaceUUT__JsonHelper.parseFromJson(serialized);
    assertNotNull(parsed);
    assertTrue(parsed.mInterfaceParentWithWrapper instanceof InterfaceImplementationUUT);
    InterfaceImplementationUUT parsedObj = (InterfaceImplementationUUT) parsed.mInterfaceParentWithWrapper;
    assertEquals(obj.mStringField, parsedObj.mStringField);
    stringWriter = new StringWriter();
    jsonGenerator = new JsonFactory().createGenerator(stringWriter);
    wrapper.mInterfaceParentWithWrapper = obj2;
    WrapperInterfaceUUT__JsonHelper.serializeToJson(jsonGenerator, wrapper, true);
    jsonGenerator.close();
    serialized = stringWriter.toString();
    parsed = WrapperInterfaceUUT__JsonHelper.parseFromJson(serialized);
    assertNotNull(parsed);
    assertTrue(parsed.mInterfaceParentWithWrapper instanceof InterfaceImplementation2UUT);
    InterfaceImplementation2UUT parsedObj2 = (InterfaceImplementation2UUT) parsed.mInterfaceParentWithWrapper;
    assertEquals(obj2.mIntegerField, parsedObj2.mIntegerField);
}
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) InterfaceImplementation2UUT(com.instagram.common.json.annotation.processor.parent.InterfaceImplementation2UUT) Test(org.junit.Test)

Example 84 with JsonFactory

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

the class MalformedJsonTest method scalarInsteadOfArray.

@Test
public void scalarInsteadOfArray() throws IOException, JSONException {
    final int intValue = 25;
    final int integerValue = 37;
    final String stringValue = "hello world\r\n\'\"";
    final int subIntValue = 30;
    StringWriter stringWriter = new StringWriter();
    ExtensibleJSONWriter writer = new ExtensibleJSONWriter(stringWriter);
    writer.object().key(SimpleParseUUT.INT_FIELD_NAME).value(intValue).key(SimpleParseUUT.INTEGER_FIELD_NAME).value(integerValue).key(SimpleParseUUT.STRING_FIELD_NAME).value(stringValue).key(SimpleParseUUT.INTEGER_LIST_FIELD_NAME).value(intValue).key(SimpleParseUUT.SUBOBJECT_FIELD_NAME).object().key(SimpleParseUUT.SubobjectParseUUT.INT_FIELD_NAME).value(subIntValue).endObject().endObject();
    String inputString = stringWriter.toString();
    JsonParser jp = new JsonFactory().createParser(inputString);
    jp.nextToken();
    SimpleParseUUT uut = SimpleParseUUT__JsonHelper.parseFromJson(jp);
    assertSame(intValue, uut.intField);
    assertSame(integerValue, uut.integerField.intValue());
    assertEquals(stringValue, uut.stringField);
    assertNull(uut.integerListField);
    assertSame(subIntValue, uut.subobjectField.intField);
}
Also used : StringWriter(java.io.StringWriter) ExtensibleJSONWriter(com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter) JsonFactory(com.fasterxml.jackson.core.JsonFactory) SimpleParseUUT(com.instagram.common.json.annotation.processor.uut.SimpleParseUUT) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test)

Example 85 with JsonFactory

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory in project drill by apache.

the class Metadata method writeFile.

/**
 * Serialize parquet metadata to json and write to a file.
 *
 * @param parquetMetadata parquet table or directory metadata
 * @param p file path
 * @param fs Drill file system
 * @throws IOException if metadata can't be serialized
 */
private void writeFile(Object parquetMetadata, Path p, FileSystem fs) throws IOException {
    JsonFactory jsonFactory = new JsonFactory();
    jsonFactory.configure(Feature.AUTO_CLOSE_TARGET, false);
    jsonFactory.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false);
    ObjectMapper mapper = new ObjectMapper(jsonFactory);
    SimpleModule module = new SimpleModule();
    module.addSerializer(Path.class, new PathSerDe.Se());
    if (parquetMetadata instanceof Metadata_V4.FileMetadata) {
        module.addSerializer(ColumnMetadata_v4.class, new ColumnMetadata_v4.Serializer());
    }
    mapper.registerModule(module);
    OutputStream os = fs.create(p);
    mapper.writerWithDefaultPrettyPrinter().writeValue(os, parquetMetadata);
    os.flush();
    os.close();
}
Also used : ColumnMetadata_v4(org.apache.drill.exec.store.parquet.metadata.Metadata_V4.ColumnMetadata_v4) OutputStream(java.io.OutputStream) JsonFactory(com.fasterxml.jackson.core.JsonFactory) FileMetadata(org.apache.drill.exec.store.parquet.metadata.Metadata_V4.FileMetadata) ParquetFileMetadata(org.apache.drill.exec.store.parquet.metadata.MetadataBase.ParquetFileMetadata) PathSerDe(org.apache.drill.exec.serialization.PathSerDe) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

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