Skip to content
This repository was archived by the owner on Apr 9, 2025. It is now read-only.

Commit fabb19a

Browse files
committed
adding test (and solving bugs) for the serialization of extensions.
1 parent 38f8b7d commit fabb19a

File tree

2 files changed

+38
-32
lines changed

2 files changed

+38
-32
lines changed

src/main/java/org/lambda3/text/simplification/discourse/model/serializer/ExtensionsDeserializer.java

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.fasterxml.jackson.core.JsonGenerationException;
2626
import com.fasterxml.jackson.core.JsonParser;
2727
import com.fasterxml.jackson.core.JsonProcessingException;
28+
import com.fasterxml.jackson.core.ObjectCodec;
2829
import com.fasterxml.jackson.databind.DeserializationContext;
2930
import com.fasterxml.jackson.databind.JsonNode;
3031
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
@@ -43,46 +44,51 @@ protected ExtensionsDeserializer() {
4344
public Map deserialize(JsonParser parser, DeserializationContext deserializationContext)
4445
throws IOException, JsonProcessingException {
4546

46-
JsonNode node = parser.getCodec().readTree(parser);
47-
Iterator<JsonNode> iter = node.iterator();
48-
4947
Map<Object, Object> map = new HashMap<>();
5048

51-
while (iter.hasNext()) {
52-
JsonNode en = iter.next();
53-
try {
54-
Class<?> clazz = Class.forName(en.get("class").asText());
55-
String key = en.has("key") ? en.get("key").asText() : null;
56-
JsonNode nodeContent = en.get("content");
49+
ObjectCodec codec = parser.getCodec();
50+
if (codec != null) {
51+
JsonNode node = parser.getCodec().readTree(parser);
52+
Iterator<JsonNode> iter = node.iterator();
53+
54+
while (iter.hasNext()) {
55+
JsonNode en = iter.next();
56+
try {
57+
Class<?> clazz = Class.forName(en.get("class").asText());
58+
String key = en.has("key") ? en.get("key").asText() : null;
59+
JsonNode nodeContent = en.get("content");
5760

58-
Object mapKey;
59-
Object content;
61+
Object mapKey;
62+
Object content;
6063

61-
if (nodeContent.isArray()) {
62-
content = new LinkedList<>();
63-
for (JsonNode aNodeContent : nodeContent) {
64-
JsonParser panc = aNodeContent.traverse();
65-
panc.nextToken();
66-
Object co = deserializationContext.readValue(panc, clazz);
67-
((List) content).add(co);
64+
if (nodeContent.isArray()) {
65+
content = new LinkedList<>();
66+
for (JsonNode aNodeContent : nodeContent) {
67+
JsonParser panc = aNodeContent.traverse();
68+
panc.nextToken();
69+
Object co = deserializationContext.readValue(panc, clazz);
70+
((List) content).add(co);
71+
}
72+
} else {
73+
JsonParser cjp = nodeContent.traverse();
74+
cjp.nextToken();
75+
content = deserializationContext.readValue(cjp, clazz);
6876
}
69-
} else {
70-
JsonParser cjp = nodeContent.traverse();
71-
cjp.nextToken();
72-
content = deserializationContext.readValue(cjp, clazz);
73-
}
7477

75-
if (key == null) {
76-
mapKey = clazz;
77-
} else {
78-
mapKey = new Extensible.Key(clazz, key);
79-
}
78+
if (key == null) {
79+
mapKey = clazz;
80+
} else {
81+
mapKey = new Extensible.Key(clazz, key);
82+
}
8083

81-
map.put(mapKey, content);
84+
map.put(mapKey, content);
8285

83-
} catch (ClassNotFoundException e) {
84-
throw new JsonGenerationException(e);
86+
} catch (ClassNotFoundException e) {
87+
throw new JsonGenerationException(e);
88+
}
8589
}
90+
} else {
91+
parser.nextToken();
8692
}
8793

8894
return map;

src/test/java/org/lambda3/text/simplification/discourse/ExtensibleTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ public void testDeSerialization() throws IOException {
8888
e.addListExtension(new LinkedContext("bla bla", RelationType.UNKNOWN));
8989
e.addListExtension(new LinkedContext("bla bla 2", RelationType.UNKNOWN));
9090
e.addExtension("key", 7);
91+
e.addExtension(new SimpleExtensible());
9192

9293
File temp = File.createTempFile("ext-discourse-simplification", ".json");
93-
System.out.println(temp);
9494
temp.deleteOnExit();
9595

9696
ObjectMapper mapper = new ObjectMapper();

0 commit comments

Comments
 (0)