Skip to content

Commit 8e7720b

Browse files
committed
[Bug #84] Use type coercion when deserializing typed literal into typed properties.
1 parent 22170e6 commit 8e7720b

File tree

3 files changed

+72
-6
lines changed

3 files changed

+72
-6
lines changed

src/main/java/cz/cvut/kbss/jsonld/deserialization/expanded/CollectionDeserializer.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,20 @@ private void resolveValue(JsonObject value) {
7676
} else if (value.containsKey(JsonLd.LANGUAGE)) {
7777
assert value.containsKey(JsonLd.VALUE);
7878
instanceBuilder.addValue(
79-
new LangString(ValueUtils.stringValue(ValueUtils.getValue(value)), ValueUtils.stringValue(value.get(JsonLd.LANGUAGE))));
79+
new LangString(ValueUtils.stringValue(ValueUtils.getValue(value)),
80+
ValueUtils.stringValue(value.get(JsonLd.LANGUAGE))));
8081
} else if (instanceBuilder.isCurrentCollectionProperties()) {
81-
// If we are deserializing an object into @Properties, just extract the identifier and put it into the map
82-
if (!value.containsKey(JsonLd.ID)) {
82+
if (value.containsKey(JsonLd.TYPE) && value.containsKey(JsonLd.VALUE)) {
83+
instanceBuilder.addValue(
84+
XSDTypeCoercer.coerceType(ValueUtils.stringValue(ValueUtils.getValue(value)),
85+
ValueUtils.stringValue(value.get(JsonLd.TYPE))));
86+
} else if (!value.containsKey(JsonLd.ID)) {
8387
throw new MissingIdentifierException(
8488
"Cannot put an object without an identifier into @Properties. Object: " + value);
89+
} else {
90+
// If we are deserializing an object into @Properties, just extract the identifier and put it into the map
91+
instanceBuilder.addValue(URI.create(ValueUtils.stringValue(value.get(JsonLd.ID))));
8592
}
86-
instanceBuilder.addValue(URI.create(ValueUtils.stringValue(value.get(JsonLd.ID))));
8793
} else {
8894
final Class<?> elementType = instanceBuilder.getCurrentCollectionElementType();
8995
new ObjectDeserializer(instanceBuilder, config, elementType).processValue(value);
@@ -126,9 +132,11 @@ private void extractLiteralValue(JsonObject value) {
126132
final JsonValue val = value.get(JsonLd.VALUE);
127133
if (value.containsKey(JsonLd.TYPE)) {
128134
instanceBuilder
129-
.addValue(property, XSDTypeCoercer.coerceType(ValueUtils.stringValue(val), ValueUtils.stringValue(value.get(JsonLd.TYPE))));
135+
.addValue(property, XSDTypeCoercer.coerceType(ValueUtils.stringValue(val),
136+
ValueUtils.stringValue(value.get(JsonLd.TYPE))));
130137
} else if (value.containsKey(JsonLd.LANGUAGE)) {
131-
instanceBuilder.addValue(property, new LangString(ValueUtils.stringValue(val), ValueUtils.stringValue(value.get(JsonLd.LANGUAGE))));
138+
instanceBuilder.addValue(property, new LangString(ValueUtils.stringValue(val),
139+
ValueUtils.stringValue(value.get(JsonLd.LANGUAGE))));
132140
} else {
133141
instanceBuilder.addValue(property, ValueUtils.literalValue(val));
134142
}

src/test/java/cz/cvut/kbss/jsonld/deserialization/expanded/ExpandedJsonLdDeserializerTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import cz.cvut.kbss.jsonld.environment.model.Organization;
4242
import cz.cvut.kbss.jsonld.environment.model.OwlPropertyType;
4343
import cz.cvut.kbss.jsonld.environment.model.Person;
44+
import cz.cvut.kbss.jsonld.environment.model.PersonWithTypedProperties;
4445
import cz.cvut.kbss.jsonld.environment.model.Role;
4546
import cz.cvut.kbss.jsonld.environment.model.Study;
4647
import cz.cvut.kbss.jsonld.environment.model.StudyOnPersons;
@@ -75,6 +76,7 @@
7576
import static org.hamcrest.Matchers.anyOf;
7677
import static org.hamcrest.Matchers.containsString;
7778
import static org.hamcrest.Matchers.hasItems;
79+
import static org.hamcrest.Matchers.hasKey;
7880
import static org.hamcrest.Matchers.instanceOf;
7981
import static org.junit.jupiter.api.Assertions.assertEquals;
8082
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -891,4 +893,28 @@ void deserializationHandlesNumericValuesWithDatatypes() throws Exception {
891893
assertEquals(BigInteger.valueOf(128000), result.getBigIntegerValue());
892894
assertEquals(BigDecimal.valueOf(128000.821), result.getBigDecimalValue());
893895
}
896+
897+
/**
898+
* Bug #84
899+
*/
900+
@Test
901+
void deserializationHandlesTypedLiteralsForTypedProperties() throws Exception {
902+
final JsonArray input = readAndExpand("objectWithTypedLiteralInProperties.json");
903+
final PersonWithTypedProperties result = sut.deserialize(input, PersonWithTypedProperties.class);
904+
assertNotNull(result);
905+
assertEquals("Catherine", result.getFirstName());
906+
assertEquals("Halsey", result.getLastName());
907+
assertThat(result.getProperties(),
908+
hasKey(URI.create("http://onto.fel.cvut.cz/ontologies/application/jb4jsonld/attribute/warrantor")));
909+
assertEquals(Set.of("Lord Hood"), result.getProperties().get(URI.create(
910+
"http://onto.fel.cvut.cz/ontologies/application/jb4jsonld/attribute/warrantor")));
911+
assertThat(result.getProperties(), hasKey(URI.create(
912+
"http://onto.fel.cvut.cz/ontologies/application/jb4jsonld/attribute/revision-count")));
913+
assertEquals(Set.of(4, 8), result.getProperties().get(URI.create(
914+
"http://onto.fel.cvut.cz/ontologies/application/jb4jsonld/attribute/revision-count")));
915+
assertThat(result.getProperties(),
916+
hasKey(URI.create("http://onto.fel.cvut.cz/ontologies/application/jb4jsonld/attribute/approved")));
917+
assertEquals(Set.of(true), result.getProperties().get(URI.create(
918+
"http://onto.fel.cvut.cz/ontologies/application/jb4jsonld/attribute/approved")));
919+
}
894920
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"@context": {
3+
"firstName": "http://xmlns.com/foaf/0.1/firstName",
4+
"lastName": "http://xmlns.com/foaf/0.1/lastName",
5+
"types": "@type",
6+
"iri": "@id"
7+
},
8+
"iri": "http://krizik.felk.cvut.cz/ontologies/jb4jsonld#Catherine+Halsey",
9+
"types": [
10+
"http://onto.fel.cvut.cz/ontologies/ufo/Person",
11+
"http://krizik.felk.cvut.cz/ontologies/jb4jsonld/Employee",
12+
"http://krizik.felk.cvut.cz/ontologies/jb4jsonld/User"
13+
],
14+
"firstName": "Catherine",
15+
"lastName": "Halsey",
16+
"http://onto.fel.cvut.cz/ontologies/application/jb4jsonld/attribute/warrantor": [
17+
"Lord Hood"
18+
],
19+
"http://onto.fel.cvut.cz/ontologies/application/jb4jsonld/attribute/revision-count": [
20+
{
21+
"types": "http://www.w3.org/2001/XMLSchema#int",
22+
"@value": 4
23+
},
24+
{
25+
"types": "http://www.w3.org/2001/XMLSchema#int",
26+
"@value": 8
27+
}
28+
],
29+
"http://onto.fel.cvut.cz/ontologies/application/jb4jsonld/attribute/approved": [
30+
true
31+
]
32+
}

0 commit comments

Comments
 (0)