Search in sources :

Example 21 with JsonFactory

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

the class JsonMatcher method matchesSafely.

@Override
protected boolean matchesSafely(String actualJson, Description description) {
    try {
        JsonFactory factory = MAPPER.getFactory();
        JsonParser jsonParser = factory.createParser(String.format(expectedJson));
        JsonNode expectedObject = MAPPER.readTree(jsonParser);
        jsonParser = factory.createParser(actualJson);
        JsonNode actualObject = MAPPER.readTree(jsonParser);
        if (!matchJsonObjects("/", expectedObject, actualObject, description)) {
            description.appendText(String.format(" in <%s>", actualJson));
            return false;
        }
    } catch (IOException e) {
        description.appendText(String.format("could not parse the following into a json object: <%s>", actualJson));
        return false;
    }
    return true;
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 22 with JsonFactory

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

the class Configuration method dumpConfiguration.

/**
   *  Writes out all properties and their attributes (final and resource) to
   *  the given {@link Writer}, the format of the output would be,
   *
   *  <pre>
   *  { "properties" :
   *      [ { key : "key1",
   *          value : "value1",
   *          isFinal : "key1.isFinal",
   *          resource : "key1.resource" },
   *        { key : "key2",
   *          value : "value2",
   *          isFinal : "ke2.isFinal",
   *          resource : "key2.resource" }
   *       ]
   *   }
   *  </pre>
   *
   *  It does not output the properties of the configuration object which
   *  is loaded from an input stream.
   *  <p>
   *
   * @param config the configuration
   * @param out the Writer to write to
   * @throws IOException
   */
public static void dumpConfiguration(Configuration config, Writer out) throws IOException {
    JsonFactory dumpFactory = new JsonFactory();
    JsonGenerator dumpGenerator = dumpFactory.createGenerator(out);
    dumpGenerator.writeStartObject();
    dumpGenerator.writeFieldName("properties");
    dumpGenerator.writeStartArray();
    dumpGenerator.flush();
    synchronized (config) {
        for (Map.Entry<Object, Object> item : config.getProps().entrySet()) {
            appendJSONProperty(dumpGenerator, config, item.getKey().toString());
        }
    }
    dumpGenerator.writeEndArray();
    dumpGenerator.writeEndObject();
    dumpGenerator.flush();
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) UnmodifiableMap(org.apache.commons.collections.map.UnmodifiableMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap)

Example 23 with JsonFactory

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

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

the class DataPacketDto method getDataPacketStream.

public static Stream<DataPacket> getDataPacketStream(InputStream inputStream) throws IOException {
    JsonParser jsonParser = new JsonFactory().createParser(inputStream);
    if (jsonParser.nextToken() != JsonToken.START_ARRAY) {
        throw new IOException("Expecting start array token to begin object array.");
    }
    jsonParser.setCodec(new ObjectMapper());
    return StreamSupport.stream(Spliterators.spliteratorUnknownSize(new Iterator<DataPacket>() {

        DataPacket next = getNext();

        @Override
        public boolean hasNext() {
            return next != null;
        }

        @Override
        public DataPacket next() {
            DataPacket next = this.next;
            this.next = getNext();
            return next;
        }

        DataPacket getNext() throws RuntimeException {
            try {
                if (jsonParser.nextToken() == JsonToken.END_ARRAY) {
                    return null;
                }
                DataPacketDto dataPacketDto = jsonParser.readValueAs(DATA_PACKET_DTO_TYPE_REFERENCE);
                return dataPacketDto.toDataPacket();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }, Spliterator.ORDERED), false);
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) Iterator(java.util.Iterator) IOException(java.io.IOException) DataPacket(org.apache.nifi.remote.protocol.DataPacket) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 25 with JsonFactory

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

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