Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
J

jsonld-java

> 编程语言
Open source

JSON-LD implementation for Java

388 stars0 likes0 views
WebsiteGitHub

About

JSON-LD implementation for Java

JSONLD-Java is looking for a maintainer

JSONLD-JAVA

This is a Java implementation of the JSON-LD 1.0 specification and the JSON-LD-API 1.0 specification.

USAGE

From Maven

    com.github.jsonld-java
    jsonld-java
    0.13.5

Code example

…

Processor options

The Options specified by the JSON-LD API Specification are accessible via the com.github.jsonldjava.core.JsonLdOptions class, and each JsonLdProcessor.* function has an optional input to take an instance of this class.

Controlling network traffic

Parsing JSON-LD will normally follow any external @context declarations. Loading these contexts from the network may in some cases not be desirable, or might require additional proxy configuration or authentication.

JSONLD-Java uses the Apache HTTPComponents Client for these network connections, based on the SystemDefaultHttpClient which reads standard Java properties like http.proxyHost.

The default HTTP Client is wrapped with a CachingHttpClient to provide a small memory-based cache (1000 objects, max 128 kB each) of regularly accessed contexts.

Loading contexts from classpath

Your application might be parsing JSONLD documents which always use the same external @context IRIs. Although the default HTTP cache (see above) will avoid repeated downloading of the same contexts, your application would still initially be vulnerable to network connectivity.

To bypass this issue, and even facilitate parsing of such documents in an offline state, it is possible to provide a 'warmed' cache populated from the classpath, e.g. loaded from a JAR.

In your application, simply add a resource jarcache.json to the root of your classpath together with the JSON-LD contexts to embed. (Note that you might have to recursively embed any nested contexts).

The syntax of jarcache.json is best explained by example:

javascript
[
  {
    "Content-Location": "http://www.example.com/context",
    "X-Classpath": "contexts/example.jsonld",
    "Content-Type": "application/ld+json"
  },
  {
    "Content-Location": "http://data.example.net/other",
    "X-Classpath": "contexts/other.jsonld",
    "Content-Type": "application/ld+json"
  }
]

(See also core/src/test/resources/jarcache.json).

This will mean that any JSON-LD document trying to import the @context http://www.example.com/context will instead be given contexts/example.jsonld loaded as a classpath resource.

The X-Classpath location is an IRI reference resolved relative to the location of the jarcache.json - so if you have multiple JARs with a jarcache.json each, then the X-Classpath will be resolved within the corresponding JAR (minimizing any conflicts).

Additional HTTP headers (such as Content-Type above) can be included, although these are generally ignored by JSONLD-Java.

Unless overridden in jarcache.json, this Cache-Control header is automatically injected together with the current Date, meaning that the resource loaded from the JAR will effectively never expire (the real HTTP server will never be consulted by the Apache HTTP client):

Date: Wed, 19 Mar 2014 13:25:08 GMT
Cache-Control: max-age=2147483647

The mechanism for loading jarcache.json relies on Thread.currentThread().getContextClassLoader() to locate resources from the classpath - if you are running on a command line, within a framework (e.g. OSGi) or Servlet container (e.g. Tomcat) this should normally be set correctly. If not, try:

java
ClassLoader oldContextCL = Thread.currentThread().getContextClassLoader();
try { 
    Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
    JsonLdProcessor.expand(input);   // or any other JsonLd operation
} finally { 
    // Restore, in case the current thread was doing something else
    // with the context classloader before calling our method
    Thread.currentThread().setContextClassLoader(oldContextCL);
}

To disable all remote document fetching, when using the default DocumentLoader, set the following Java System Property to "true" using:

java
System.setProperty("com.github.jsonldjava.disallowRemoteContextLoading", "true");

You can also use the constant provided in DocumentLoader for the same purpose:

java
System.setProperty(DocumentLoader.DISALLOW_REMOTE_CONTEXT_LOADING, "true");

Note that if you override DocumentLoader you should also support this setting for consistency and security.

Loading contexts from a string

Your application might be parsing JSONLD documents which reference external @context IRIs that are not available as file URIs on the classpath. In this case, the jarcache.json approach will not work. Instead you can inject the literal context file strings through the JsonLdOptions object, as follows:

…

Customizing the Apache HttpClient

To customize the HTTP behaviour (e.g. to disable the cache or provide authentication credentials), you may want to create and configure your own CloseableHttpClient instance, which can be passed to a DocumentLoader instance using setHttpClient(). This document loader can then be inserted into JsonLdOptions using setDocumentLoader() and passed as an argument to JsonLdProcessor arguments.

