Skip to content

Commit

Permalink
fix json externalization problem
Browse files Browse the repository at this point in the history
  • Loading branch information
aeberhart committed Nov 12, 2024
1 parent dfc131b commit 80c81b0
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,12 @@ public boolean update(Table s, Map<String, Object> search, Map<String, Object> o
for (Entry<String, Object> e : object.entrySet()) {
if (e.getValue() == null)
r.remove(e.getKey());
else
else {
// when nested objects are overwritten and the old value contains a pointer,
// make sure to re-use the pointer
copyPointers(r.get(e.getKey()), e.getValue());
r.put(e.getKey(), e.getValue());
}
}
return update(s, r);
}
Expand Down Expand Up @@ -304,4 +308,29 @@ public static String toJsonString(Map<String, Object> obj) throws JsonProcessing
public static Map<String, Object> fromJsonString(String json) throws JsonProcessingException {
return objectMapper.readValue(json, JSONDatabase.tr);
}

/**
* copy -pointer fields from source to dest
*/
@SuppressWarnings("unchecked")
static void copyPointers(Object _from, Object _to) {
if (_from instanceof Map && _to instanceof Map) {
Map<String, Object> from = (Map<String, Object>) _from;
Map<String, Object> to = (Map<String, Object>) _to;
for (Entry<String, Object> e : from.entrySet()) {
Object toValue = to.get(e.getKey());
if (e.getKey().endsWith("-pointer")) {
String key = e.getKey().substring(0, e.getKey().length() - "-pointer".length());
if (from.containsKey(key))
if (to.containsKey(key))
if (toValue == null)
// from and to contain externalized value
// only from contains pointer
// re-use the from pointer
to.put(e.getKey(), e.getValue());
}
copyPointers(e.getValue(), toValue);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -428,4 +428,45 @@ public void testExtraField() throws Exception {
Payload p = JSONDatabase.fromMap(MapUtil.of("s", "hello world", "unknown", 42), Payload.class);
Assertions.assertEquals(p.s, "hello world");
}

@SuppressWarnings("unchecked")
@Test
public void testCopyPointer() {
JSONDatabase.copyPointers(null, null);
JSONDatabase.copyPointers(1, null);
JSONDatabase.copyPointers("test", null);
JSONDatabase.copyPointers(null, 1);
JSONDatabase.copyPointers(null, "test");
JSONDatabase.copyPointers(null, Map.of("x", 1));
JSONDatabase.copyPointers(Map.of("x", 1), null);

Map<String, Object> from = MapUtil.of("x", "code", "x-pointer", "0.txt");
Map<String, Object> to = MapUtil.of("x", "new code");
JSONDatabase.copyPointers(from, to);
Assertions.assertEquals("0.txt", to.get("x-pointer"));

from = Map.of("n", MapUtil.of("x", "code", "x-pointer", "0.txt"));
to = Map.of("n", MapUtil.of("x", "new code"));
JSONDatabase.copyPointers(from, to);
Assertions.assertEquals("0.txt", ((Map<String, Object>) to.get("n")).get("x-pointer"));

from = Map.of("n", MapUtil.of("x", "code", "x-pointer", "0.txt"));
to = Map.of();
JSONDatabase.copyPointers(from, to);

from = MapUtil.of("x", "code", "x-pointer", "0.txt");
to = MapUtil.of();
JSONDatabase.copyPointers(from, to);
Assertions.assertNull(to.get("x-pointer"));

from = MapUtil.of("x-pointer", "0.txt");
to = MapUtil.of("x", "code");
JSONDatabase.copyPointers(from, to);
Assertions.assertNull(to.get("x-pointer"));

from = MapUtil.of("x", "code", "x-pointer", "0.txt");
to = MapUtil.of("x", "code", "x-pointer", "1.txt");
JSONDatabase.copyPointers(from, to);
Assertions.assertEquals("1.txt", to.get("x-pointer"));
}
}

0 comments on commit 80c81b0

Please sign in to comment.