/** * @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; }
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); } } } }
protected void translate(ObjectField field, JsonNodeCursor cursor, Object target) { if(!cursor.firstChild()){ throw Error.get(MetadataConstants.ERR_ILL_FORMED_METADATA, cursor.getCurrentPath().toString()); } do { translate(cursor, target); } while (cursor.nextSibling()); cursor.parent(); }
protected void translate(final ObjectArrayElement objectArrayElement, JsonNode node, Object target) { JsonNodeCursor cursor = new JsonNodeCursor(objectArrayElement.getFullPath(), node); if(!cursor.firstChild()){ throw Error.get(MetadataConstants.ERR_ILL_FORMED_METADATA, cursor.getCurrentPath().toString()); } do { translate(cursor, target); } while (cursor.nextSibling()); }
/** * @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; }
JsonNodeCursor postCursor = processedDocument.getPostDoc().cursor(); while (preCursor.next()) { Path path = preCursor.getCurrentPath(); JsonNode node = preCursor.getCurrentNode(); while (postCursor.next()) { Path path = postCursor.getCurrentPath(); JsonNode node = postCursor.getCurrentNode();
/** * 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(); } }
private JsonNode projectObjectField(JsonNode fieldNode, ObjectNode ret, Path fieldPath, JsonNodeCursor cursor, Projector projector, FieldTreeNode mdContext, Path contextPath, JsonNodeFactory factory, QueryEvaluationContext ctx) { if (fieldNode instanceof ObjectNode) { LOGGER.debug("projecting object node {}",fieldPath); if (cursor.firstChild()) { ObjectNode newNode = projectObject(projector, factory, mdContext, contextPath, cursor, ctx); ret.set(fieldPath.tail(0), newNode); cursor.parent(); } else { ret.set(fieldPath.tail(0), factory.objectNode()); } } else { LOGGER.warn("Expecting object node, found {} for {}", fieldNode.getClass().getName(), fieldPath); } return null; }
Projector deciding=projector.getDecidingProjector(); ArrayNode newNode = factory.arrayNode(); if (cursor.firstChild()) { do { JsonNode node = projectArrayElement(projector, newNode.add(node); } while (cursor.nextSibling()); cursor.parent(); if(deciding instanceof ArrayProjector&& ((ArrayProjector)deciding).getSort()!=null) {
/** * Translates an entire {@link JsonDoc} to T. * @param document - {@link JsonDoc} * @param target - T */ public void translate(JsonDoc document, T target){ Error.push("translating from json"); try{ JsonNodeCursor cursor = document.cursor(); if (!cursor.firstChild()) { //TODO throw exception? return; } do { translate(cursor, target); } while (cursor.nextSibling()); } finally{ Error.pop(); } }
@Override protected void translate(JsonNodeCursor cursor, Object target) { Path path = cursor.getCurrentPath(); FieldTreeNode fieldNode = entityMetadata.resolve(path); if(fieldNode instanceof Field){ String fieldName = ((Field) fieldNode).getName(); Fields fields = entityMetadata.getFields(); if(fieldNode instanceof ObjectField){ fields = ((ObjectField) fieldNode).getFields(); } if(PredefinedFields.isPredefinedField(fieldName, fields)){ /* * Indicates the field is auto-generated for lightblue purposes. These fields * should not be inserted into the data store. */ return; } } super.translate(cursor, target); }
private BasicDBObject toBson(JsonDoc doc, EntityMetadata md) { LOGGER.debug("Entity: {}", md.getName()); BasicDBObject ret = null; JsonNodeCursor cursor = doc.cursor(); if (cursor.firstChild()) { ret = objectToBson(cursor, md); } return ret; }
/** * Returns a cursor that iterates all the nodes under the given root, where * the root is an intermediate node in a Json document accessed by path 'p'. * Path can be empty, meaning the 'root' is the real document root. */ public static JsonNodeCursor cursor(JsonNode root, Path p) { return new JsonNodeCursor(p, root); }
/** * @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; }
protected void translate(final ObjectArrayElement objectArrayElement, JsonNode node, Object target) { JsonNodeCursor cursor = new JsonNodeCursor(objectArrayElement.getFullPath(), node); if(!cursor.firstChild()){ throw Error.get(MetadataConstants.ERR_ILL_FORMED_METADATA, cursor.getCurrentPath().toString()); } do { translate(cursor, target); } while (cursor.nextSibling()); }
while (preCursor.next()) { Path path = preCursor.getCurrentPath(); JsonNode node = preCursor.getCurrentNode(); while (postCursor.next()) { Path path = postCursor.getCurrentPath(); JsonNode node = postCursor.getCurrentNode();
/** * 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(); } }
private void convertObjectFieldToBson(JsonNode node, JsonNodeCursor cursor, BasicDBObject ret, Path path, EntityMetadata md,ResultMetadata rmd) { if (node != null) { if (node instanceof ObjectNode) { if (cursor.firstChild()) { ret.append(path.tail(0), objectToBson(cursor, md,rmd)); cursor.parent(); } } else if (node instanceof NullNode) { ret.append(path.tail(0), null); } else { throw Error.get(ERR_INVALID_FIELD, path.toString()); } } }
/** * Translates an entire {@link JsonDoc} to T. * @param document - {@link JsonDoc} * @param target - T */ public void translate(JsonDoc document, T target){ Error.push("translating from json"); try{ JsonNodeCursor cursor = document.cursor(); if (!cursor.firstChild()) { //TODO throw exception? return; } do { translate(cursor, target); } while (cursor.nextSibling()); } finally{ Error.pop(); } }
@Override protected void translate(JsonNodeCursor cursor, Object target) { Path path = cursor.getCurrentPath(); FieldTreeNode fieldNode = entityMetadata.resolve(path); if(fieldNode instanceof Field){ String fieldName = ((Field) fieldNode).getName(); Fields fields = entityMetadata.getFields(); if(fieldNode instanceof ObjectField){ fields = ((ObjectField) fieldNode).getFields(); } if(PredefinedFields.isPredefinedField(fieldName, fields)){ /* * Indicates the field is auto-generated for lightblue purposes. These fields * should not be inserted into the data store. */ return; } } super.translate(cursor, target); }