Example of inserting a credential provider (e.g. to load a @context protected by HTTP Basic Auth):

…

PLAYGROUND

The jsonld-java-tools repository contains a simple application which provides command line access to JSON-LD functions

Initial clone and setup

bash
git clone [email protected]:jsonld-java/jsonld-java-tools.git
chmod +x ./jsonldplayground

Usage

run the following to get usage details:

bash
./jsonldplayground --help

For Developers

Compiling & Packaging

jsonld-java uses maven to compile. From the base jsonld-java module run mvn clean install to install the jar into your local maven repository.

Running tests

bash
mvn test

or

bash
mvn test -pl core

to run only core package tests

Code style

The JSONLD-Java project uses custom Eclipse formatting and cleanup style guides to ensure that Pull Requests are fairly simple to merge.

These guides can be found in the /conf directory and can be installed in Eclipse using "Properties>Java Code Style>Formatter", followed by "Properties>Java Code Style>Clean Up" for each of the modules making up the JSONLD-Java project.

If you don't use Eclipse, then don't worry, your pull requests can be cleaned up by a repository maintainer prior to merging, but it makes the initial check easier if the modified code uses the conventions.

Submitting Pull Requests

Once you have made a change to fix a bug or add a new feature, you should commit and push the change to your fork.

Then, you can open a pull request to merge your change into the master branch of the main repository.

Implementation Reports for JSONLD-Java conformance with JSONLD-1.0

The Implementation Reports documenting the conformance of JSONLD-Java with JSONLD-1.0 are available at:

https://github.com/jsonld-java/jsonld-java/tree/master/core/reports

Regenerating Implementation Report

Implementation Reports conforming to the JSON-LD Implementation Report document can be regenerated using the following command:

bash
mvn test -pl core -Dtest=JsonLdProcessorTest -Dreport.format=

Current possible values for `` include JSON-LD (application/ld+json or jsonld), NQuads (text/plain, nquads, ntriples, nq or nt) and Turtle (text/turtle, turtle or ttl). * can be used to generate reports in all available formats.

Integration of JSONLD-Java with other Java packages

This is the base package for JSONLD-Java. Integration with other Java packages are done in separate repositories.

Existing integrations

  • Eclipse RDF4J
  • Apache Jena
  • RDF2GO
  • Apache Clerezza

Creating an integration module

Create a repository for your module

Create a GitHub repository for your module under your user account, or have a JSONLD-Java maintainer create one in the jsonld-java organisation.

Create maven module

Create pom.xml for your module

Here is the basic outline for what your module's pom.xml should look like

…

Make sure you edit the following:

  • project/artifactId : set this to jsonld-java-{module id}, where {module id} usually represents the RDF library you're integrating (e.g. jsonld-java-jena)
  • project/name : set this to JSONLD Java :: {Module Name}, wher {module name} is usually the name of the RDF library you're integrating.
  • project/description
  • project/developers/developer/... : Give youself credit by filling in the developer field. At least put your `` in (see here for all available options).
  • project/dependencies/... : remember to add any dependencies your project needs

Import into your favorite editor

For Example: Follow the first few steps in the section above to import the whole jsonld-java project or only your new module into eclipse.

Create RDFParser Implementation

The interface com.github.jsonldjava.core.RDFParser is used to parse RDF from the library into the JSONLD-Java internal RDF format. See the documentation in RDFParser.java for details on how to implement this interface.

Create TripleCallback Implementation

The interface com.github.jsonldjava.core.JSONLDTripleCallback is used to generate a representation of the JSON-LD input in the RDF library. See the documentation in JSONLDTripleCallback.java for details on how to implement this interface.

Using your Implementations

RDFParser

A JSONLD RDF parser is a class that can parse your frameworks' RDF model and generate JSON-LD.

There are two ways to use your RDFParser implementation.

Register your parser with the JSONLD class and set options.format when you call fromRDF

java
JSONLD.registerRDFParser("format/identifier", new YourRDFParser());
Object jsonld = JSONLD.fromRDF(yourInput, new Options("") {{ format = "format/identifier" }});

or pass an instance of your RDFParser into the fromRDF function

java
Object jsonld = JSONLD.fromRDF(yourInput, new YourRDFParser());

JSONLDTripleCallback

A JSONLD triple callback is a class that can populate your framework's RDF model from JSON-LD - being calle

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Javajavajson-ldrdf

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言