Examples for the second version of the article

This commit is contained in:
mdhtr 2020-07-12 23:29:55 +02:00
parent 57dcc48b92
commit 64dc02f18f
8 changed files with 112 additions and 109 deletions

View File

@ -1,40 +0,0 @@
package com.baeldung.jsonld.deserialization.jsonldjava.jackson;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.jsonldjava.core.JsonLdOptions;
import com.github.jsonldjava.core.JsonLdProcessor;
import com.github.jsonldjava.utils.JsonUtils;
public class JacksonDeserializationExample {
public static void main(String[] args) throws IOException {
String exampleJsonld ="{" +
" \"@context\": {" +
" \"@vocab\": \"http://schema.org/\"," +
" \"knows\": {" +
" \"@type\": \"@id\"" +
" }" +
" }," +
" \"@type\": \"Person\"," +
" \"@id\": \"http://example.com/person/1234\"," +
" \"name\": \"Example Name\"," +
" \"knows\": \"http://example.com/person/2345\"" +
"}";
Object jsonObject = JsonUtils.fromString(exampleJsonld);
Object compact = JsonLdProcessor.compact(jsonObject, new HashMap(), new JsonLdOptions());
String compactContent = JsonUtils.toString(compact);
System.out.println(JsonUtils.toPrettyString(compact));
ObjectMapper objectMapper = new ObjectMapper();
JacksonExamplePerson person = objectMapper.readValue(compactContent, JacksonExamplePerson.class);
System.out.println(person.id);
System.out.println(person.name);
System.out.println(person.knows.id);
}
}

View File

@ -1,15 +0,0 @@
package com.baeldung.jsonld.serialization.hydrajsonld;
import com.fasterxml.jackson.annotation.JsonProperty;
import de.escalon.hypermedia.hydra.mapping.Expose;
import de.escalon.hypermedia.hydra.mapping.Vocab;
@Vocab("http://example.com/vocab/")
@Expose("person")
public class HydraJsonldExamplePerson {
@JsonProperty("@id")
public String id;
@Expose("fullName")
public String name;
}

View File

@ -1,25 +0,0 @@
package com.baeldung.jsonld.serialization.jacksonjsonld;
import java.io.IOException;
import com.fasterxml.jackson.databind.ObjectMapper;
import ioinformarics.oss.jackson.module.jsonld.HydraCollection;
import ioinformarics.oss.jackson.module.jsonld.JsonldGraph;
import ioinformarics.oss.jackson.module.jsonld.JsonldModule;
import ioinformarics.oss.jackson.module.jsonld.JsonldResource;
public class JacksonJsonLdSerializationExample {
public static void main(String[] args) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JsonldModule());
JacksonJsonldExamplePerson person = new JacksonJsonldExamplePerson();
JsonldResource jsonldResource = JsonldResource.Builder.create()
.build(person);
System.out.println(objectMapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(jsonldResource));
}
}

View File

@ -1,17 +0,0 @@
package com.baeldung.jsonld.serialization.jacksonjsonld;
import ioinformarics.oss.jackson.module.jsonld.annotation.JsonldId;
import ioinformarics.oss.jackson.module.jsonld.annotation.JsonldLink;
import ioinformarics.oss.jackson.module.jsonld.annotation.JsonldNamespace;
import ioinformarics.oss.jackson.module.jsonld.annotation.JsonldProperty;
import ioinformarics.oss.jackson.module.jsonld.annotation.JsonldType;
@JsonldNamespace(name = "s", uri = "http://schema.org/")
@JsonldLink(rel = "s:knows", name = "knows", href = "http://example.com/person/2345")
@JsonldType("s:Person")
class JacksonJsonldExamplePerson {
@JsonldId
public String id = "http://example.com/person/1234";
@JsonldProperty("s:name")
public String name = "Example Name";
}

View File

@ -0,0 +1,41 @@
package com.baeldung.jsonld.deserialization.jsonldjava.jackson;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.IOException;
import java.util.HashMap;
import org.junit.jupiter.api.Test;
import com.baeldung.jsonld.deserialization.jsonldjava.jackson.Person.Link;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.jsonldjava.core.JsonLdOptions;
import com.github.jsonldjava.core.JsonLdProcessor;
import com.github.jsonldjava.utils.JsonUtils;
public class JacksonDeserializationUnitTest {
@Test
void givenAJsonLdObject_whenCompactIsUsedWithEmptyContext_thenItCanBeDeserializedIntoAJacksonAnnotatedJavaObject() throws IOException {
String inputJsonLd = "{\"@context\":{\"@vocab\":\"http://schema.org/\",\"knows\":{\"@type\":\"@id\"}},\"@type\":\"Person\",\"@id\":\"http://example.com/person/1234\",\"name\":\"Example Name\",\"knows\":\"http://example.com/person/2345\"}";
Object jsonObject = JsonUtils.fromString(inputJsonLd);
Object compact = JsonLdProcessor.compact(jsonObject, new HashMap(), new JsonLdOptions());
String compactContent = JsonUtils.toString(compact);
assertEquals("{\"@id\":\"http://example.com/person/1234\",\"@type\":\"http://schema.org/Person\",\"http://schema.org/knows\":{\"@id\":\"http://example.com/person/2345\"},\"http://schema.org/name\":\"Example Name\"}", compactContent);
ObjectMapper objectMapper = new ObjectMapper();
Person person = objectMapper.readValue(compactContent, Person.class);
Person expectedPerson = new Person();
expectedPerson.id = "http://example.com/person/1234";
expectedPerson.name = "Example Name";
expectedPerson.knows = new Link();
expectedPerson.knows.id = "http://example.com/person/2345";
assertEquals(expectedPerson.id, person.id);
assertEquals(expectedPerson.name, person.name);
assertEquals(expectedPerson.knows.id, person.knows.id);
}
}

