Search in sources :

Example 6 with ArrayNode

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode in project jackson-databind by FasterXML.

the class ObjectReaderTest method testTreeToValue.

/*
    /**********************************************************
    /* Test methods, ObjectCodec
    /**********************************************************
     */
public void testTreeToValue() throws Exception {
    ArrayNode n = MAPPER.createArrayNode();
    n.add("xyz");
    ObjectReader r = MAPPER.readerFor(String.class);
    List<?> list = r.treeToValue(n, List.class);
    assertEquals(1, list.size());
}
Also used : ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 7 with ArrayNode

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode in project jackson-databind by FasterXML.

the class NodeMergeTest method testObjectDeepUpdate.

public void testObjectDeepUpdate() throws Exception {
    ObjectNode base = MAPPER.createObjectNode();
    ObjectNode props = base.putObject("props");
    props.put("base", 123);
    props.put("value", 456);
    ArrayNode a = props.putArray("array");
    a.add(true);
    base.putNull("misc");
    assertSame(base, MAPPER.readerForUpdating(base).readValue(aposToQuotes("{'props':{'value':true, 'extra':25.5, 'array' : [ 3 ]}}")));
    assertEquals(2, base.size());
    ObjectNode resultProps = (ObjectNode) base.get("props");
    assertEquals(4, resultProps.size());
    assertEquals(123, resultProps.path("base").asInt());
    assertTrue(resultProps.path("value").asBoolean());
    assertEquals(25.5, resultProps.path("extra").asDouble());
    JsonNode n = resultProps.get("array");
    assertEquals(ArrayNode.class, n.getClass());
    assertEquals(2, n.size());
    assertEquals(3, n.get(1).asInt());
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 8 with ArrayNode

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode in project jackson-databind by FasterXML.

the class ArrayNodeTest method testDirectCreation2.

public void testDirectCreation2() throws IOException {
    JsonNodeFactory f = objectMapper().getNodeFactory();
    ArrayList<JsonNode> list = new ArrayList<>();
    list.add(f.booleanNode(true));
    list.add(f.textNode("foo"));
    ArrayNode n = new ArrayNode(f, list);
    assertEquals(2, n.size());
    assertTrue(n.get(0).isBoolean());
    assertTrue(n.get(1).isTextual());
    // also, should fail with invalid set attempt
    try {
        n.set(2, f.nullNode());
        fail("Should not pass");
    } catch (IndexOutOfBoundsException e) {
        verifyException(e, "illegal index");
    }
    n.insert(1, (String) null);
    assertEquals(3, n.size());
    assertTrue(n.get(0).isBoolean());
    assertTrue(n.get(1).isNull());
    assertTrue(n.get(2).isTextual());
    n.removeAll();
    n.insert(0, (JsonNode) null);
    assertEquals(1, n.size());
    assertTrue(n.get(0).isNull());
}
Also used : ArrayList(java.util.ArrayList) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory)

Example 9 with ArrayNode

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode in project jackson-databind by FasterXML.

the class ArrayNodeTest method testArrayNodeEquality.

public void testArrayNodeEquality() {
    ArrayNode n1 = new ArrayNode(null);
    ArrayNode n2 = new ArrayNode(null);
    assertTrue(n1.equals(n2));
    assertTrue(n2.equals(n1));
    n1.add(TextNode.valueOf("Test"));
    assertFalse(n1.equals(n2));
    assertFalse(n2.equals(n1));
    n2.add(TextNode.valueOf("Test"));
    assertTrue(n1.equals(n2));
    assertTrue(n2.equals(n1));
}
Also used : ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 10 with ArrayNode

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode in project jackson-databind by FasterXML.

the class ArrayNodeTest method testDirectCreation.

public void testDirectCreation() throws IOException {
    ArrayNode n = new ArrayNode(JsonNodeFactory.instance);
    assertStandardEquals(n);
    assertFalse(n.elements().hasNext());
    assertFalse(n.fieldNames().hasNext());
    TextNode text = TextNode.valueOf("x");
    n.add(text);
    assertEquals(1, n.size());
    assertFalse(0 == n.hashCode());
    assertTrue(n.elements().hasNext());
    // no field names for arrays
    assertFalse(n.fieldNames().hasNext());
    // not used with arrays
    assertNull(n.get("x"));
    assertTrue(n.path("x").isMissingNode());
    assertSame(text, n.get(0));
    // single element, so:
    assertFalse(n.has("field"));
    assertFalse(n.hasNonNull("field"));
    assertTrue(n.has(0));
    assertTrue(n.hasNonNull(0));
    assertFalse(n.has(1));
    assertFalse(n.hasNonNull(1));
    // add null node too
    n.add((JsonNode) null);
    assertEquals(2, n.size());
    assertTrue(n.get(1).isNull());
    assertTrue(n.has(1));
    assertFalse(n.hasNonNull(1));
    // change to text
    n.set(1, text);
    assertSame(text, n.get(1));
    n.set(0, null);
    assertTrue(n.get(0).isNull());
    // and finally, clear it all
    ArrayNode n2 = new ArrayNode(JsonNodeFactory.instance);
    n2.add("foobar");
    assertFalse(n.equals(n2));
    n.addAll(n2);
    assertEquals(3, n.size());
    assertFalse(n.get(0).isTextual());
    assertNotNull(n.remove(0));
    assertEquals(2, n.size());
    assertTrue(n.get(0).isTextual());
    assertNull(n.remove(-1));
    assertNull(n.remove(100));
    assertEquals(2, n.size());
    ArrayList<JsonNode> nodes = new ArrayList<JsonNode>();
    nodes.add(text);
    n.addAll(nodes);
    assertEquals(3, n.size());
    assertNull(n.get(10000));
    assertNull(n.remove(-4));
    TextNode text2 = TextNode.valueOf("b");
    n.insert(0, text2);
    assertEquals(4, n.size());
    assertSame(text2, n.get(0));
    assertNotNull(n.addArray());
    assertEquals(5, n.size());
    n.addPOJO("foo");
    assertEquals(6, n.size());
    // Try serializing it for fun, too...
    JsonGenerator g = objectMapper().getFactory().createGenerator(new StringWriter());
    n.serialize(g, null);
    g.close();
    n.removeAll();
    assertEquals(0, n.size());
}
Also used : ArrayList(java.util.ArrayList) TextNode(com.fasterxml.jackson.databind.node.TextNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Aggregations

ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)979 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)542 JsonNode (com.fasterxml.jackson.databind.JsonNode)402 Test (org.junit.Test)140 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)125 ArrayList (java.util.ArrayList)110 IOException (java.io.IOException)93 Map (java.util.Map)74 HashMap (java.util.HashMap)68 List (java.util.List)50 TextNode (com.fasterxml.jackson.databind.node.TextNode)38 Test (org.testng.annotations.Test)35 DefaultSerializerProvider (com.fasterxml.jackson.databind.ser.DefaultSerializerProvider)30 HashSet (java.util.HashSet)30 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)25 Deployment (org.activiti.engine.test.Deployment)23 File (java.io.File)22 InputStream (java.io.InputStream)20 Iterator (java.util.Iterator)20 StringEntity (org.apache.http.entity.StringEntity)20