Skip to content
This repository was archived by the owner on Jun 26, 2021. It is now read-only.

Commit b2be007

Browse files
committed
[#72] Allow deserialization into value
1 parent 1e15540 commit b2be007

3 files changed

Lines changed: 105 additions & 28 deletions

File tree

emfjson-jackson/src/main/java/org/emfjson/jackson/databind/deser/EObjectDeserializer.java

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,30 @@ public EObject deserialize(JsonParser jp, DeserializationContext ctxt) throws IO
5959
return doDeserialize(jp, findRoot(ctxt), ctxt);
6060
}
6161

62+
@Override
63+
public EObject deserialize(JsonParser jp, DeserializationContext ctxt, EObject intoValue) throws IOException {
64+
if (intoValue == null) {
65+
return null;
66+
}
67+
68+
prepareContext(ctxt, null);
69+
70+
final Resource resource = (Resource) ctxt.getAttribute("resource");
71+
final ReferenceEntries entries = (ReferenceEntries) ctxt.getAttribute("entries");
72+
73+
while (jp.nextToken() != JsonToken.END_OBJECT) {
74+
final String fieldName = jp.getCurrentName();
75+
76+
if (options.idField.equalsIgnoreCase(fieldName)) {
77+
options.idDeserializer.deserialize(jp, intoValue, ctxt);
78+
} else {
79+
readFeature(jp, intoValue, fieldName, ctxt, resource, entries);
80+
}
81+
}
82+
83+
return postDeserialize(null, intoValue, null, ctxt);
84+
}
85+
6286
public EObject deserialize(JsonParser jp, DeserializationContext ctxt, EReference containment) throws IOException {
6387
EClass defaultType = null;
6488

@@ -115,16 +139,18 @@ protected EObject postDeserialize(TokenBuffer buffer, EObject object, EClass def
115139
object = EcoreUtil.create(defaultType);
116140
}
117141

118-
final Resource resource = (Resource) ctxt.getAttribute("resource");
119-
final ReferenceEntries entries = (ReferenceEntries) ctxt.getAttribute("entries");
120-
final JsonParser bufferedParser = buffer.asParser();
142+
if (buffer != null) {
143+
final Resource resource = (Resource) ctxt.getAttribute("resource");
144+
final ReferenceEntries entries = (ReferenceEntries) ctxt.getAttribute("entries");
145+
final JsonParser bufferedParser = buffer.asParser();
121146

122-
while (bufferedParser.nextToken() != null) {
123-
readFeature(bufferedParser, object, bufferedParser.getCurrentName(), ctxt, resource, entries);
124-
}
147+
while (bufferedParser.nextToken() != null) {
148+
readFeature(bufferedParser, object, bufferedParser.getCurrentName(), ctxt, resource, entries);
149+
}
125150

126-
bufferedParser.close();
127-
buffer.close();
151+
bufferedParser.close();
152+
buffer.close();
153+
}
128154

129155
return object;
130156
}
@@ -138,7 +164,10 @@ private EClass findRoot(DeserializationContext ctxt) {
138164
return options.rootElement;
139165
}
140166

