@Test public void shouldDeserializedJSONLDWithContext() throws JsonParseException, JsonMappingException, JSONLDProcessingError { String json = "{`@context`:{`nao`:`http: PersonContact resource = JSONLDUtils.deserialize(json, PersonContact.class); Model metadata = resource.getModel(); URI profile = new URIImpl("urn:uuid:6c329b90-737d-46cd-ae2f-eb86cddea172"); URI email = new URIImpl("urn:uuid:4182dcd4-a510-4b0c-8145-271a3dcfe455"); assertEquals(profile, profile.asURI()); assertTrue("Must contain a nco:PersonContact resource", metadata.contains(profile, RDF.type, NCO.PersonContact)); assertEquals("Test Test", ModelUtils.findObject(metadata, profile, NAO.prefLabel).asLiteral().getValue()); assertTrue("Must contain a nco:EmailAddress resource", metadata.contains(email, RDF.type, NCO.EmailAddress)); assertEquals("test@email.com", ModelUtils.findObject(metadata, email, NCO.emailAddress).asLiteral().getValue()); }
public static <T extends Resource> T deserialize(Object object, Class<T> returnType) throws JsonParseException, JsonMappingException, JSONLDProcessingError { RDF2GoTripleCallback callback = new RDF2GoTripleCallback(); ModelSet modelSet = toRDF(object, callback); URI subject = null; ClosableIterator<Statement> statements = modelSet.findStatements(Variable.ANY, Variable.ANY, RDF.type, Variable.ANY); while (statements.hasNext()) { Statement statement = statements.next(); URI type = statement.getObject().asURI(); if (returnType.equals(guessClass(type))) { if (subject == null) { subject = statement.getSubject().asURI(); } else if (!subject.equals(statement.getSubject())) { throw new IllegalArgumentException("The following JSON contains more than one resource of type '" + returnType.getName() + "', please consider calling instead deserializeCollection(Object):\n" + JSONUtils.toString(object)); } } } statements.close(); if (subject == null) { throw new IllegalArgumentException("The following JSON does not contain any resource of type '" + returnType.getName() + "':\n" + JSONUtils.toString(object)); } Model resourceModel = RDF2Go.getModelFactory().createModel().open(); resourceModel.addAll(modelSet.iterator()); Resource resource = new Resource(resourceModel, subject, false); modelSet.close(); return (T) resource.castTo(returnType); }
JSONLDUtils { public static <T extends Resource> T deserialize(Object object, Class<T> returnType) throws JsonParseException, JsonMappingException, JSONLDProcessingError { RDF2GoTripleCallback callback = new RDF2GoTripleCallback(); ModelSet modelSet = toRDF(object, callback); URI subject = null; ClosableIterator<Statement> statements = modelSet.findStatements(Variable.ANY, Variable.ANY, RDF.type, Variable.ANY); while (statements.hasNext()) { Statement statement = statements.next(); URI type = statement.getObject().asURI(); if (returnType.equals(guessClass(type))) { if (subject == null) { subject = statement.getSubject().asURI(); } else if (!subject.equals(statement.getSubject())) { throw new IllegalArgumentException("The following JSON contains more than one resource of type '" + returnType.getName() + "', please consider calling instead deserializeCollection(Object):\n" + JSONUtils.toString(object)); } } } statements.close(); if (subject == null) { throw new IllegalArgumentException("The following JSON does not contain any resource of type '" + returnType.getName() + "':\n" + JSONUtils.toString(object)); } Model resourceModel = RDF2Go.getModelFactory().createModel().open(); resourceModel.addAll(modelSet.iterator()); Resource resource = new Resource(resourceModel, subject, false); modelSet.close(); return (T) resource.castTo(returnType); } }
JSONLDUtils { public static <T extends Resource> T deserialize(Object object, Class<T> returnType) throws JsonParseException, JsonMappingException, JSONLDProcessingError { RDF2GoTripleCallback callback = new RDF2GoTripleCallback(); ModelSet modelSet = toRDF(object, callback); URI subject = null; ClosableIterator<Statement> statements = modelSet.findStatements(Variable.ANY, Variable.ANY, RDF.type, Variable.ANY); while (statements.hasNext()) { Statement statement = statements.next(); URI type = statement.getObject().asURI(); if (returnType.equals(guessClass(type))) { if (subject == null) { subject = statement.getSubject().asURI(); } else if (!subject.equals(statement.getSubject())) { throw new IllegalArgumentException("The following JSON contains more than one resource of type '" + returnType.getName() + "', please consider calling instead deserializeCollection(Object):\n" + JSONUtils.toString(object)); } } } statements.close(); if (subject == null) { throw new IllegalArgumentException("The following JSON does not contain any resource of type '" + returnType.getName() + "':\n" + JSONUtils.toString(object)); } Model resourceModel = RDF2Go.getModelFactory().createModel().open(); resourceModel.addAll(modelSet.iterator()); Resource resource = new Resource(resourceModel, subject, false); modelSet.close(); return (T) resource.castTo(returnType); }  }
JSONLDUtils { public static <T extends Resource> T deserialize(Object object, Class<T> returnType) throws JsonParseException, JsonMappingException, JSONLDProcessingError { RDF2GoTripleCallback callback = new RDF2GoTripleCallback(); ModelSet modelSet = toRDF(object, callback); URI subject = null; ClosableIterator<Statement> statements = modelSet.findStatements(Variable.ANY, Variable.ANY, RDF.type, Variable.ANY); while (statements.hasNext()) { Statement statement = statements.next(); URI type = statement.getObject().asURI(); if (returnType.equals(guessClass(type))) { if (subject == null) { subject = statement.getSubject().asURI(); } else if (!subject.equals(statement.getSubject())) { throw new IllegalArgumentException("The following JSON contains more than one resource of type '" + returnType.getName() + "', please consider calling instead deserializeCollection(Object):\n" + JSONUtils.toString(object)); } } } statements.close(); if (subject == null) { throw new IllegalArgumentException("The following JSON does not contain any resource of type '" + returnType.getName() + "':\n" + JSONUtils.toString(object)); } Model resourceModel = RDF2Go.getModelFactory().createModel().open(); resourceModel.addAll(modelSet.iterator()); Resource resource = new Resource(resourceModel, subject, false); modelSet.close(); return (T) resource.castTo(returnType); }  static T deserialize(Object object, Class<T> returnType); static T deserialize(String jsonObject, Class<T> returnType); @Deprecated static List<? extends Resource> deserializeCollection(List<Object> collection); @Deprecated static List<? extends Resource> deserializeCollection(String jsonArray); static Object serialize(T resource); static Object serialize(Map<String, String> prefixes, T resource); static String serializeAsString(T resource); static String serializeAsString(Map<String, String> prefixes, T resource); @Deprecated static List<Object> serializeCollection(T... collection); @Deprecated static List<Object> serializeCollection(Map<String, String> prefixes,
			T... collection); }
JSONLDUtils { public static <T extends Resource> T deserialize(Object object, Class<T> returnType) throws JsonParseException, JsonMappingException, JSONLDProcessingError { RDF2GoTripleCallback callback = new RDF2GoTripleCallback(); ModelSet modelSet = toRDF(object, callback); URI subject = null; ClosableIterator<Statement> statements = modelSet.findStatements(Variable.ANY, Variable.ANY, RDF.type, Variable.ANY); while (statements.hasNext()) { Statement statement = statements.next(); URI type = statement.getObject().asURI(); if (returnType.equals(guessClass(type))) { if (subject == null) { subject = statement.getSubject().asURI(); } else if (!subject.equals(statement.getSubject())) { throw new IllegalArgumentException("The following JSON contains more than one resource of type '" + returnType.getName() + "', please consider calling instead deserializeCollection(Object):\n" + JSONUtils.toString(object)); } } } statements.close(); if (subject == null) { throw new IllegalArgumentException("The following JSON does not contain any resource of type '" + returnType.getName() + "':\n" + JSONUtils.toString(object)); } Model resourceModel = RDF2Go.getModelFactory().createModel().open(); resourceModel.addAll(modelSet.iterator()); Resource resource = new Resource(resourceModel, subject, false); modelSet.close(); return (T) resource.castTo(returnType); }  static T deserialize(Object object, Class<T> returnType); static T deserialize(String jsonObject, Class<T> returnType); @Deprecated static List<? extends Resource> deserializeCollection(List<Object> collection); @Deprecated static List<? extends Resource> deserializeCollection(String jsonArray); static Object serialize(T resource); static Object serialize(Map<String, String> prefixes, T resource); static String serializeAsString(T resource); static String serializeAsString(Map<String, String> prefixes, T resource); @Deprecated static List<Object> serializeCollection(T... collection); @Deprecated static List<Object> serializeCollection(Map<String, String> prefixes,
			T... collection);  }