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());
}
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));
}
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());
}
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());
}
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;
}
Aggregations