141-
private void readFeature(JsonParser jp, EObject current, String fieldName, DeserializationContext ctxt, Resource resource, ReferenceEntries entries) throws IOException {
167+
private void readFeature(JsonParser jp, EObject current,
168+
String fieldName, DeserializationContext ctxt,
169+
Resource resource, ReferenceEntries entries) throws IOException {
170+
142171
final EClass eClass = current.eClass();
143172
final EStructuralFeature feature = cache.getEStructuralFeature(eClass, fieldName);
144173

@@ -149,7 +178,7 @@ private void readFeature(JsonParser jp, EObject current, String fieldName, Deser
149178
}
150179

151180
if (feature instanceof EAttribute) {
152-
readAttribute(jp, current, (EAttribute) feature, resource);
181+
readAttribute(jp, current, (EAttribute) feature, resource, ctxt);
153182
} else {
154183
EReference reference = (EReference) feature;
155184

@@ -174,12 +203,9 @@ private void readFeature(JsonParser jp, EObject current, String fieldName, Deser
174203
}
175204

176205
@SuppressWarnings("unchecked")
177-
private void readContainment(JsonParser jp,
178-
EObject owner,
179-
DeserializationContext ctxt,
180-
EReference reference,
181-
Resource resource,
182-
ReferenceEntries entries) throws IOException {
206+
private void readContainment(JsonParser jp, EObject owner,
207+
DeserializationContext ctxt, EReference reference,
208+
Resource resource, ReferenceEntries entries) throws IOException {
183209

184210
final Class<?> type = reference.getEReferenceType().getInstanceClass();
185211

@@ -227,7 +253,10 @@ private void readContainment(JsonParser jp,
227253
}
228254
}
229255

230-
private void readReference(JsonParser jp, EObject owner, EReference reference, ReferenceEntries entries, DeserializationContext context) throws IOException {
256+
private void readReference(JsonParser jp, EObject owner,
257+
EReference reference, ReferenceEntries entries,
258+
DeserializationContext context) throws IOException {
259+
231260
if (jp.getCurrentToken() == JsonToken.START_ARRAY) {
232261
while (jp.nextToken() != JsonToken.END_ARRAY) {
233262
entries.add(options.referenceDeserializer.deserialize(jp, owner, reference, context));
@@ -237,7 +266,9 @@ private void readReference(JsonParser jp, EObject owner, EReference reference, R
237266
}
238267
}
239268

240-
private void readAttribute(JsonParser jp, EObject owner, EAttribute attribute, Resource resource) throws IOException {
269+
private void readAttribute(JsonParser jp, EObject owner,
270+
EAttribute attribute, Resource resource,
271+
DeserializationContext ctxt) throws IOException {
241272
final EDataType dataType = attribute.getEAttributeType();
242273
if (dataType == null) {
243274
resource.getErrors().add(new JSONException("Missing feature type", jp.getCurrentLocation()));
@@ -247,14 +278,18 @@ private void readAttribute(JsonParser jp, EObject owner, EAttribute attribute, R
247278

248279
if (jp.getCurrentToken() == JsonToken.START_ARRAY) {
249280
while (jp.nextToken() != JsonToken.END_ARRAY) {
250-
readSingleAttribute(owner, attribute, resource, dataType, jp);
281+
readSingleAttribute(jp, owner, attribute, resource, dataType, ctxt);
251282
}
252283
} else {
253-
readSingleAttribute(owner, attribute, resource, dataType, jp);
284+
readSingleAttribute(jp, owner, attribute, resource, dataType, ctxt);
254285
}
255286
}
256287

257-
private void readSingleAttribute(EObject owner, EAttribute attribute, Resource resource, EDataType dataType, JsonParser jp) throws IOException {
288+
private void readSingleAttribute(JsonParser jp, EObject owner,
289+
EAttribute attribute, Resource resource,
290+
EDataType dataType,
291+
DeserializationContext ctxt) throws IOException {
292+
258293
final Class<?> type = dataType.getInstanceClass();
259294

260295
Object value;
@@ -266,7 +301,7 @@ private void readSingleAttribute(EObject owner, EAttribute attribute, Resource r
266301

267302
} else {
268303

269-
value = jp.readValueAs(type);
304+
value = ctxt.readValue(jp, type);
270305

271306
}
272307

emfjson-jackson/src/main/java/org/emfjson/jackson/databind/deser/ResourceDeserializer.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,19 @@ public boolean isCachable() {
4242

4343
@Override
4444
public Resource deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
45-
final Resource resource = getResource(ctxt);
46-
final ReferenceEntries entries = new ReferenceEntries();
45+
return doDeserialize(jp, getResource(ctxt), ctxt);
46+
}
47+
48+
@Override
49+
public Resource deserialize(JsonParser jp, DeserializationContext ctxt, Resource intoValue) throws IOException {
50+
return doDeserialize(jp, intoValue, ctxt);
51+
}
52+
53+
private Resource doDeserialize(JsonParser jp, Resource resource, DeserializationContext ctxt) throws IOException {
54+
if (resource == null)
55+
return null;
4756

57+
final ReferenceEntries entries = new ReferenceEntries();
4858
ctxt.setAttribute("resource", resource);
4959
ctxt.setAttribute("entries", entries);
5060

@@ -57,8 +67,8 @@ public Resource deserialize(JsonParser jp, DeserializationContext ctxt) throws I
5767
while (jp.nextToken() != JsonToken.END_ARRAY) {
5868

5969
EObject result = ctxt.readPropertyValue(jp,
60-
new ResourceProperty(resourceSet, resource, entries),
61-
EObject.class);
70+
new ResourceProperty(resourceSet, resource, entries),
71+
EObject.class);
6272

6373
if (result != null) {
6474
resource.getContents().add(result);
@@ -69,8 +79,8 @@ public Resource deserialize(JsonParser jp, DeserializationContext ctxt) throws I
6979
} else if (jp.getCurrentToken() == JsonToken.START_OBJECT) {
7080

7181
EObject result = ctxt.readPropertyValue(jp,
72-
new ResourceProperty(resourceSet, resource, entries),
73-
EObject.class);
82+
new ResourceProperty(resourceSet, resource, entries),
83+
EObject.class);
7484

7585
if (result != null) {
7686
resource.getContents().add(result);

emfjson-jackson/src/test/java/org/emfjson/jackson/junit/tests/ModuleTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import org.eclipse.emf.ecore.EcorePackage;
2121
import org.eclipse.emf.ecore.resource.Resource;
2222
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
23+
import org.emfjson.jackson.junit.model.ModelFactory;
24+
import org.emfjson.jackson.junit.model.User;
2325
import org.emfjson.jackson.module.EMFModule;
2426
import org.emfjson.jackson.resource.JsonResource;
2527
import org.junit.Before;
@@ -90,4 +92,34 @@ public void testReadResourceWithModule() throws IOException {
9092
assertEquals("A", ((EClass) result.getContents().get(0)).getName());
9193
}
9294

95+
@Test
96+
public void testLoadIntoValue() throws IOException {
97+
JsonNode data = mapper.createObjectNode()
98+
.put("name", "A");
99+
100+
User user = ModelFactory.eINSTANCE.createUser();
101+
mapper.readerForUpdating(user)
102+
.treeToValue(data, EObject.class);
103+
104+
assertEquals("A", user.getName());
105+
}
106+
107+
@Test
108+
public void testLoadIntoResource() throws JsonProcessingException {
109+
JsonNode data = mapper.createObjectNode()
110+
.put("eClass", "http://www.eclipse.org/emf/2002/Ecore#//EClass")
111+
.put("name", "A");
112+
113+
Resource resource = new JsonResource();
114+
115+
mapper.readerForUpdating(resource)
116+
.treeToValue(data, Resource.class);
117+
118+
assertEquals(1, resource.getContents().size());
119+
120+
EObject root = resource.getContents().get(0);
121+
122+
assertEquals(EcorePackage.Literals.ECLASS, root.eClass());
123+
assertEquals("A", ((EClass) root).getName());
124+
}
93125
}

0 commit comments

Comments
 (0)