Search in sources :

Example 46 with JsonGenerator

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project open-kilda by telstra.

the class TopologyPrinter method toJson.

/**
 * @return A json representation of the topology. Intended for printing only.
 */
public static final String toJson(ITopology topo, boolean pretty) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    if (pretty)
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
    StringWriter sw = new StringWriter();
    JsonFactory f = mapper.getFactory();
    try (JsonGenerator g = f.createGenerator(sw)) {
        g.writeStartObject();
        // use TreeSet to sort the list
        g.writeObjectField("switches", new TreeSet<String>(topo.getSwitches().keySet()));
        g.writeObjectField("links", new TreeSet<String>(topo.getLinks().keySet()));
        g.writeEndObject();
    }
    return sw.toString();
}
Also used : StringWriter(java.io.StringWriter) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 47 with JsonGenerator

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project nifi by apache.

the class RedisStateMapJsonSerDe method serialize.

@Override
public byte[] serialize(final RedisStateMap stateMap) throws IOException {
    if (stateMap == null) {
        return null;
    }
    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        final JsonGenerator jsonGenerator = jsonFactory.createGenerator(out);
        jsonGenerator.writeStartObject();
        jsonGenerator.writeNumberField(FIELD_VERSION, stateMap.getVersion());
        jsonGenerator.writeNumberField(FIELD_ENCODING, stateMap.getEncodingVersion());
        jsonGenerator.writeObjectFieldStart(FIELD_STATE_VALUES);
        for (Map.Entry<String, String> entry : stateMap.toMap().entrySet()) {
            jsonGenerator.writeStringField(entry.getKey(), entry.getValue());
        }
        jsonGenerator.writeEndObject();
        jsonGenerator.writeEndObject();
        jsonGenerator.flush();
        return out.toByteArray();
    }
}
Also used : JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Map(java.util.Map)

Example 48 with JsonGenerator

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project nifi by apache.

the class SiteToSiteReceiver method receiveFiles.

public TransactionCompletion receiveFiles() throws IOException {
    Transaction transaction = siteToSiteClient.createTransaction(TransferDirection.RECEIVE);
    JsonGenerator jsonGenerator = new JsonFactory().createJsonGenerator(output);
    jsonGenerator.writeStartArray();
    DataPacket dataPacket;
    while ((dataPacket = transaction.receive()) != null) {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeFieldName("attributes");
        jsonGenerator.writeStartObject();
        Map<String, String> attributes = dataPacket.getAttributes();
        if (attributes != null) {
            for (Map.Entry<String, String> stringStringEntry : attributes.entrySet()) {
                jsonGenerator.writeStringField(stringStringEntry.getKey(), stringStringEntry.getValue());
            }
        }
        jsonGenerator.writeEndObject();
        InputStream data = dataPacket.getData();
        if (data != null) {
            jsonGenerator.writeBinaryField("data", IOUtils.toByteArray(data));
        }
        jsonGenerator.writeEndObject();
    }
    jsonGenerator.writeEndArray();
    jsonGenerator.close();
    transaction.confirm();
    return transaction.complete();
}
Also used : Transaction(org.apache.nifi.remote.Transaction) InputStream(java.io.InputStream) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) DataPacket(org.apache.nifi.remote.protocol.DataPacket) Map(java.util.Map)

Example 49 with JsonGenerator

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project CFLint by cflint.

the class TestCFLintConfig method test2.

@Test
public void test2() throws IOException {
    StringWriter writer = new StringWriter();
    JsonFactory jsonF = new JsonFactory();
    JsonGenerator jg = jsonF.createGenerator(writer);
    jg.writeStartArray();
    jg.writeEndArray();
    jg.close();
    writer.close();
    System.out.println(writer);
}
Also used : StringWriter(java.io.StringWriter) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) Test(org.junit.Test)

Example 50 with JsonGenerator

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project geode by apache.

the class PdxToJSON method getJSON.

public String getJSON() {
    JsonFactory jf = new JsonFactory();
    // OutputStream os = new ByteArrayOutputStream();
    HeapDataOutputStream hdos = new HeapDataOutputStream(org.apache.geode.internal.Version.CURRENT);
    try {
        JsonGenerator jg = jf.createJsonGenerator(hdos, JsonEncoding.UTF8);
        enableDisableJSONGeneratorFeature(jg);
        getJSONString(jg, m_pdxInstance);
        jg.close();
        return new String(hdos.toByteArray());
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage());
    } finally {
        hdos.close();
    }
}
Also used : HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) IOException(java.io.IOException)

Aggregations

JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)704 KriptonJsonContext (com.abubusoft.kripton.KriptonJsonContext)257 KriptonByteArrayOutputStream (com.abubusoft.kripton.common.KriptonByteArrayOutputStream)257 KriptonRuntimeException (com.abubusoft.kripton.exception.KriptonRuntimeException)257 JacksonWrapperSerializer (com.abubusoft.kripton.persistence.JacksonWrapperSerializer)257 IOException (java.io.IOException)169 StringWriter (java.io.StringWriter)144 JsonFactory (com.fasterxml.jackson.core.JsonFactory)101 ByteArrayOutputStream (java.io.ByteArrayOutputStream)66 Map (java.util.Map)57 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)54 HashMap (java.util.HashMap)40 Test (org.junit.Test)30 File (java.io.File)27 ArrayList (java.util.ArrayList)25 OutputStream (java.io.OutputStream)24 Writer (java.io.Writer)22 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)21 SerializerProvider (com.fasterxml.jackson.databind.SerializerProvider)20 FileOutputStream (java.io.FileOutputStream)19