@Override public boolean isEmpty() { return !data.fieldNames().hasNext(); }
@Override public List<String> getAttributeNames() { return ImmutableList.copyOf(data.fieldNames()); }
@Override public Collection<String> getPropertyKeys(Object obj) { List<String> keys = new ArrayList<String>(); Iterator<String> iter = toJsonObject(obj).fieldNames(); while (iter.hasNext()){ keys.add(iter.next()); } return keys; }
protected JsonNode mergeObjects(JsonNode target, JsonNode source) { ObjectNode targetObject = (ObjectNode) target; ObjectNode srcObject = (ObjectNode) source; Iterator<String> fieldNames = srcObject.fieldNames(); while (fieldNames.hasNext()) { String fieldName = fieldNames.next(); JsonNode srcChild = srcObject.get(fieldName); JsonNode targetChild = targetObject.get(fieldName); targetObject.replace(fieldName, apply(targetChild, srcChild)); } return target; }
private ObjectNode mergeObjectNodes(ObjectNode targetNode, ObjectNode updateNode) { Iterator<String> fieldNames = updateNode.fieldNames(); while (fieldNames.hasNext()) { String fieldName = fieldNames.next(); JsonNode targetValue = targetNode.get(fieldName); JsonNode updateValue = updateNode.get(fieldName); if (targetValue == null) { // Target node doesn't have this field from update node: just add it targetNode.set(fieldName, updateValue); } else { // Both nodes have the same field: merge the values if (targetValue.isObject() && updateValue.isObject()) { // Both values are objects: recurse targetNode.set(fieldName, mergeObjectNodes((ObjectNode) targetValue, (ObjectNode) updateValue)); } else if (targetValue.isArray() && updateValue.isArray()) { // Both values are arrays: concatenate them to be merged later ((ArrayNode) targetValue).addAll((ArrayNode) updateValue); } else { // Values have different types: use the one from the update node targetNode.set(fieldName, updateValue); } } } return targetNode; }
@Override public Paths deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { Paths result = new Paths(); JsonNode node = jp.getCodec().readTree(jp); ObjectNode objectNode = (ObjectNode)node; Map<String, Object> extensions = new LinkedHashMap<>(); for (Iterator<String> it = objectNode.fieldNames(); it.hasNext(); ) { String childName = it.next(); JsonNode child = objectNode.get(childName); // if name start with `x-` consider it an extesion if (childName.startsWith("x-")) { extensions.put(childName, Json.mapper().convertValue(child, Object.class)); } else { result.put(childName, Json.mapper().convertValue(child, PathItem.class)); } } if (!extensions.isEmpty()) { result.setExtensions(extensions); } return result; } }
@Override public ApiResponses deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { ApiResponses result = new ApiResponses(); JsonNode node = jp.getCodec().readTree(jp); ObjectNode objectNode = (ObjectNode)node; Map<String, Object> extensions = new LinkedHashMap<>(); for (Iterator<String> it = objectNode.fieldNames(); it.hasNext(); ) { String childName = it.next(); JsonNode child = objectNode.get(childName); // if name start with `x-` consider it an extesion if (childName.startsWith("x-")) { extensions.put(childName, Json.mapper().convertValue(child, Object.class)); } else { result.put(childName, Json.mapper().convertValue(child, ApiResponse.class)); } } if (!extensions.isEmpty()) { result.setExtensions(extensions); } return result; } }
@Override public Callback deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { Callback result = new Callback(); JsonNode node = jp.getCodec().readTree(jp); ObjectNode objectNode = (ObjectNode)node; Map<String, Object> extensions = new LinkedHashMap<>(); for (Iterator<String> it = objectNode.fieldNames(); it.hasNext(); ) { String childName = it.next(); JsonNode child = objectNode.get(childName); // if name start with `x-` consider it an extesion if (childName.startsWith("x-")) { extensions.put(childName, Json.mapper().convertValue(child, Object.class)); } else if (childName.equals("$ref")) { result.$ref(child.asText()); } else { result.put(childName, Json.mapper().convertValue(child, PathItem.class)); } } if (!extensions.isEmpty()) { result.setExtensions(extensions); } return result; } }
obj.fieldNames().forEachRemaining(m -> { if (!excludes.contains(m)) { modified.set(m, obj.get(m));
@Override public Collection<String> getPropertyKeys(Object obj) { List<String> keys = new ArrayList<String>(); Iterator<String> iter = toJsonObject(obj).fieldNames(); while (iter.hasNext()){ keys.add(iter.next()); } return keys; }
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); } } } }
private void parseDefines(Query query, ObjectNode jsonNode) throws QueryException { Iterator<String> fieldNames = jsonNode.fieldNames(); // First pass, get all the name and create stub includes, using two passing to allow the usage of includes defined later in the structure while (fieldNames.hasNext()) { String fieldName = fieldNames.next(); JsonNode defineNode = jsonNode.get(fieldName); if (defineNode instanceof ObjectNode) { Include include = new Include(packageMetaData); query.addDefine(fieldName, include); } else { throw new QueryException("\"defines\"[" + fieldName + "] must be of type object"); } } // Second pass, actually construct the includes fieldNames = jsonNode.fieldNames(); while (fieldNames.hasNext()) { String fieldName = fieldNames.next(); JsonNode defineNode = jsonNode.get(fieldName); ObjectNode define = (ObjectNode)defineNode; parseInclude(query, define, query.getDefine(fieldName), null); } }
if (!objectNode.fieldNames().hasNext()) { return; if (!objectNode.fieldNames().hasNext()) { i.remove();
Iterator<String> fieldNames = objectNode.fieldNames(); while (fieldNames.hasNext()) { String fieldName = fieldNames.next();
Iterator<String> fieldNames = jsonObject.fieldNames(); int nrFields = 0; while (fieldNames.hasNext()) {
public Set<String> getKeys(ObjectNode node) { Set<String> keys = new LinkedHashSet<>(); if (node == null) { return keys; } Iterator<String> it = node.fieldNames(); while (it.hasNext()) { keys.add(it.next()); } return keys; }
@Override public Set<String> names() { Iterator<String> fieldNames = objectNode.fieldNames(); return CollectionUtils.iteratorToSet(fieldNames); }
private void setAllMissingFields(ObjectNode config, ObjectNode defaults) { for (String fieldName : forEach(defaults.fieldNames())) if (! config.has(fieldName) || config.get(fieldName).isNull()) config.put(fieldName, defaults.get(fieldName)); }
private static Map<String, ArrayProperty> propertyFrom(final String json) { try { final ObjectNode object = (ObjectNode) MAPPER.readTree(json); final String propertyName = object.fieldNames().next(); final JsonNode node = object.elements().next(); final ArrayProperty array = MAPPER.readerFor(Property.class).readValue(node); return Collections.singletonMap(propertyName, array); } catch (final IOException e) { throw new AssertionError("Unable to deserialize given parameter", e); } }
private static Map<String, ArrayProperty> propertyFrom(final String json) { try { final ObjectNode object = (ObjectNode) MAPPER.readTree(json); final String propertyName = object.fieldNames().next(); final JsonNode node = object.elements().next(); final ArrayProperty array = MAPPER.readerFor(Property.class).readValue(node); return Collections.singletonMap(propertyName, array); } catch (final IOException e) { throw new AssertionError("Unable to deserialize given parameter", e); } }