在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:jsonld-java/jsonld-java开源软件地址:https://github.com/jsonld-java/jsonld-java开源编程语言:Java 100.0%开源软件介绍:JSONLD-Java is looking for a maintainer JSONLD-JAVAThis is a Java implementation of the JSON-LD 1.0 specification and the JSON-LD-API 1.0 specification. USAGEFrom Maven
Code example// Open a valid json(-ld) input file
InputStream inputStream = new FileInputStream("input.json");
// Read the file into an Object (The type of this object will be a List, Map, String, Boolean,
// Number or null depending on the root object in the file).
Object jsonObject = JsonUtils.fromInputStream(inputStream);
// Create a context JSON map containing prefixes and definitions
Map context = new HashMap();
// Customise context...
// Create an instance of JsonLdOptions with the standard JSON-LD options
JsonLdOptions options = new JsonLdOptions();
// Customise options...
// Call whichever JSONLD function you want! (e.g. compact)
Object compact = JsonLdProcessor.compact(jsonObject, context, options);
// Print out the result (or don't, it's your call!)
System.out.println(JsonUtils.toPrettyString(compact)); Processor optionsThe Options specified by the JSON-LD API Specification are accessible via the Controlling network trafficParsing JSON-LD will normally follow any external JSONLD-Java uses the Apache HTTPComponents Client for these network connections,
based on the SystemDefaultHttpClient which reads
standard Java properties like 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 classpathYour application might be parsing JSONLD documents which always use the same
external 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 The syntax of [
{
"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 The Additional HTTP headers (such as Unless overridden in
The mechanism for loading 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: System.setProperty("com.github.jsonldjava.disallowRemoteContextLoading", "true"); You can also use the constant provided in DocumentLoader for the same purpose: 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 stringYour application might be parsing JSONLD documents which reference external // Inject a context document into the options as a literal string
DocumentLoader dl = new DocumentLoader();
JsonLdOptions options = new JsonLdOptions();
// ... the contents of "contexts/example.jsonld"
String jsonContext = "{ \"@context\": { ... } }";
dl.addInjectedDoc("http://www.example.com/context", jsonContext);
options.setDocumentLoader(dl);
InputStream inputStream = new FileInputStream("input.json");
Object jsonObject = JsonUtils.fromInputStream(inputStream);
Map context = new HashMap();
Object compact = JsonLdProcessor.compact(jsonObject, context, options);
System.out.println(JsonUtils.toPrettyString(compact)); Customizing the Apache HttpClientTo customize the HTTP behaviour (e.g. to disable the cache or provide
authentication
credentials),
you may want to create and configure your own Example of inserting a credential provider (e.g. to load a Object input = JsonUtils.fromInputStream(..);
DocumentLoader documentLoader = new DocumentLoader();
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope("localhost", 443),
new UsernamePasswordCredentials("username", "password"));
CacheConfig cacheConfig = CacheConfig.custom().setMaxCacheEntries(1000)
.setMaxObjectSize(1024 * 128).build();
CloseableHttpClient httpClient = CachingHttpClientBuilder
.create()
// allow caching
.setCacheConfig(cacheConfig)
// Wrap the local JarCacheStorage around a BasicHttpCacheStorage
.setHttpCacheStorage(
new JarCacheStorage(null, cacheConfig, new BasicHttpCacheStorage(
cacheConfig)))....
// Add in the credentials provider
.setDefaultCredentialsProvider(credsProvider);
// When you are finished setting the properties, call build
.build();
documentLoader.setHttpClient(httpClient);
JsonLdOptions options = new JsonLdOptions();
options.setDocumentLoader(documentLoader);
// .. and any other options
Object rdf = JsonLdProcessor.toRDF(input, options); PLAYGROUNDThe jsonld-java-tools repository contains a simple application which provides command line access to JSON-LD functions Initial clone and setupgit clone [email protected]:jsonld-java/jsonld-java-tools.git
chmod +x ./jsonldplayground Usagerun the following to get usage details: ./jsonldplayground --help For DevelopersCompiling & Packaging
Running testsmvn test or mvn test -pl core to run only core package tests Code styleThe 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 RequestsOnce 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.0The 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 ReportImplementation Reports conforming to the JSON-LD Implementation Report document can be regenerated using the following command: mvn test -pl core -Dtest=JsonLdProcessorTest -Dreport.format=<format> Current possible values for Integration of JSONLD-Java with other Java packagesThis is the base package for JSONLD-Java. Integration with other Java packages are done in separate repositories. Existing integrationsCreating an integration moduleCreate a repository for your moduleCreate 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 moduleCreate pom.xml for your moduleHere is the basic outline for what your module's pom.xml should look like <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<groupId>com.github.jsonld-java</groupId>
<artifactId>jsonld-java-parent</artifactId>
<version>0.13.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>jsonld-java-{your module}</artifactId>
<version>0.13.4-SNAPSHOT</version>
<name>JSONLD Java :: {your module name}</name>
<description>JSON-LD Java integration module for {RDF Library your module integrates}</description>
<packaging>jar</packaging>
<developers>
<developer>
<name>{YOU}</name>
<email>{YOUR EMAIL ADDRESS}</email>
</developer>
</developers>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jsonld-java</artifactId>
<version>${project.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jsonld-java</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project> Make sure you edit the following:
Import into your favorite editorFor Example: Follow the first few steps in the section above to import the whole Create RDFParser ImplementationThe interface Create TripleCallback ImplementationThe interface Using your ImplementationsRDFParserA JSONLD RDF parser is a class that can parse your frameworks' RDF model and generate JSON-LD. There are two ways to use your Register your parser with the JSONLD.registerRDFParser("format/identifier", new YourRDFParser());
Object jsonld = JSONLD.fromRDF(yourInput, new Options("") {{ format = "format/identifier" }}); or pass an instance of your Object jsonld = JSONLD.fromRDF(yourInput, new YourRDFParser()); JSONLDTripleCallbackA JSONLD triple callback is a class that can populate your framework's RDF model from JSON-LD - being called for each triple (technically quad). Pass an instance of your Object yourOutput = JSONLD.toRDF(jsonld, new YourTripleCallback()); Integrate with your frameworkYour framework might have its own system of readers and writers, where you should register JSON-LD as a supported format. Remember that here the "parse" direction is opposite of above, a 'reader' may be a class that can parse JSON-LD and populate an RDF Graph. Write TestsIt's helpful to have a test or two for your implementations to make sure they work and continue to work with future versions. Write README.mdWrite a Submit your moduleOnce you've Alternatively, we can also host your repository in the jsonld-java organisation to give it more visibility. CHANGELOG2021-12-13
2021-03-06
2020-09-24
|