Skip to content
Martin Ledvinka edited this page Dec 28, 2021 · 2 revisions

Java Binding for JSON-LD (JB4JSON-LD) is a lightweight library for marshalling/unmarshalling Java objects to/from JSON-LD - a JSON extension for Linked Data.

It works by taking instances of classes annotated by JOPA and connecting them with corresponding attributes and objects in JSON-LD. That is, Java attributes annotated with OWLObjectProperty, OWLAnnotationProperty and OWLDataProperty are mapped to JSON-LD attributes with the corresponding identifiers. Instance identifier is mapped to an @id attribute and the instance's types (both OWLClass and Types-based) are mapped to the content of the @type attribute.

Project Javadoc can be found here.

How to Use

The library is split into the core implementation module (this repository) and a module integrating it with existing JSON serialization libraries (currently JB4JSON-LD Jackson). This allows us to leverage the existing JSON processing tools and keep the library as small as possible.

The easiest way to use the library is with Maven (or Gradle). Add a dependency to JB4JSON-LD to your project:

<dependency>
    <groupId>cz.cvut.kbss.jsonld</groupId>
    <artifactId>jb4jsonld-jackson</artifactId>
    <!-- <artifactId>jb4jsonld</artifactId> This one is transitively imported if you use the integration libraries -->
</dependency>

Example

Java

@OWLClass(iri = "http://onto.fel.cvut.cz/ontologies/ufo/Person")
public class User {

    @Id
    public URI uri;

    @OWLDataProperty(iri = "http://xmlns.com/foaf/0.1/firstName")
    private String firstName;

    @OWLDataProperty(iri = "http://xmlns.com/foaf/0.1/lastName")
    private String lastName;
    
    @OWLDataProperty(iri = "http://xmlns.com/foaf/0.1/accountName")
    private String username;

    @OWLDataProperty(iri = "http://krizik.felk.cvut.cz/ontologies/jb4jsonld/role")
    private Role role;  // Role is an enum

    @Properties
    private Map<String, Set<String>> properties;
    
    // Getters and setters follow
}

JSON-LD

{
  "@context": {
    "firstName": "http://xmlns.com/foaf/0.1/firstName",
    "lastName": "http://xmlns.com/foaf/0.1/lastName",
    "accountName": "http://xmlns.com/foaf/0.1/accountName",
    "isAdmin": "http://krizik.felk.cvut.cz/ontologies/jb4jsonld/isAdmin",
    "role": "http://krizik.felk.cvut.cz/ontologies/jb4jsonld/role"
  },
  "@id": "http://krizik.felk.cvut.cz/ontologies/jb4jsonld#Catherine+Halsey",
  "@type": [
    "http://onto.fel.cvut.cz/ontologies/ufo/Person",
    "http://krizik.felk.cvut.cz/ontologies/jb4jsonld/User",
    "http://onto.fel.cvut.cz/ontologies/ufo/Agent"
  ],
  "isAdmin": true,
  "accountName": "halsey@unsc.org",
  "firstName": "Catherine",
  "lastName": "Halsey",
  "role": "USER"
}