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 think you need is to register the module in jackson. The module constructor takes a function that returns the jsonld context of your application.
// this configure the JsonldModule with an empty default context.
objectMapper.registerModule(new JsonldModule());
If you want to provide a default JSONLD context for your application check the other constructors of JsonldModule
Now we can have annotated java beans which can 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/HydraCollections. 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 jsonld 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]"
}