/** * Set the JSON-LD java API's options * * If not set, a default value is used. (Note that this default * is not the same as the one used by JSON-LD java API). * * @param opts the options as defined by the JSON-LD java API */ public void setOptions(JsonLdOptions opts) { set(JsonLDWriter.JSONLD_OPTIONS, opts); }
private void writeJsonLd(final OutputStream output, final DatasetGraph graph, final IRI... profiles) { final String profile = getCustomJsonLdProfile(profiles); final RDFFormat format = canUseCustomJsonLdProfile(profile) ? JSONLD_COMPACT_FLAT : getJsonLdProfile(profiles); final WriterDatasetRIOT writer = RDFDataMgr.createDatasetWriter(format); final PrefixMap pm = RiotLib.prefixMap(graph); final String base = null; final JsonLDWriteContext ctx = new JsonLDWriteContext(); if (canUseCustomJsonLdProfile(profile)) { LOGGER.debug("Setting JSON-LD context with profile: {}", profile); final String c = cache.get(profile, p -> { try (final TypedInputStream res = HttpOp.execHttpGet(profile)) { return IOUtils.toString(res.getInputStream(), UTF_8); } catch (final IOException | HttpException ex) { LOGGER.warn("Error fetching profile {}: {}", p, ex.getMessage()); return null; } }); if (nonNull(c)) { ctx.setJsonLDContext(c); ctx.setJsonLDContextSubstitution("\"" + profile + "\""); } } writer.write(output, graph, pm, base, ctx); }
/** * Test using a context to compute the output, and replacing the @context with a given value */ @Test public void testSubstitutingContext() { Model m = ModelFactory.createDefaultModel(); String ns = "http://schema.org/"; Resource person = m.createResource(ns + "Person"); Resource s = m.createResource(); m.add(s, m.createProperty(ns + "name"), "Jane Doe"); m.add(s, m.createProperty(ns + "url"), "http://www.janedoe.com"); m.add(s, m.createProperty(ns + "jobTitle"), "Professor"); m.add(s, RDF.type, person); // change @context to a URI JsonLDWriteContext jenaCtx = new JsonLDWriteContext(); jenaCtx.setJsonLDContextSubstitution((new JsonString(ns)).toString()); String jsonld; jsonld = toString(m, RDFFormat.JSONLD_COMPACT_FLAT, jenaCtx); String c = "\"@context\":\"http://schema.org/\""; assertTrue(jsonld.contains(c)); // change @context to a given ctx String ctx = "{\"jobTitle\":{\"@id\":\"http://ex.com/jobTitle\"},\"url\":{\"@id\":\"http://ex.com/url\"},\"name\":{\"@id\":\"http://ex.com/name\"}}"; jenaCtx.setJsonLDContextSubstitution(ctx); jsonld = toString(m, RDFFormat.JSONLD_COMPACT_FLAT, jenaCtx); assertTrue(jsonld.contains("http://ex.com/name")); }
JsonLDWriteContext jenaCtx = new JsonLDWriteContext(); jenaCtx.setJsonLDContext(js); jenaCtx.setJsonLDContext(js);
JsonLDWriteContext jenaContext = new JsonLDWriteContext(); try { jenaContext.set(JsonLDWriter.JSONLD_CONTEXT, "{\"@context\" : \"http://schema.org/\"}"); String jsonld = toString(m, RDFFormat.JSONLD, jenaContext);
/** Test passing a JsonLdOptions through Context */ @Test public final void jsonldOptions() { Model m = ModelFactory.createDefaultModel(); String ns = "http://schema.org/"; Resource s = m.createResource(); m.add(s, m.createProperty(ns + "name"), "Jane Doe"); m.add(s, m.createProperty(ns + "url"), "http://www.janedoe.com"); m.add(s, m.createProperty(ns + "jobTitle"), "Professor"); // our default uses true for compactArrays String jsonld = toString(m, RDFFormat.JSONLD, null); // compactArrays is true -> no "@graph" assertFalse(jsonld.contains("@graph")); // compactArrays is true -> string, not an array for props with one value assertTrue(jsonld.contains("\"jobTitle\" : \"Professor\"")); // now output using a value for JsonLdOptions in Context that sets compactArrays to false JsonLDWriteContext jenaCtx = new JsonLDWriteContext(); JsonLdOptions opts = new JsonLdOptions(null); opts.setCompactArrays(false); jenaCtx.setOptions(opts); jsonld = toString(m, RDFFormat.JSONLD, jenaCtx); // compactArrays is false -> a "@graph" node assertTrue(jsonld.contains("@graph")); // compactArrays is false -> an array for all props, even when there's only one value assertTrue(jsonld.contains("\"jobTitle\" : [ \"Professor\" ]")); }
private void writeJsonLd(final OutputStream output, final DatasetGraph graph, final IRI... profiles) { final String profile = getCustomJsonLdProfile(profiles); final RDFFormat format = canUseCustomJsonLdProfile(profile) ? JSONLD_COMPACT_FLAT : getJsonLdProfile(profiles); final WriterDatasetRIOT writer = RDFDataMgr.createDatasetWriter(format); final PrefixMap pm = RiotLib.prefixMap(graph); final String base = null; final JsonLDWriteContext ctx = new JsonLDWriteContext(); if (canUseCustomJsonLdProfile(profile)) { LOGGER.debug("Setting JSON-LD context with profile: {}", profile); final String c = cache.get(profile, p -> { try (final TypedInputStream res = HttpOp.execHttpGet(profile)) { return IOUtils.toString(res.getInputStream(), UTF_8); } catch (final IOException | HttpException ex) { LOGGER.warn("Error fetching profile {}: {}", p, ex.getMessage()); return null; } }); if (nonNull(c)) { ctx.setJsonLDContext(c); ctx.setJsonLDContextSubstitution("\"" + profile + "\""); } } writer.write(output, graph, pm, base, ctx); }
/** * Set the frame used in a "Frame" output * @param frame the Json Object used as frame for the "frame" output */ public void setFrame(String frame) { set(JsonLDWriter.JSONLD_FRAME, frame); }
/** * Set the frame used in a "Frame" output * @param frame the frame Object expected by the JSON-LD java API */ public void setFrame(Object frame) { set(JsonLDWriter.JSONLD_FRAME, frame); }
/** * Set the value of the JSON-LD "@context" node, used in "Compact" and "Flattened" JSON-LD outputs. * * Only useful for "Compact" and "Flattened" JSON-LD outputs, and not required: if not set, * a value for the "@Context" node is computed, based on the content of the dataset and its prefix mappings. * * @param jsonLdContext the value of the "@context" node (a JSON value). Note that the use of an URI to pass an external context is not supported (as of JSONLD-java API 0.8.3) * @see #setJsonLDContextSubstitution(String) for a way to overcome this problem. * * @see #setJsonLDContext(Object) */ public void setJsonLDContext(String jsonLdContext) { set(JsonLDWriter.JSONLD_CONTEXT, jsonLdContext); }
/** * Allow to replace the content of the "@context" node with a given value. * * This is useful, for instance, to allow to set the @content in the output to an URI, such as "@context": "http://schema.org/" * Note that the actual content at this URI is NOT used when computing the output. * The context used to compute the JSONLD output is the one normally used (as defined by a call to - * or the lack of call to - setJsonLdContext) * * Only useful for "Compact" and "Flattened" JSON-LD outputs, and not required * * @param jsonLdContext the value of the "@context" node. Note the string is supposed to be a JSON Value: if passing an URI, the String must be quoted. */ public void setJsonLDContextSubstitution(String jsonLdContext) { set(JsonLDWriter.JSONLD_CONTEXT_SUBSTITUTION, jsonLdContext); }
/** * Set the value of the JSON-LD "@context" node, used in "Compact" and "Flattened" JSON-LD outputs. * * Only useful for "Compact" and "Flattened" JSON-LD outputs, and not required: if not set, * a value for the "@Context" node is computed, based on the content of the dataset and its prefix mappings. * * @param jsonLdContext the context as expected by JSON-LD java API. As of JSON-LD java 0.8.3, a Map * defining the properties and the prefixes is OK. Note that the use an URI to pass an external context is not supported (JSONLD-java RDF 0.8.3) * @see #setJsonLDContextSubstitution(String) for a way to overcome this problem. * * @see #setJsonLDContext(String) */ public void setJsonLDContext(Object jsonLdContext) { set(JsonLDWriter.JSONLD_CONTEXT, jsonLdContext); }