JSON-LD Module for Jackson
If you use maven, just add the dependency to your pom.xml
<dependency>
<groupId>com.io-informatics.oss</groupId>
<artifactId>jackson-jsonld</artifactId>
<version>0.0.5</version>
</dependency>
The first step is to register the module with jackson.
// this configure the JsonldModule with an empty default context.
objectMapper.registerModule(new JsonldModule());
If you want to provide a default JSON-LD context for your application check the other constructors of JsonldModule
Next, we can have annotated java beans which can be serialized using Jsonld. For instance:
@JsonldType("http://schema.org/Person")
public class Person {
@JsonldId
public String id;
@JsonldProperty("http://schema.org/name")
public String name;
@JsonldProperty("http://schema.org/jobTitle")
public String jobtitle;
@JsonldProperty("http://schema.org/url")
public String url;
}
Instances of Person can we wrapped inside a JsonldResource or JsonldGraph/HydraCollection. To do this you can use the builders that the library provides:
Person alex = new Person();
alex.id = "mailto:[email protected]";
alex.name = "Alex De Leon";
alex.jobtitle = "Software Developer";
alex.url = "http://alexdeleon.name";
objectMapper.writer().writeValue(System.out, JsonldResource.Builder.create().build(alex));
The above will generate the following JSON-LD representation:
{
"@context": {
"name": "http://schema.org/name",
"jobtitle": "http://schema.org/jobTitle",
"url": "http://schema.org/url"
},
"@type": "http://schema.org/Person",
"name": "Alex De Leon",
"jobtitle": "Software Developer",
"url": "http://alexdeleon.name",
"@id": "mailto:[email protected]"
}