/** * Method called to create a new context for iterating all * contents of the current structured value (JSON array or object) */ public final NodeCursor iterateChildren() { JsonNode n = currentNode(); if (n == null) throw new IllegalStateException("No current node"); if (n.isArray()) { // false since we have already returned START_ARRAY return new Array(n, this); } if (n.isObject()) { return new Object(n, this); } throw new IllegalStateException("Current node of type "+n.getClass().getName()); }
static Set<String> parseAliases(JsonNode node) { JsonNode aliasesNode = node.get("aliases"); if (aliasesNode == null) return null; if (!aliasesNode.isArray()) throw new SchemaParseException("aliases not an array: "+node); Set<String> aliases = new LinkedHashSet<String>(); for (JsonNode aliasNode : aliasesNode) { if (!aliasNode.isTextual()) throw new SchemaParseException("alias not a string: "+aliasNode); aliases.add(aliasNode.getTextValue()); } return aliases; }
private void parseTypes(JsonNode json) { JsonNode defs = json.get("types"); if (defs == null) return; // no types defined if (!defs.isArray()) throw new SchemaParseException("Types not an array: "+defs); for (JsonNode type : defs) { if (!type.isObject()) throw new SchemaParseException("Type not an object: "+type); Schema.parse(type, types); } }
public TreeTraversingParser(JsonNode n, ObjectCodec codec) { super(0); _objectCodec = codec; if (n.isArray()) { _nextToken = JsonToken.START_ARRAY; _nodeCursor = new NodeCursor.Array(n, null); } else if (n.isObject()) { _nextToken = JsonToken.START_OBJECT; _nodeCursor = new NodeCursor.Object(n, null); } else { // value node _nodeCursor = new NodeCursor.RootValue(n, null); } }
/** * Removes UUIDs from a json tree. * * @param json json tree node */ public static void removeUuids(JsonNode json) { boolean containerWithId = json.isContainerNode() && json.has(UUID_FIELD); boolean isArray = json.isArray(); boolean childIsNotArray = !(json.size() == 1 && json.getElements().next().isArray()); if (containerWithId && !isArray && childIsNotArray) { ((ObjectNode) json).remove(UUID_FIELD); } for (JsonNode node : json) { if (node.isContainerNode()) { removeUuids(node); } } } }
private static void assertListEquals( String what, List<String> expected, JsonNode jsonNode ) { assertTrue( what + " - should be a list", jsonNode.isArray() ); List<String> actual = new ArrayList<>( jsonNode.size() ); for ( JsonNode node : jsonNode ) { actual.add( node.getTextValue() ); } assertEquals( what, expected, actual ); }
@Override protected boolean matchesSafely( HTTP.Response response ) { try { Iterator<JsonNode> meta = getJsonNodeWithName( response, "meta" ).iterator(); for ( int metaIndex = 0; meta.hasNext(); metaIndex++ ) { JsonNode node = meta.next(); if ( metaIndex == index ) { assertTrue( node.isArray() ); } } return true; } catch ( JsonParseException e ) { return false; } }
/** * Parses the given string CTL schema along with its dependencies as an * {@link org.apache.avro.Schema Avro schema}. * * @param avroSchema A string CTL schema to parse * @return A parsed CTL schema as an Avro schema * @throws Exception - if the given CTL schema is invalid and thus cannot be parsed. */ public static Schema parseStringCtlSchema(String avroSchema) throws Exception { Schema.Parser parser = new Schema.Parser(); ObjectMapper mapper = new ObjectMapper(); JsonNode node = mapper.readTree(avroSchema); JsonNode dependenciesNode = node.get(DEPENDENCIES); if (dependenciesNode != null && dependenciesNode.isArray()) { Map<String, Schema> types = new HashMap<>(); for (int i = 0; i < dependenciesNode.size(); i++) { JsonNode dependencyNode = dependenciesNode.get(i); Fqn fqn = new Fqn(dependencyNode.get(FQN).asText()); Schema fakeSchema = SchemaBuilder .record(fqn.getName()).namespace(fqn.getNamespace()) .fields() .endRecord(); types.put(fqn.getFqnString(), fakeSchema); } parser.addTypes(types); } return parser.parse(avroSchema); }
} else if (node.isArray()) { final ArrayList<Object> array = new ArrayList<>(); final Iterator<JsonNode> iter = node.getElements();
return defaultValue.isNull(); case ARRAY: if (!defaultValue.isArray()) return false; for (JsonNode element : defaultValue)
} else if (jsonNode.isArray()) { List l = new ArrayList(); for (JsonNode node : jsonNode) {
assertTrue( "data is a list", data.isArray() ); assertEquals( "one entry", initialData + 1, data.size() ); JsonNode entry = data.get( 0 ); JsonNode nodes = entry.get( "graph" ).get( "nodes" ); JsonNode rels = entry.get( "graph" ).get( "relationships" ); assertTrue( "nodes is a list", nodes.isArray() ); assertTrue( "relationships is a list", rels.isArray() ); assertEquals( "one node", 1, nodes.size() ); assertEquals( "no relationships", 0, rels.size() );
if (!object.has(DEPENDENCIES)) { schema.setDependencySet(dependencies); } else if (!object.get(DEPENDENCIES).isArray()) { throw new IllegalArgumentException("Illegal dependencies format!"); } else {
@Test public void testJsonEmptyRPFormatter() throws Exception { WMFullResourcePlan fullRp = createRP("test_rp_1", null, null); formatter.showFullResourcePlan(out, fullRp); out.flush(); ObjectMapper objectMapper = new ObjectMapper(); JsonNode jsonTree = objectMapper.readTree(bos.toByteArray()); assertNotNull(jsonTree); assertTrue(jsonTree.isObject()); assertEquals("test_rp_1", jsonTree.get("name").asText()); assertTrue(jsonTree.get("parallelism").isNull()); assertTrue(jsonTree.get("defaultPool").isNull()); assertTrue(jsonTree.get("pools").isArray()); assertEquals(0, jsonTree.get("pools").size()); }
if (fieldNode.isArray()) { final ArrayNode arrayNode = (ArrayNode) fieldNode; final int numElements = arrayNode.size();
assertTrue( "Expected to find a JSON node, but there was none", meta.hasNext() ); JsonNode node = meta.next(); assertTrue( "Expected the node to be a list (for a path)", node.isArray() ); for ( JsonNode inner : node )
if (fieldNode.isArray()) { final ArrayNode arrayNode = (ArrayNode) fieldNode; final int numElements = arrayNode.size();
assertEquals(10, jsonTree.get("parallelism").asInt()); assertEquals("def", jsonTree.get("defaultPool").asText()); assertTrue(jsonTree.get("pools").isArray()); assertEquals(2, jsonTree.get("pools").size()); assertEquals(7, pool2.get("parallelism").asInt()); assertEquals(0.7, pool2.get("allocFraction").asDouble(), 0.00001); assertTrue(pool2.get("triggers").isArray()); assertEquals(0, pool2.get("triggers").size()); assertTrue(pool2.get("mappings").isArray()); JsonNode type0 = pool2.get("mappings").get(0); assertEquals("user", type0.get("type").asText()); assertTrue(type0.get("values").isArray()); assertEquals(2, type0.get("values").size()); HashSet<String> vals = new HashSet<>(); assertEquals(3, pool1.get("parallelism").asInt()); assertEquals(0.3, pool1.get("allocFraction").asDouble(), 0.00001); assertTrue(pool1.get("triggers").isArray()); assertEquals(1, pool1.get("triggers").size());
if (byDefault.isArray() && AvroUtils.getSchemaByType(schemaNode, Type.BYTES) != null) {