Search in sources :

Example 16 with ArrayNode

use of 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 17 with ArrayNode

use of 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 18 with ArrayNode

use of 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)

Example 19 with ArrayNode

use of com.fasterxml.jackson.databind.node.ArrayNode in project jackson-databind by FasterXML.

the class ArrayNodeTest method testArrayViaMapper.

public void testArrayViaMapper() throws Exception {
    final String JSON = "[[[-0.027512,51.503221],[-0.008497,51.503221],[-0.008497,51.509744],[-0.027512,51.509744]]]";
    JsonNode n = objectMapper().readTree(JSON);
    assertNotNull(n);
    assertTrue(n.isArray());
    ArrayNode an = (ArrayNode) n;
    assertEquals(1, an.size());
    ArrayNode an2 = (ArrayNode) n.get(0);
    assertTrue(an2.isArray());
    assertEquals(4, an2.size());
}
Also used : ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 20 with ArrayNode

use of com.fasterxml.jackson.databind.node.ArrayNode in project series-rest-api by 52North.

the class GeoJSONEncoder method encode.

protected ObjectNode encode(MultiPolygon geometry, int parentSrid) {
    ObjectNode json = jsonFactory.objectNode();
    ArrayNode list = json.put(JSONConstants.TYPE, JSONConstants.MULTI_POLYGON).putArray(JSONConstants.COORDINATES);
    for (int i = 0; i < geometry.getNumGeometries(); ++i) {
        list.add(encodeCoordinates((Polygon) geometry.getGeometryN(i)));
    }
    encodeCRS(json, geometry, parentSrid);
    return json;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) Polygon(com.vividsolutions.jts.geom.Polygon) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint)

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