diff --git a/json-2/src/main/java/com/baeldung/jsonld/deserialization/jsonldjava/jackson/JacksonDeserializationExample.java b/json-2/src/main/java/com/baeldung/jsonld/deserialization/jsonldjava/jackson/JacksonDeserializationExample.java deleted file mode 100644 index b80769b408..0000000000 --- a/json-2/src/main/java/com/baeldung/jsonld/deserialization/jsonldjava/jackson/JacksonDeserializationExample.java +++ /dev/null @@ -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); - } -} diff --git a/json-2/src/main/java/com/baeldung/jsonld/serialization/hydrajsonld/HydraJsonldExamplePerson.java b/json-2/src/main/java/com/baeldung/jsonld/serialization/hydrajsonld/HydraJsonldExamplePerson.java deleted file mode 100644 index 16b2199a73..0000000000 --- a/json-2/src/main/java/com/baeldung/jsonld/serialization/hydrajsonld/HydraJsonldExamplePerson.java +++ /dev/null @@ -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; -} diff --git a/json-2/src/main/java/com/baeldung/jsonld/serialization/jacksonjsonld/JacksonJsonLdSerializationExample.java b/json-2/src/main/java/com/baeldung/jsonld/serialization/jacksonjsonld/JacksonJsonLdSerializationExample.java deleted file mode 100644 index 6a1acee1b8..0000000000 --- a/json-2/src/main/java/com/baeldung/jsonld/serialization/jacksonjsonld/JacksonJsonLdSerializationExample.java +++ /dev/null @@ -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)); - } -} diff --git a/json-2/src/main/java/com/baeldung/jsonld/serialization/jacksonjsonld/JacksonJsonldExamplePerson.java b/json-2/src/main/java/com/baeldung/jsonld/serialization/jacksonjsonld/JacksonJsonldExamplePerson.java deleted file mode 100644 index a3161ba1d3..0000000000 --- a/json-2/src/main/java/com/baeldung/jsonld/serialization/jacksonjsonld/JacksonJsonldExamplePerson.java +++ /dev/null @@ -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"; -} diff --git a/json-2/src/test/java/com/baeldung/jsonld/deserialization/jsonldjava/jackson/JacksonDeserializationUnitTest.java b/json-2/src/test/java/com/baeldung/jsonld/deserialization/jsonldjava/jackson/JacksonDeserializationUnitTest.java new file mode 100644 index 0000000000..35a2613957 --- /dev/null +++ b/json-2/src/test/java/com/baeldung/jsonld/deserialization/jsonldjava/jackson/JacksonDeserializationUnitTest.java @@ -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); + } +} diff --git a/json-2/src/main/java/com/baeldung/jsonld/deserialization/jsonldjava/jackson/JacksonExamplePerson.java b/json-2/src/test/java/com/baeldung/jsonld/deserialization/jsonldjava/jackson/Person.java similarity index 88% rename from json-2/src/main/java/com/baeldung/jsonld/deserialization/jsonldjava/jackson/JacksonExamplePerson.java rename to json-2/src/test/java/com/baeldung/jsonld/deserialization/jsonldjava/jackson/Person.java index 2e9f38e665..0cb6d43336 100644 --- a/json-2/src/main/java/com/baeldung/jsonld/deserialization/jsonldjava/jackson/JacksonExamplePerson.java +++ b/json-2/src/test/java/com/baeldung/jsonld/deserialization/jsonldjava/jackson/Person.java @@ -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; } diff --git a/json-2/src/main/java/com/baeldung/jsonld/serialization/hydrajsonld/HydraJsonldSerializationExample.java b/json-2/src/test/java/com/baeldung/jsonld/serialization/hydrajsonld/HydraJsonldSerializationUnitTest.java similarity index 52% rename from json-2/src/main/java/com/baeldung/jsonld/serialization/hydrajsonld/HydraJsonldSerializationExample.java rename to json-2/src/test/java/com/baeldung/jsonld/serialization/hydrajsonld/HydraJsonldSerializationUnitTest.java index 3d3531b0b5..e5d1e35236 100644 --- a/json-2/src/main/java/com/baeldung/jsonld/serialization/hydrajsonld/HydraJsonldSerializationExample.java +++ b/json-2/src/test/java/com/baeldung/jsonld/serialization/hydrajsonld/HydraJsonldSerializationUnitTest.java @@ -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 diff --git a/json-2/src/test/java/com/baeldung/jsonld/serialization/jacksonjsonld/JacksonJsonLdSerializationUnitTest.java b/json-2/src/test/java/com/baeldung/jsonld/serialization/jacksonjsonld/JacksonJsonLdSerializationUnitTest.java new file mode 100644 index 0000000000..b539c73608 --- /dev/null +++ b/json-2/src/test/java/com/baeldung/jsonld/serialization/jacksonjsonld/JacksonJsonLdSerializationUnitTest.java @@ -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); + } +}