/** * Encode a {@link PropertySetSerializationNode} into a document fragment. * @param context Resolution context * @param propertyBox The PropertyBox to encode * @param node The property set serialization node * @param parentPath Parent nodes path * @return The document fragment field-value map * @throws InvalidExpressionException If an error occurred */ private static Map<String, Object> encodePropertyBoxNode(MongoDocumentContext<?> context, PropertyBox propertyBox, PropertySetSerializationNode node, String parentPath, boolean forUpdate) throws InvalidExpressionException { return isValidNodeProperty(node) .map(p -> encodeProperty(context, propertyBox, p, node.getName(), parentPath, forUpdate)) .orElseGet(() -> { // nested String parent = composeFieldPath(parentPath, node.getName()); Map<String, Object> nested = encodePropertyBoxNodes(context, propertyBox, node.getChildren(), parent, forUpdate); return forUpdate ? nested : Collections.singletonMap(node.getName(), nested); }); }
/** * Serialize the node of a PropertyBox serialization tree. * @param context Serialization context * @param obj Json object to use * @param propertyBox PropertyBox to serialize * @param node The PropertyBox node to serialize * @throws JsonSerializationException If an error occurred */ private static void serializePropertyBoxNode(JsonSerializationContext context, JsonObject obj, PropertyBox propertyBox, PropertySetSerializationNode node) throws JsonSerializationException { if (node.getProperty().isPresent()) { serializePropertyBoxProperty(context, obj, propertyBox, node.getProperty().get(), node.getName()); } else { try { // add nested object JsonObject nested = new JsonObject(); obj.add(node.getName(), nested); // serialize nested object serializePropertyBoxNodes(context, nested, propertyBox, node.getChildren()); } catch (JsonSerializationException se) { throw se; } catch (Exception e) { throw new JsonSerializationException( "Failed to serialize PropertyBox [" + propertyBox + "] for field name [" + node.getName() + "]", e); } } }