private void flatten(String prefix, JsonNode node, List<PathAndValue> entityData) { if (node.size() == 0) { return; } JsonNodeCursor cursor = new JsonNodeCursor(Path.EMPTY, node); while(cursor.next()) { String p=cursor.getCurrentPath().toString(); JsonNode value=cursor.getCurrentNode(); if(value.isValueNode()) { String path = prefix.isEmpty() ? p : (prefix + "." + p); PathAndValue data = new PathAndValue(path, value.asText(null)); // TODO(ahenning): Consider using Set instead of List for entityData if (!entityData.contains(data)) { entityData.add(data); } } } }
/** * @param cursor The cursor, pointing to the first element of the array */ @SuppressWarnings({"rawtypes", "unchecked"}) private List arrayToBson(JsonNodeCursor cursor, ArrayElement el, EntityMetadata md,ResultMetadata rmd) { List l = new ArrayList(); if (el instanceof SimpleArrayElement) { Type t = el.getType(); do { Object value = toValue(t, cursor.getCurrentNode()); l.add(value); } while (cursor.nextSibling()); } else { do { JsonNode node = cursor.getCurrentNode(); if (node == null || node instanceof NullNode) { l.add(null); } else if (cursor.firstChild()) { l.add(objectToBson(cursor, md,rmd)); cursor.parent(); } else { l.add(null); } } while (cursor.nextSibling()); } return l; }
/** * @param cursor The cursor, pointing to the first element of the array */ @SuppressWarnings({"rawtypes", "unchecked"}) private List arrayToBson(JsonNodeCursor cursor, ArrayElement el, EntityMetadata md) { List l = new ArrayList(); if (el instanceof SimpleArrayElement) { Type t = el.getType(); do { Object value = toValue(t, cursor.getCurrentNode()); l.add(value); } while (cursor.nextSibling()); } else { do { JsonNode node = cursor.getCurrentNode(); if (node == null || node instanceof NullNode) { l.add(null); } else { if (cursor.firstChild()) { l.add(objectToBson(cursor, md)); cursor.parent(); } else { l.add(null); } } } while (cursor.nextSibling()); } return l; }
return cursor.getCurrentNode(); } else { if (cursor.firstChild()) {
private void translate(ArrayField field, JsonNodeCursor cursor, Object target) { if(!cursor.firstChild()){ throw Error.get(MetadataConstants.ERR_ILL_FORMED_METADATA, cursor.getCurrentPath().toString()); } ArrayElement arrayElement = field.getElement(); if (arrayElement instanceof SimpleArrayElement) { List<Object> items = new ArrayList<>(); do { items.add(fromJson(arrayElement.getType(), cursor.getCurrentNode())); } while (cursor.nextSibling()); translate(field, items, target); } else if(arrayElement instanceof ObjectArrayElement){ List<Object> items = new ArrayList<>(); do { Object item = createInstanceFor(arrayElement.getFullPath()); translate((ObjectArrayElement) arrayElement, cursor.getCurrentNode(), item); items.add(item); } while (cursor.nextSibling()); translate(field, items, target); } else{ throw Error.get(MetadataConstants.ERR_UNSUPPORTED_FEATURE + arrayElement.getClass().getName(), field.getFullPath().toString()); } cursor.parent(); }
private void translate(ArrayField field, JsonNodeCursor cursor, Object target) { if(!cursor.firstChild()){ throw Error.get(MetadataConstants.ERR_ILL_FORMED_METADATA, cursor.getCurrentPath().toString()); } ArrayElement arrayElement = field.getElement(); if (arrayElement instanceof SimpleArrayElement) { List<Object> items = new ArrayList<>(); do { items.add(fromJson(arrayElement.getType(), cursor.getCurrentNode())); } while (cursor.nextSibling()); translate(field, items, target); } else if(arrayElement instanceof ObjectArrayElement){ List<Object> items = new ArrayList<>(); do { Object item = createInstanceFor(arrayElement.getFullPath()); translate((ObjectArrayElement) arrayElement, cursor.getCurrentNode(), item); items.add(item); } while (cursor.nextSibling()); translate(field, items, target); } else{ throw Error.get(MetadataConstants.ERR_UNSUPPORTED_FEATURE + arrayElement.getClass().getName(), field.getFullPath().toString()); } cursor.parent(); }
JsonNode fieldNode = cursor.getCurrentNode(); LOGGER.debug("projectObject context={} fieldPath={} contextRelativePath={}", contextPath, fieldPath, contextRelativePath); FieldTreeNode fieldMd = mdContext.resolve(contextRelativePath);
/** * @param cursor The cursor, pointing to the first element of the object */ private BasicDBObject objectToBson(JsonNodeCursor cursor, EntityMetadata md) { BasicDBObject ret = new BasicDBObject(); do { Path path = cursor.getCurrentPath(); JsonNode node = cursor.getCurrentNode(); LOGGER.debug("field: {}", path); FieldTreeNode fieldMdNode = md.resolve(path); if (fieldMdNode == null) { throw Error.get(ERR_INVALID_FIELD, path.toString()); } if (fieldMdNode instanceof SimpleField) { toBson(ret, (SimpleField) fieldMdNode, path, node); } else if (fieldMdNode instanceof ObjectField) { convertObjectFieldToBson(node, cursor, ret, path, md); } else if (fieldMdNode instanceof ArrayField) { convertArrayFieldToBson(node, cursor, ret, fieldMdNode, path, md); } else if (fieldMdNode instanceof ReferenceField) { convertReferenceFieldToBson(node,path); } } while (cursor.nextSibling()); return ret; }
/** * Uses the current position of the cursor to translate the current node and any children it may have. * This is ultimately the driver behind this class and may be called recursively by implementing classes to process child nodes.<br> * <b>NOTE:</b> Calling method is responsible for moving the cursor to where it needs to be. * @param cursor - {@link JsonNodeCursor} * @param target - T */ protected void translate(JsonNodeCursor cursor, Object target) { JsonNode node = cursor.getCurrentNode(); FieldTreeNode fieldNode = entityMetadata.resolve(cursor.getCurrentPath()); Error.push(fieldNode.getFullPath().getLast()); try{ if (fieldNode instanceof SimpleField) { translate((SimpleField) fieldNode, node, target); } else if (fieldNode instanceof ObjectField) { translate((ObjectField) fieldNode, cursor, target); } else if (fieldNode instanceof ArrayField) { translate((ArrayField) fieldNode, cursor, target); } else{ throw Error.get(MetadataConstants.ERR_UNSUPPORTED_FEATURE + fieldNode.getClass().getName(), fieldNode.getFullPath().toString()); } } finally{ Error.pop(); } }
/** * Uses the current position of the cursor to translate the current node and any children it may have. * This is ultimately the driver behind this class and may be called recursively by implementing classes to process child nodes.<br> * <b>NOTE:</b> Calling method is responsible for moving the cursor to where it needs to be. * @param cursor - {@link JsonNodeCursor} * @param target - T */ protected void translate(JsonNodeCursor cursor, Object target) { JsonNode node = cursor.getCurrentNode(); FieldTreeNode fieldNode = entityMetadata.resolve(cursor.getCurrentPath()); Error.push(fieldNode.getFullPath().getLast()); try{ if (fieldNode instanceof SimpleField) { translate((SimpleField) fieldNode, node, target); } else if (fieldNode instanceof ObjectField) { translate((ObjectField) fieldNode, cursor, target); } else if (fieldNode instanceof ArrayField) { translate((ArrayField) fieldNode, cursor, target); } else{ throw Error.get(MetadataConstants.ERR_UNSUPPORTED_FEATURE + fieldNode.getClass().getName(), fieldNode.getFullPath().toString()); } } finally{ Error.pop(); } }
/** * @param cursor The cursor, pointing to the first element of the object */ private BasicDBObject objectToBson(JsonNodeCursor cursor, EntityMetadata md,ResultMetadata rmd) { BasicDBObject ret = new BasicDBObject(); do { Path path = cursor.getCurrentPath(); JsonNode node = cursor.getCurrentNode(); LOGGER.debug("field: {}", path); FieldTreeNode fieldMdNode = md.resolve(path); // Do not translate result metadata fields if(isResultMetadataNode(fieldMdNode)) { fillMetadata(node,rmd); } else if(isDocVerNode(fieldMdNode)) { fillDocVer(node,rmd); } else { if (fieldMdNode instanceof SimpleField) { toBson(ret, (SimpleField) fieldMdNode, path, node, md); } else if (fieldMdNode instanceof ObjectField) { convertObjectFieldToBson(node, cursor, ret, path, md,rmd); } else if (fieldMdNode instanceof ArrayField) { convertArrayFieldToBson(node, cursor, ret, fieldMdNode, path, md,rmd); } else if (fieldMdNode instanceof ReferenceField) { convertReferenceFieldToBson(node, path); } } } while (cursor.nextSibling()); return ret; }
do { Path fieldPath = cursor.getCurrentPath(); JsonNode fieldNode = cursor.getCurrentNode(); LOGGER.debug("project context={} fieldPath={} isArray={}", contextPath, fieldPath, processingArray);
do { Path fieldPath = cursor.getCurrentPath(); JsonNode fieldNode = cursor.getCurrentNode(); LOGGER.debug("project context={} fieldPath={} isArray={}", contextPath, fieldPath, processingArray);
JsonNode node = preCursor.getCurrentNode(); JsonNode node = postCursor.getCurrentNode();
while (preCursor.next()) { Path path = preCursor.getCurrentPath(); JsonNode node = preCursor.getCurrentNode(); while (postCursor.next()) { Path path = postCursor.getCurrentPath(); JsonNode node = postCursor.getCurrentNode();