Examples for the second version of the article
This commit is contained in:
+41
@@ -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);
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.jsonld.deserialization.jsonldjava.jackson;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class Person {
|
||||
@JsonProperty("@id")
|
||||
public String id;
|
||||
@JsonProperty("http://schema.org/name")
|
||||
public String name;
|
||||
@JsonProperty("http://schema.org/knows")
|
||||
public Link knows;
|
||||
|
||||
public static class Link {
|
||||
@JsonProperty("@id")
|
||||
public String id;
|
||||
}
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
package com.baeldung.jsonld.serialization.hydrajsonld;
|
||||
|
||||
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;
|
||||
import com.fasterxml.jackson.databind.SerializationConfig;
|
||||
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 HydraJsonldSerializationUnitTest {
|
||||
@Vocab("http://example.com/vocab/")
|
||||
@Expose("person")
|
||||
static class Person {
|
||||
@JsonProperty("@id")
|
||||
public String id;
|
||||
@Expose("fullName")
|
||||
public String name;
|
||||
}
|
||||
|
||||
@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";
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
static SimpleModule getJacksonHydraSerializerModule() {
|
||||
return new SimpleModule() {
|
||||
|
||||
@Override
|
||||
public void setupModule(SetupContext context) {
|
||||
super.setupModule(context);
|
||||
|
||||
context.addBeanSerializerModifier(new BeanSerializerModifier() {
|
||||
|
||||
@Override
|
||||
public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescription beanDesc, JsonSerializer<?> serializer) {
|
||||
|
||||
if (serializer instanceof BeanSerializerBase) {
|
||||
return new JacksonHydraSerializer((BeanSerializerBase) serializer);
|
||||
} else {
|
||||
return serializer;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+42
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user