Merge pull request #9557 from mdhtr/mdhtr/BAEL-1488_json-ld

BAEL-1488: JSON-LD
This commit is contained in:
Jonathan Cook 2020-08-02 10:49:40 +02:00 committed by GitHub
commit 1e0b6ded84
7 changed files with 266 additions and 0 deletions

View File

@ -49,6 +49,66 @@
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>com.io-informatics.oss</groupId>
<artifactId>jackson-jsonld</artifactId>
<version>0.1.1</version>
<exclusions>
<exclusion>
<artifactId>jackson-databind</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
</exclusion>
<exclusion>
<artifactId>jackson-annotations</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
</exclusion>
<exclusion>
<artifactId>jackson-core</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
</exclusion>
<exclusion>
<artifactId>jsonld-java</artifactId>
<groupId>com.github.jsonld-java</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>de.escalon.hypermedia</groupId>
<artifactId>hydra-jsonld</artifactId>
<version>0.4.2</version>
<exclusions>
<exclusion>
<artifactId>jackson-databind</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.jsonld-java</groupId>
<artifactId>jsonld-java</artifactId>
<version>0.13.0</version>
<exclusions>
<exclusion>
<artifactId>jackson-core</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
</exclusion>
<exclusion>
<artifactId>jackson-databind</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<properties>
<jsoniter.version>0.9.23</jsoniter.version>

View File

@ -0,0 +1,55 @@
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_thenItCanBeDeserializedWithJackson() 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

@ -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;
}
}

View File

@ -0,0 +1,65 @@
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.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.serialize.JacksonHydraSerializer;
public class HydraJsonldSerializationUnitTest {
@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;
}
}
});
}
};
}
}

View File

@ -0,0 +1,15 @@
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 Person {
@JsonProperty("@id")
public String id;
@Expose("fullName")
public String name;
}

View File

@ -0,0 +1,33 @@
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;
public class JacksonJsonLdSerializationUnitTest {
@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);
}
}

View File

@ -0,0 +1,19 @@
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.JsonldResource;
import ioinformarics.oss.jackson.module.jsonld.annotation.JsonldType;
@JsonldResource
@JsonldNamespace(name = "s", uri = "http://schema.org/")
@JsonldType("s:Person")
@JsonldLink(rel = "s:knows", name = "knows", href = "http://example.com/person/2345")
public class Person {
@JsonldId
public String id = "http://example.com/person/1234";
@JsonldProperty("s:name")
public String name = "Example Name";
}