View File

@ -4,7 +4,7 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
@JsonIgnoreProperties(ignoreUnknown = true)
public class JacksonExamplePerson {
public class Person {
@JsonProperty("@id")
public String id;
@JsonProperty("http://schema.org/name")
@ -12,7 +12,7 @@ public class JacksonExamplePerson {
@JsonProperty("http://schema.org/knows")
public Link knows;
public class Link {
public static class Link {
@JsonProperty("@id")
public String id;
}

View File

@ -1,8 +1,12 @@
package com.baeldung.jsonld.serialization.hydrajsonld;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
@ -11,23 +15,36 @@ import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
import com.fasterxml.jackson.databind.ser.std.BeanSerializerBase;
import de.escalon.hypermedia.hydra.mapping.Expose;
import de.escalon.hypermedia.hydra.mapping.Vocab;
import de.escalon.hypermedia.hydra.serialize.JacksonHydraSerializer;
public class HydraJsonldSerializationExample {
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.registerModule(getJacksonHydraSerializerModule());
public class HydraJsonldSerializationUnitTest {
@Vocab("http://example.com/vocab/")
@Expose("person")
static class Person {
@JsonProperty("@id")
public String id;
@Expose("fullName")
public String name;
}
HydraJsonldExamplePerson person = new HydraJsonldExamplePerson();
@Test
void givenAHydraJsonldAnnotatedObject_whenJacksonHydraSerializerIsUsed_thenAJsonLdDocumentIsGenerated() throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(getJacksonHydraSerializerModule());
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
Person person = new Person();
person.id = "http://example.com/person/1234";
person.name = "Example Name";
System.out.println(mapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(person));
String personJsonLd = objectMapper.writeValueAsString(person);
assertEquals("{\"@context\":{\"@vocab\":\"http://example.com/vocab/\",\"name\":\"fullName\"},\"@type\":\"person\",\"name\":\"Example Name\",\"@id\":\"http://example.com/person/1234\"}", personJsonLd);
}
public static SimpleModule getJacksonHydraSerializerModule() {
static SimpleModule getJacksonHydraSerializerModule() {
return new SimpleModule() {
@Override

View File

@ -0,0 +1,42 @@
package com.baeldung.jsonld.serialization.jacksonjsonld;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import ioinformarics.oss.jackson.module.jsonld.JsonldModule;
import ioinformarics.oss.jackson.module.jsonld.annotation.JsonldId;
import ioinformarics.oss.jackson.module.jsonld.annotation.JsonldLink;
import ioinformarics.oss.jackson.module.jsonld.annotation.JsonldNamespace;
import ioinformarics.oss.jackson.module.jsonld.annotation.JsonldProperty;
import ioinformarics.oss.jackson.module.jsonld.annotation.JsonldResource;
import ioinformarics.oss.jackson.module.jsonld.annotation.JsonldType;
public class JacksonJsonLdSerializationUnitTest {
@JsonldResource
@JsonldNamespace(name = "s", uri = "http://schema.org/")
@JsonldType("s:Person")
@JsonldLink(rel = "s:knows", name = "knows", href = "http://example.com/person/2345")
static class Person {
@JsonldId
public String id = "http://example.com/person/1234";
@JsonldProperty("s:name")
public String name = "Example Name";
}
@Test
void givenAJacksonJsonldAnnotatedObject_whenJsonldModuleIsUsed_thenAJsonLdDocumentIsGenerated() throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JsonldModule());
Person person = new Person();
String personJsonLd = objectMapper.writeValueAsString(person);
assertEquals(
"{\"@type\":\"s:Person\",\"@context\":{\"s\":\"http://schema.org/\",\"name\":\"s:name\",\"knows\":{\"@id\":\"s:knows\",\"@type\":\"@id\"}},\"name\":\"Example Name\",\"@id\":\"http://example.com/person/1234\",\"knows\":\"http://example.com/person/2345\"}",
personJsonLd);
}
}