Search in sources :

Example 1 with ArrayNode

use of com.fasterxml.jackson.databind.node.ArrayNode in project jvm-serializers by eishay.

the class JacksonJsonTree method readMedia.

protected static Media readMedia(JsonNode node) {
    Media media = new Media();
    JsonNode bitrate = node.get("bitrate");
    if (bitrate != null && !bitrate.isNull()) {
        media.bitrate = bitrate.intValue();
        media.hasBitrate = true;
    }
    media.copyright = node.path("copyright").textValue();
    media.duration = node.path("duration").longValue();
    media.format = node.path("format").textValue();
    media.height = node.path("height").intValue();
    media.player = Media.Player.valueOf(node.get("player").textValue());
    ArrayNode personsArrayNode = (ArrayNode) node.get("persons");
    int size = personsArrayNode.size();
    List<String> persons = new ArrayList<String>(size);
    for (JsonNode person : personsArrayNode) {
        persons.add(person.textValue());
    }
    media.persons = persons;
    media.size = node.get("size").intValue();
    media.title = node.get("title").textValue();
    media.uri = node.get("uri").textValue();
    media.width = node.get("width").intValue();
    return media;
}
Also used : Media(data.media.Media) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 2 with ArrayNode

use of com.fasterxml.jackson.databind.node.ArrayNode in project jvm-serializers by eishay.

the class JacksonJsonTree method addMedia.

protected static ObjectNode addMedia(Media media, ObjectNode node) {
    if (media.hasBitrate) {
        node.put("bitrate", media.bitrate);
    }
    node.put("copyright", media.copyright);
    node.put("duration", media.duration);
    node.put("format", media.format);
    node.put("height", media.height);
    ArrayNode persons = node.arrayNode();
    for (String person : media.persons) {
        persons.add(person);
    }
    node.set("persons", persons);
    node.put("player", media.player.name());
    node.put("size", media.size);
    node.put("title", media.title);
    node.put("uri", media.uri);
    node.put("width", media.width);
    return node;
}
Also used : ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 3 with ArrayNode

use of com.fasterxml.jackson.databind.node.ArrayNode in project jvm-serializers by eishay.

the class JacksonJsonTree method readMediaContent.

protected static MediaContent readMediaContent(JsonNode root) throws IOException {
    MediaContent mediaContent = new MediaContent();
    mediaContent.media = readMedia(root.get("media"));
    mediaContent.images = readImages((ArrayNode) root.get("images"));
    return mediaContent;
}
Also used : MediaContent(data.media.MediaContent) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 4 with ArrayNode

use of com.fasterxml.jackson.databind.node.ArrayNode in project jsonschema2pojo by joelittlejohn.

the class DefaultRule method getDefaultSet.

/**
     * Creates a default value for a set property by:
     * <ol>
     * <li>Creating a new {@link LinkedHashSet} with the correct generic type
     * <li>Using {@link Arrays#asList(Object...)} to initialize the set with the
     * correct default values
     * </ol>
     *
     * @param fieldType
     *            the java type that applies for this field ({@link Set} with
     *            some generic type argument)
     * @param node
     *            the node containing default values for this set
     * @return an expression that creates a default value that can be assigned
     *         to this field
     */
private JExpression getDefaultSet(JType fieldType, JsonNode node) {
    JClass setGenericType = ((JClass) fieldType).getTypeParameters().get(0);
    JClass setImplClass = fieldType.owner().ref(LinkedHashSet.class);
    setImplClass = setImplClass.narrow(setGenericType);
    JInvocation newSetImpl = JExpr._new(setImplClass);
    if (node instanceof ArrayNode && node.size() > 0) {
        JInvocation invokeAsList = fieldType.owner().ref(Arrays.class).staticInvoke("asList");
        for (JsonNode defaultValue : node) {
            invokeAsList.arg(getDefaultValue(setGenericType, defaultValue));
        }
        newSetImpl.arg(invokeAsList);
    } else if (!ruleFactory.getGenerationConfig().isInitializeCollections()) {
        return JExpr._null();
    }
    return newSetImpl;
}
Also used : JClass(com.sun.codemodel.JClass) JInvocation(com.sun.codemodel.JInvocation) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Arrays(java.util.Arrays)

Example 5 with ArrayNode

use of com.fasterxml.jackson.databind.node.ArrayNode in project jsonschema2pojo by joelittlejohn.

the class DefaultRule method getDefaultList.

/**
     * Creates a default value for a list property by:
     * <ol>
     * <li>Creating a new {@link ArrayList} with the correct generic type
     * <li>Using {@link Arrays#asList(Object...)} to initialize the list with
     * the correct default values
     * </ol>
     *
     * @param fieldType
     *            the java type that applies for this field ({@link List} with
     *            some generic type argument)
     * @param node
     *            the node containing default values for this list
     * @return an expression that creates a default value that can be assigned
     *         to this field
     */
private JExpression getDefaultList(JType fieldType, JsonNode node) {
    JClass listGenericType = ((JClass) fieldType).getTypeParameters().get(0);
    JClass listImplClass = fieldType.owner().ref(ArrayList.class);
    listImplClass = listImplClass.narrow(listGenericType);
    JInvocation newListImpl = JExpr._new(listImplClass);
    if (node instanceof ArrayNode && node.size() > 0) {
        JInvocation invokeAsList = fieldType.owner().ref(Arrays.class).staticInvoke("asList");
        for (JsonNode defaultValue : node) {
            invokeAsList.arg(getDefaultValue(listGenericType, defaultValue));
        }
        newListImpl.arg(invokeAsList);
    } else if (!ruleFactory.getGenerationConfig().isInitializeCollections()) {
        return JExpr._null();
    }
    return newListImpl;
}
Also used : JClass(com.sun.codemodel.JClass) JInvocation(com.sun.codemodel.JInvocation) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Arrays(java.util.Arrays)

Aggregations

ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)711 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)383 JsonNode (com.fasterxml.jackson.databind.JsonNode)296 Test (org.junit.Test)113 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)90 ArrayList (java.util.ArrayList)67 IOException (java.io.IOException)62 HashMap (java.util.HashMap)50 Map (java.util.Map)50 TextNode (com.fasterxml.jackson.databind.node.TextNode)30 DefaultSerializerProvider (com.fasterxml.jackson.databind.ser.DefaultSerializerProvider)30 Deployment (org.activiti.engine.test.Deployment)23 List (java.util.List)22 HashSet (java.util.HashSet)21 ArrayNode (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode)20 StringEntity (org.apache.http.entity.StringEntity)20 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)19 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)19 Cluster (org.apache.geode.tools.pulse.internal.data.Cluster)18 HttpPost (org.apache.http.client.methods.HttpPost)16