InPlaceLeftHandMerger implements BinaryOperator<JsonNode> { @Override public JsonNode apply(JsonNode target, JsonNode source) { if (target == null) { return source; } if (source == null) { return target; } if (source.getNodeType() != target.getNodeType() && (source.getNodeType() != JsonNodeType.NULL) && (target.getNodeType() != JsonNodeType.NULL)) { throw new RuntimeException( "Can't merge incompatible node types: " + target.getNodeType() + " vs. " + source.getNodeType()); } switch (source.getNodeType()) { case ARRAY: return mergeArrays(target, source); case OBJECT: return mergeObjects(target, source); case BINARY: case BOOLEAN: case NULL: case NUMBER: case STRING: return mergeScalars(target, source); default: bootLogger.stderr("Skipping merging of unsupported JSON node: " + source.getNodeType()); } return target; } InPlaceLeftHandMerger(BootLogger bootLogger); @Override JsonNode apply(JsonNode target, JsonNode source);  }
@Test public void testApply_MergeMap() { JsonNode target = parse("{\"a\":1}"); JsonNode source = parse("{\"b\":{\"c\":2}}"); JsonNode merged = merger.apply(target, source); assertSame(target, merged); assertEquals("{\"a\":1,\"b\":{\"c\":2}}", merged.toString()); }
@Test public void testApply_MergeList_Replace() { JsonNode target = parse("[1,2,5]"); JsonNode source = parse("[4,6]"); JsonNode merged = merger.apply(target, source); assertEquals("[4,6]", merged.toString()); }
@Test public void testApply_OverrideValues() { JsonNode target = parse("{\"a\":true,\"b\":\"string\",\"c\":5,\"d\":\"unchanged\"}"); JsonNode source = parse("{\"a\":false,\"b\":\"string1\",\"c\":6}"); JsonNode merged = merger.apply(target, source); assertEquals("{\"a\":false,\"b\":\"string1\",\"c\":6,\"d\":\"unchanged\"}", merged.toString()); }
@Test public void testApply_OverrideValues_Null() { JsonNode target = parse("{\"a\":null,\"b\":\"not_null\",\"c\":null}"); JsonNode source = parse("{\"a\":\"not_null\",\"b\":null,\"c\":null}"); JsonNode merged = merger.apply(target, source); assertEquals("{\"a\":\"not_null\",\"b\":null,\"c\":null}", merged.toString()); }
@Test public void testApply_MergeSubMap() { JsonNode target = parse("{\"a\":{\"b\":2}}"); JsonNode source = parse("{\"a\":{\"c\":3}}"); JsonNode merged = merger.apply(target, source); assertEquals("{\"a\":{\"b\":2,\"c\":3}}", merged.toString()); }
@Test public void testApply_MergeSubList_Replace() { JsonNode target = parse("{\"a\":[1,3,5]}"); JsonNode source = parse("{\"a\":[4,6]}"); JsonNode merged = merger.apply(target, source); assertEquals("{\"a\":[4,6]}", merged.toString()); }
@Test public void testApply_MultiMerge() { JsonNode target = parse("{\"a\":{\"b\":2},\"e\":6}"); JsonNode source1 = parse("{\"a\":{\"b\":3,\"c\":true,\"d\":5}}"); JsonNode source2 = parse("{\"a\":{\"b\":4,\"c\":false}}"); JsonNode merged = merger.apply(target, source1); merged = merger.apply(merged, source2); assertEquals("{\"a\":{\"b\":4,\"c\":false,\"d\":5},\"e\":6}", merged.toString()); }