@Test public void nullify_array() throws Exception { UpdateExpression expr = EvalTestContext.updateExpressionFromJson("[ {'$set' : { 'field7' : '$null' } }] "); Updater updater = Updater.getInstance(JSON_NODE_FACTORY, md, expr); Assert.assertTrue(updater.update(jsonDoc, md.getFieldTreeRoot(), new Path())); Assert.assertEquals(NullNode.class, jsonDoc.get(new Path("field7")).getClass()); }
@Override public boolean update(JsonDoc doc, FieldTreeNode contextMd, Path contextPath) { boolean ret = false; LOGGER.debug("Starting"); for (FieldData df : setValues) { LOGGER.debug("Set field {} in ctx: {} to {}/{}", df.field, contextPath, df.value, df.value.getType()); JsonNode oldValueNode; JsonNode newValueNode = null; Object newValue = null; Type newValueType = null; switch (df.value.getType()) { case _null: newValueNode = factory.nullNode(); break; case _dereference: JsonNode refNode = doc.get(new Path(contextPath, df.refPath)); if (refNode != null) { newValueNode = refNode.deepCopy(); newValue = df.refType.fromJson(newValueNode); newValueType = df.refType; } break; case _value: newValue = df.value.getValue().getValue(); newValueNode = newValue instanceof JsonNode ? (JsonNode) newValue : df.fieldType.toJson(factory, newValue); newValueType = df.fieldType; break; } oldValueNode = setOrAdd(doc, contextPath, df, newValueNode, newValue, newValueType); if (!ret) { ret = oldAndNewAreDifferent(oldValueNode, newValueNode); } } LOGGER.debug("Completed"); return ret; }
SetExpressionEvaluator extends Updater { @Override public boolean update(JsonDoc doc, FieldTreeNode contextMd, Path contextPath) { boolean ret = false; LOGGER.debug("Starting"); for (FieldData df : setValues) { LOGGER.debug("Set field {} in ctx: {} to {}/{}", df.field, contextPath, df.value, df.value.getType()); JsonNode oldValueNode; JsonNode newValueNode = null; Object newValue = null; Type newValueType = null; switch (df.value.getType()) { case _null: newValueNode = factory.nullNode(); break; case _dereference: JsonNode refNode = doc.get(new Path(contextPath, df.refPath)); if (refNode != null) { newValueNode = refNode.deepCopy(); newValue = df.refType.fromJson(newValueNode); newValueType = df.refType; } break; case _value: newValue = df.value.getValue().getValue(); newValueNode = newValue instanceof JsonNode ? (JsonNode) newValue : df.fieldType.toJson(factory, newValue); newValueType = df.fieldType; break; } oldValueNode = setOrAdd(doc, contextPath, df, newValueNode, newValue, newValueType); if (!ret) { ret = oldAndNewAreDifferent(oldValueNode, newValueNode); } } LOGGER.debug("Completed"); return ret; } }
SetExpressionEvaluator extends Updater { @Override public boolean update(JsonDoc doc, FieldTreeNode contextMd, Path contextPath) { boolean ret = false; LOGGER.debug("Starting"); for (FieldData df : setValues) { LOGGER.debug("Set field {} in ctx: {} to {}/{}", df.field, contextPath, df.value, df.value.getType()); JsonNode oldValueNode; JsonNode newValueNode = null; Object newValue = null; Type newValueType = null; switch (df.value.getType()) { case _null: newValueNode = factory.nullNode(); break; case _dereference: JsonNode refNode = doc.get(new Path(contextPath, df.refPath)); if (refNode != null) { newValueNode = refNode.deepCopy(); newValue = df.refType.fromJson(newValueNode); newValueType = df.refType; } break; case _value: newValue = df.value.getValue().getValue(); newValueNode = newValue instanceof JsonNode ? (JsonNode) newValue : df.fieldType.toJson(factory, newValue); newValueType = df.fieldType; break; } oldValueNode = setOrAdd(doc, contextPath, df, newValueNode, newValue, newValueType); if (!ret) { ret = oldAndNewAreDifferent(oldValueNode, newValueNode); } } LOGGER.debug("Completed"); return ret; } SetExpressionEvaluator(JsonNodeFactory factory, FieldTreeNode context, SetExpression expr); }
SetExpressionEvaluator extends Updater { @Override public boolean update(JsonDoc doc, FieldTreeNode contextMd, Path contextPath) { boolean ret = false; LOGGER.debug("Starting"); for (FieldData df : setValues) { LOGGER.debug("Set field {} in ctx: {} to {}/{}", df.field, contextPath, df.value, df.value.getType()); JsonNode oldValueNode; JsonNode newValueNode = null; Object newValue = null; Type newValueType = null; switch (df.value.getType()) { case _null: newValueNode = factory.nullNode(); break; case _dereference: JsonNode refNode = doc.get(new Path(contextPath, df.refPath)); if (refNode != null) { newValueNode = refNode.deepCopy(); newValue = df.refType.fromJson(newValueNode); newValueType = df.refType; } break; case _value: newValue = df.value.getValue().getValue(); newValueNode = newValue instanceof JsonNode ? (JsonNode) newValue : df.fieldType.toJson(factory, newValue); newValueType = df.fieldType; break; } oldValueNode = setOrAdd(doc, contextPath, df, newValueNode, newValue, newValueType); if (!ret) { ret = oldAndNewAreDifferent(oldValueNode, newValueNode); } } LOGGER.debug("Completed"); return ret; } SetExpressionEvaluator(JsonNodeFactory factory, FieldTreeNode context, SetExpression expr); @Override void getUpdateFields(Set<Path> fields); @Override boolean update(JsonDoc doc, FieldTreeNode contextMd, Path contextPath); }
SetExpressionEvaluator extends Updater { @Override public boolean update(JsonDoc doc, FieldTreeNode contextMd, Path contextPath) { boolean ret = false; LOGGER.debug("Starting"); for (FieldData df : setValues) { LOGGER.debug("Set field {} in ctx: {} to {}/{}", df.field, contextPath, df.value, df.value.getType()); JsonNode oldValueNode; JsonNode newValueNode = null; Object newValue = null; Type newValueType = null; switch (df.value.getType()) { case _null: newValueNode = factory.nullNode(); break; case _dereference: JsonNode refNode = doc.get(new Path(contextPath, df.refPath)); if (refNode != null) { newValueNode = refNode.deepCopy(); newValue = df.refType.fromJson(newValueNode); newValueType = df.refType; } break; case _value: newValue = df.value.getValue().getValue(); newValueNode = newValue instanceof JsonNode ? (JsonNode) newValue : df.fieldType.toJson(factory, newValue); newValueType = df.fieldType; break; } oldValueNode = setOrAdd(doc, contextPath, df, newValueNode, newValue, newValueType); if (!ret) { ret = oldAndNewAreDifferent(oldValueNode, newValueNode); } } LOGGER.debug("Completed"); return ret; } SetExpressionEvaluator(JsonNodeFactory factory, FieldTreeNode context, SetExpression expr); @Override void getUpdateFields(Set<Path> fields); @Override boolean update(JsonDoc doc, FieldTreeNode contextMd, Path contextPath);  }