if (node.get(key) != null && node.get(key).isArray()) { final ArrayNode arrayNode = (ArrayNode) obj.get(key); arrayNode.removeAll(); Arrays.stream(ESCAPED_COMMA_SPLIT_PATTERN.split(value)) .map(String::trim)
private static void merge(ObjectNode to, ObjectNode from) { Iterator<String> newFieldNames = from.fieldNames(); while (newFieldNames.hasNext()) { String newFieldName = newFieldNames.next(); JsonNode oldVal = to.get(newFieldName); JsonNode newVal = from.get(newFieldName); if (oldVal == null || oldVal.isNull()) { to.set(newFieldName, newVal); } else if (oldVal.isArray() && newVal.isArray()) { ((ArrayNode) oldVal).removeAll(); ((ArrayNode) oldVal).addAll((ArrayNode) newVal); } else if (oldVal.isObject() && newVal.isObject()) { merge((ObjectNode) oldVal, (ObjectNode) newVal); } else if (!(newVal == null || newVal.isNull())) { to.set(newFieldName, newVal); } } } }
public static void sort(ArrayNode array, Comparator<JsonNode> comparator) { Preconditions.checkNotNull(array); Preconditions.checkNotNull(comparator); List<JsonNode> arr = Lists.newArrayList(); for (JsonNode n : array) { arr.add(n); } Collections.sort(arr, comparator); array.removeAll(); for (JsonNode n : arr) { array.add(n); } }
private static void merge(ObjectNode to, ObjectNode from) { Iterator<String> newFieldNames = from.fieldNames(); while (newFieldNames.hasNext()) { String newFieldName = newFieldNames.next(); JsonNode oldVal = to.get(newFieldName); JsonNode newVal = from.get(newFieldName); if (oldVal == null || oldVal.isNull()) { to.set(newFieldName, newVal); } else if (oldVal.isArray() && newVal.isArray()) { ((ArrayNode) oldVal).removeAll(); ((ArrayNode) oldVal).addAll((ArrayNode) newVal); } else if (oldVal.isObject() && newVal.isObject()) { merge((ObjectNode) oldVal, (ObjectNode) newVal); } else if (!(newVal == null || newVal.isNull())) { to.set(newFieldName, newVal); } } } }
public void sort(MenuItem m) { List<MenuItem> tmp = getItems(); Collections.sort(tmp, comparator); ArrayNode n = (ArrayNode) m.getModel().get("items"); n.removeAll(); for (MenuItem t : tmp) { n.add(t.getModel()); } tmp.stream().forEach(it -> { it.sort(); }); } }
public class Tags { public static void main(String[] args) throws Exception { InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("tags.json"); ObjectMapper om = new ObjectMapper(); JsonNode node = om.readTree(in); recursiveFind(node); System.out.println(node); //Prints {"test1":{"get":{"tags":["my rest calls"]}},"test2":{"put":{"tags":["my rest calls"]}}} } private static void recursiveFind(JsonNode node) { if (!node.isObject()) { return; } JsonNode tags = node.get("tags"); if (tags != null) { ArrayNode arry = (ArrayNode) tags; arry.removeAll(); arry.add("my rest calls"); } Iterator<JsonNode> it = node.elements(); while (it.hasNext()) { JsonNode jsonNode = it.next(); recursiveFind(jsonNode); } } }
private void processEndObject() throws IOException { Level child; Level parent; child = stack.pop(); if( bufferingLevel == child ) { filterBufferedNode( child ); mapper.writeTree( generator, child.node ); bufferingLevel = null; bufferingConfig = null; } else if( bufferingLevel == null ) { generator.writeEndObject(); if( !stack.isEmpty() ) { parent = stack.peek(); switch( parent.node.asToken() ) { case START_ARRAY: ((ArrayNode)parent.node ).removeAll(); break; case START_OBJECT: ((ObjectNode)parent.node ).removeAll(); break; default: throw new IllegalStateException(); } } } }
while (accessNodes.hasNext()) { ArrayNode child = (ArrayNode) accessNodes.next(); child.removeAll(); child.add("anyone");
private void processEndObject() throws IOException { Level child; Level parent; child = stack.pop(); if( child.equals(bufferingLevel) ) { filterBufferedNode( child ); mapper.writeTree( generator, child.node ); bufferingLevel = null; bufferingConfig = null; } else if( bufferingLevel == null ) { generator.writeEndObject(); if( !stack.isEmpty() ) { parent = stack.peek(); switch( parent.node.asToken() ) { case START_ARRAY: ((ArrayNode)parent.node ).removeAll(); break; case START_OBJECT: ((ObjectNode)parent.node ).removeAll(); break; default: throw new IllegalStateException(); } } } }
private void processValueNull() throws IOException { Level child; if(stack.isEmpty()) { generator.writeNull(); return; } Level parent = stack.peek(); if( parent.isArray() ) { ((ArrayNode)parent.node ).addNull(); //dump(); if( bufferingLevel == null ) { ((ArrayNode)parent.node ).removeAll(); } } else { child = stack.pop(); parent = stack.peek(); ((ObjectNode)parent.node ).putNull( child.field ); //dump(); if( bufferingLevel == null ) { ((ObjectNode)parent.node ).remove( child.field ); } } if( bufferingLevel == null ) { generator.writeNull(); } }
private void processEndArray() throws IOException { Level child; Level parent; child = stack.pop(); if( bufferingLevel == child ) { filterBufferedNode( child ); mapper.writeTree( generator, child.node ); bufferingLevel = null; bufferingConfig = null; } else if( bufferingLevel == null ) { generator.writeEndArray(); if( !stack.isEmpty() ) { parent = stack.peek(); switch( parent.node.asToken() ) { case START_ARRAY: ((ArrayNode)parent.node ).removeAll(); break; case START_OBJECT: ((ObjectNode)parent.node ).removeAll(); break; default: throw new IllegalStateException(); } } } }
private void processValueNull() throws IOException { Level child; if(stack.isEmpty()) { generator.writeNull(); return; } Level parent = stack.peek(); if( parent.isArray() ) { ((ArrayNode)parent.node ).addNull(); //dump(); if( bufferingLevel == null ) { ((ArrayNode)parent.node ).removeAll(); } } else { child = stack.pop(); parent = stack.peek(); ((ObjectNode)parent.node ).putNull( child.field ); //dump(); if( bufferingLevel == null ) { ((ObjectNode)parent.node ).remove( child.field ); } } if( bufferingLevel == null ) { generator.writeNull(); } }
private void processEndArray() throws IOException { Level child; Level parent; child = stack.pop(); if( child.equals(bufferingLevel) ) { filterBufferedNode( child ); mapper.writeTree( generator, child.node ); bufferingLevel = null; bufferingConfig = null; } else if( bufferingLevel == null ) { generator.writeEndArray(); if( !stack.isEmpty() ) { parent = stack.peek(); switch( parent.node.asToken() ) { case START_ARRAY: ((ArrayNode)parent.node ).removeAll(); break; case START_OBJECT: ((ObjectNode)parent.node ).removeAll(); break; default: throw new IllegalStateException(); } } } }
/** * Apply a migrator to all elements of the array at the current pointer * * <p>Note that if the migrator fails to apply to at least one element, the * original array is left untouched; its elements are replaced if and only * if the migrator applies successfully to <strong>all</strong> elements. * </p> * * @param migrator the migrator to apply * @throws SwaggerMigrationException current node is not a JSON Array, or * migrator failed to apply to at least one array element */ public void applyMigratorToElements(final SwaggerMigrator migrator) throws SwaggerMigrationException { if (!currentNode.isArray()) { throw new SwaggerMigrationException(); } final ArrayNode array = (ArrayNode) currentNode; final ArrayNode transformed = array.arrayNode(); for (final JsonNode element : array) { transformed.add(migrator.migrate(element)); } array.removeAll().addAll(transformed); }
operationNode.remove("parameters"); } else { parameters.removeAll(); parameters.addAll(parametersList);
private void processValueBoolean() throws IOException { Level child; Level parent; if(stack.isEmpty()) { generator.writeBoolean(parser.getBooleanValue()); return; } parent = stack.peek(); if( parent.isArray() ) { ((ArrayNode)parent.node ).add( parser.getBooleanValue() ); //dump(); if( bufferingLevel == null ) { ((ArrayNode)parent.node ).removeAll(); } } else { child = stack.pop(); parent = stack.peek(); ((ObjectNode)parent.node ).put( child.field, parser.getBooleanValue() ); //dump(); if( bufferingLevel == null ) { ((ObjectNode)parent.node ).remove( child.field ); } } if( bufferingLevel == null ) { generator.writeBoolean( parser.getBooleanValue() ); } }
private void processValueBoolean() throws IOException { Level child; Level parent; if(stack.isEmpty()) { generator.writeBoolean(parser.getBooleanValue()); return; } parent = stack.peek(); if( parent.isArray() ) { ((ArrayNode)parent.node ).add( parser.getBooleanValue() ); //dump(); if( bufferingLevel == null ) { ((ArrayNode)parent.node ).removeAll(); } } else { child = stack.pop(); parent = stack.peek(); ((ObjectNode)parent.node ).put( child.field, parser.getBooleanValue() ); //dump(); if( bufferingLevel == null ) { ((ObjectNode)parent.node ).remove( child.field ); } } if( bufferingLevel == null ) { generator.writeBoolean( parser.getBooleanValue() ); } }