Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
252 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/java/cz/cvut/kbss/jsonld/jackson/JsonLdModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package cz.cvut.kbss.jsonld.jackson; | ||
|
||
import com.fasterxml.jackson.databind.module.SimpleModule; | ||
import cz.cvut.kbss.jsonld.jackson.deserialization.JsonLdDeserializerModifier; | ||
import cz.cvut.kbss.jsonld.jackson.serialization.JsonLdSerializerModifier; | ||
|
||
/** | ||
* Simple module with pre-configured serializer and deserializer modifier. | ||
*/ | ||
public class JsonLdModule extends SimpleModule { | ||
|
||
public JsonLdModule() { | ||
init(); | ||
} | ||
|
||
private void init() { | ||
setSerializerModifier(new JsonLdSerializerModifier()); | ||
setDeserializerModifier(new JsonLdDeserializerModifier()); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/main/java/cz/cvut/kbss/jsonld/jackson/deserialization/JacksonJsonLdDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package cz.cvut.kbss.jsonld.jackson.deserialization; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.deser.std.DelegatingDeserializer; | ||
import com.github.jsonldjava.core.JsonLdError; | ||
import com.github.jsonldjava.core.JsonLdProcessor; | ||
import cz.cvut.kbss.jsonld.deserialization.JsonLdDeserializer; | ||
import cz.cvut.kbss.jsonld.exception.JsonLdDeserializationException; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class JacksonJsonLdDeserializer extends DelegatingDeserializer { | ||
|
||
private ObjectMapper mapper = new ObjectMapper(); | ||
|
||
private final Class<?> resultType; | ||
|
||
public JacksonJsonLdDeserializer(JsonDeserializer<?> delegatee, Class<?> resultType) { | ||
super(delegatee); | ||
this.resultType = resultType; | ||
} | ||
|
||
@Override | ||
protected JsonDeserializer<?> newDelegatingInstance(JsonDeserializer<?> newDelegatee) { | ||
// TODO Is the null ok? Perhaps we should use a different base class, we are not delegating deserialization to anything else anyway | ||
return new JacksonJsonLdDeserializer(newDelegatee, null); | ||
} | ||
|
||
@Override | ||
public Object deserialize(JsonParser jp, DeserializationContext ctx) throws IOException { | ||
try { | ||
final Object input = parseJsonObject(jp); | ||
final List<Object> expanded = JsonLdProcessor.expand(input); | ||
final JsonLdDeserializer deserializer = JsonLdDeserializer.createExpandedDeserializer(); | ||
return deserializer.deserialize(expanded, resultType); | ||
} catch (JsonLdError e) { | ||
throw new JsonLdDeserializationException("Unable to expand the input JSON.", e); | ||
} | ||
} | ||
|
||
private Object parseJsonObject(JsonParser parser) throws IOException { | ||
Object value = null; | ||
final JsonToken initialToken = parser.getCurrentToken(); | ||
parser.setCodec(mapper); | ||
if (initialToken == JsonToken.START_ARRAY) { | ||
value = parser.readValueAs(new TypeReference<List<?>>() { | ||
}); | ||
} else if (initialToken == JsonToken.START_OBJECT) { | ||
value = parser.readValueAs(new TypeReference<Map<?, ?>>() { | ||
}); | ||
} else if (initialToken == JsonToken.VALUE_STRING) { | ||
value = parser.readValueAs(String.class); | ||
} else if (initialToken == JsonToken.VALUE_FALSE || initialToken == JsonToken.VALUE_TRUE) { | ||
value = parser.readValueAs(Boolean.class); | ||
} else if (initialToken == JsonToken.VALUE_NUMBER_FLOAT || initialToken == JsonToken.VALUE_NUMBER_INT) { | ||
value = parser.readValueAs(Number.class); | ||
} else if (initialToken == JsonToken.VALUE_NULL) { | ||
value = null; | ||
} | ||
return value; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/cz/cvut/kbss/jsonld/jackson/deserialization/JsonLdDeserializerModifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package cz.cvut.kbss.jsonld.jackson.deserialization; | ||
|
||
import com.fasterxml.jackson.databind.BeanDescription; | ||
import com.fasterxml.jackson.databind.DeserializationConfig; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.deser.BeanDeserializerModifier; | ||
import cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor; | ||
|
||
public class JsonLdDeserializerModifier extends BeanDeserializerModifier { | ||
|
||
@Override | ||
public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, | ||
JsonDeserializer<?> deserializer) { | ||
if (BeanAnnotationProcessor.isOwlClassEntity(beanDesc.getBeanClass())) { | ||
return new JacksonJsonLdDeserializer(deserializer, beanDesc.getBeanClass()); | ||
} | ||
return deserializer; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/test/java/cz/cvut/kbss/jsonld/jackson/environment/Environment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package cz.cvut.kbss.jsonld.jackson.environment; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
|
||
public class Environment { | ||
|
||
/** | ||
* Reads test data from the test resources classpath folder. | ||
* | ||
* @param fileName File to read | ||
* @return Content of the file | ||
*/ | ||
public static String readData(String fileName) { | ||
final File file = new File(Environment.class.getClassLoader().getResource(fileName).getFile()); | ||
assert file.exists(); | ||
try (final BufferedReader reader = new BufferedReader(new FileReader(file))) { | ||
final StringBuilder sb = new StringBuilder(); | ||
String line; | ||
while ((line = reader.readLine()) != null) { | ||
sb.append(line).append('\n'); | ||
} | ||
return sb.toString(); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Unable to read test file " + fileName, e); | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...va/cz/cvut/kbss/jsonld/jackson/environment/deserialization/JsonLdDeserializationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package cz.cvut.kbss.jsonld.jackson.environment.deserialization; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import cz.cvut.kbss.jsonld.jackson.JsonLdModule; | ||
import cz.cvut.kbss.jsonld.jackson.environment.Environment; | ||
import cz.cvut.kbss.jsonld.jackson.environment.model.Employee; | ||
import cz.cvut.kbss.jsonld.jackson.environment.model.User; | ||
import org.junit.Before; | ||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
|
||
import java.net.URI; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
public class JsonLdDeserializationTest { | ||
|
||
private ObjectMapper objectMapper; | ||
|
||
@Before | ||
public void setUp() { | ||
this.objectMapper = new ObjectMapper(); | ||
objectMapper.registerModule(new JsonLdModule()); | ||
} | ||
|
||
@Ignore | ||
@Test | ||
public void testDeserializeInstanceWithDataProperties() throws Exception { | ||
final String input = Environment.readData("objectWithDataProperties.json"); | ||
final User expected = new User(); | ||
expected.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld#787088083")); | ||
expected.setFirstName("Catherine"); | ||
expected.setLastName("Halsey"); | ||
expected.setUsername("halsey@unsc.org"); | ||
expected.setAdmin(true); | ||
final User result = objectMapper.readValue(input, User.class); | ||
assertNotNull(result); | ||
assertEquals(expected.getUri(), result.getUri()); | ||
assertEquals(expected.getFirstName(), result.getFirstName()); | ||
assertEquals(expected.getLastName(), result.getLastName()); | ||
assertEquals(expected.getUsername(), result.getUsername()); | ||
assertEquals(expected.getAdmin(), result.getAdmin()); | ||
} | ||
|
||
@Ignore | ||
@Test | ||
public void testDeserializeInstanceWithSingularObjectProperty() throws Exception { | ||
final String input = Environment.readData("objectWithSingularReference.json"); | ||
final Employee result = objectMapper.readValue(input, Employee.class); | ||
// TODO | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"@context": { | ||
"firstName": "http://xmlns.com/foaf/0.1/firstName", | ||
"lastName": "http://xmlns.com/foaf/0.1/lastName", | ||
"accountName": "http://xmlns.com/foaf/0.1/accountName", | ||
"isAdmin": "http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld/isAdmin" | ||
}, | ||
"@id": "http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld#787088083", | ||
"@type": [ | ||
"http://onto.fel.cvut.cz/ontologies/ufo/Person", | ||
"http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld/User" | ||
], | ||
"isAdmin": true, | ||
"accountName": "halsey@unsc.org", | ||
"firstName": "Catherine", | ||
"lastName": "Halsey" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"@context": { | ||
"firstName": "http://xmlns.com/foaf/0.1/firstName", | ||
"lastName": "http://xmlns.com/foaf/0.1/lastName", | ||
"accountName": "http://xmlns.com/foaf/0.1/accountName", | ||
"isAdmin": "http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld/isAdmin" | ||
}, | ||
"@id": "http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld#-697813743", | ||
"@type": [ | ||
"http://onto.fel.cvut.cz/ontologies/ufo/Person", | ||
"http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld/Employee", | ||
"http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld/User" | ||
], | ||
"isAdmin": false, | ||
"http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld/isMemberOf": { | ||
"@id": "http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld#-82218522", | ||
"@type": "http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld/Organization", | ||
"http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld/brand": "Brandy0", | ||
"http://purl.org/dc/terms/created": "Fri Jul 29 14:52:04 CEST 2016", | ||
"http://www.w3.org/2000/01/rdf-schema#label": "Organization291065182" | ||
}, | ||
"accountName": "user647104078", | ||
"firstName": "FirstName647104078", | ||
"lastName": "LastName647104078" | ||
} |