Object expanded = new JsonLdApi().expand(context, jsonMap); String result = ((Map<String, Object>) expanded).keySet().iterator().next(); if (result != null) {
Object expanded = new JsonLdApi(opts).expand(activeCtx, input);
/** * Performs RDF dataset normalization on the given JSON-LD input. The output * is an RDF dataset unless the 'format' option is used. * * @param input * the JSON-LD input to normalize. * @param options * the options to use: [base] the base IRI to use. [format] the * format if output is a string: 'application/nquads' for * N-Quads. [loadContext(url, callback(err, url, result))] the * context loader. * @return The JSON-LD object * @throws JsonLdError * If there is an error normalizing the dataset. */ public static Object normalize(Object input, JsonLdOptions options) throws JsonLdError { final JsonLdOptions opts = new JsonLdOptions(options.getBase()); opts.format = null; final RDFDataset dataset = (RDFDataset) toRDF(input, opts); return new JsonLdApi(options).normalize(dataset); }
/** * Performs RDF dataset normalization on the given JSON-LD input. The output * is an RDF dataset unless the 'format' option is used. * * @param input * the JSON-LD input to normalize. * @param options * the options to use: [base] the base IRI to use. [format] the * format if output is a string: 'application/nquads' for * N-Quads. [loadContext(url, callback(err, url, result))] the * context loader. * @return The JSON-LD object * @throws JsonLdError * If there is an error normalizing the dataset. */ public static Object normalize(Object input, JsonLdOptions options) throws JsonLdError { final JsonLdOptions opts = new JsonLdOptions(options.getBase()); opts.format = null; final RDFDataset dataset = (RDFDataset) toRDF(input, opts); return new JsonLdApi(options).normalize(dataset); }
final JsonLdApi api = new JsonLdApi(expandedInput, options); final RDFDataset dataset = api.toRDF();
/** * Performs RDF dataset normalization on the given JSON-LD input. The output * is an RDF dataset unless the 'format' option is used. * * @param input * the JSON-LD input to normalize. * @param options * the options to use: [base] the base IRI to use. [format] the * format if output is a string: 'application/nquads' for * N-Quads. [loadContext(url, callback(err, url, result))] the * context loader. * @return The JSON-LD object * @throws JsonLdError * If there is an error normalizing the dataset. */ public static Object normalize(Object input, JsonLdOptions options) throws JsonLdError { final JsonLdOptions opts = new JsonLdOptions(options.getBase()); opts.format = null; final RDFDataset dataset = (RDFDataset) toRDF(input, opts); return new JsonLdApi(options).normalize(dataset); }
final JsonLdApi api = new JsonLdApi(expandedInput, options); final RDFDataset dataset = api.toRDF();
final JsonLdApi api = new JsonLdApi(expandedInput, options); final RDFDataset dataset = api.toRDF();
final Object rval = new JsonLdApi(options).fromRDF(dataset);
final Object rval = new JsonLdApi(options).fromRDF(dataset);
final Object rval = new JsonLdApi(options).fromRDF(dataset);
final List<Object> expandedFrame = expand(frame, opts); final JsonLdApi api = new JsonLdApi(expandedInput, opts); final List<Object> framed = api.frame(expandedInput, expandedFrame); final Context activeCtx = api.context.parse(((Map<String, Object>) frame).get("@context"));
Object obj = (new JsonLdApi(opts)).fromRDF(jsonldDataset, true); // true because we know that we don't have any duplicate in jsonldDataset
@Test public void testFrame0010() throws IOException, JsonLdError { final Object frame = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0010-frame.jsonld")); //{ // "@id": "http://example.com/main/id", // "http://www.w3.org/1999/02/22-rdf-syntax-ns#type": { // "@id": "http://example.com/rdf/id", // "http://www.w3.org/1999/02/22-rdf-syntax-ns#label": "someLabel" // } //} final RDFDataset ds = new RDFDataset(); ds.addTriple("http://example.com/main/id", "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", "http://example.com/rdf/id"); ds.addTriple("http://example.com/rdf/id", "http://www.w3.org/1999/02/22-rdf-syntax-ns#label", "someLabel", null, null); final JsonLdOptions opts = new JsonLdOptions(); opts.setProcessingMode(JsonLdOptions.JSON_LD_1_0); final Object in = new JsonLdApi(opts).fromRDF(ds, true); final Map<String, Object> frame2 = JsonLdProcessor.frame(in, frame, opts); final Object out = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0010-out.jsonld")); assertEquals(out, frame2); } }
@Test public void write() throws Exception { final Model model = ModelFactory.createDefaultModel(); final Resource resource = model.createResource("http://example.com/test"); final Property property = model.createProperty("http://example.com/value"); model.add(resource, property, "Test"); final JsonLdOptions options = new JsonLdOptions(); options.format = "application/ld+json"; final JenaRDFParser parser = new JenaRDFParser(); final RDFDataset dataset = parser.parse(model); final Object json = new JsonLdApi(options).fromRDF(dataset); final String jsonStr = JsonUtils.toPrettyString(json); // System.out.println(jsonStr); assertTrue(jsonStr.contains("@id")); assertTrue(jsonStr.contains("http://example.com/test")); assertTrue(jsonStr.contains("http://example.com/value")); assertTrue(jsonStr.contains("Test")); }
@Test public void modelToJsonLD() throws Exception { JenaJSONLD.init(); // Only needed once final Model model = ModelFactory.createDefaultModel(); final Resource resource = model.createResource("http://example.com/test"); final Property property = model.createProperty("http://example.com/value"); model.add(resource, property, "Test"); final JsonLdOptions options = new JsonLdOptions(); options.format = "application/ld+json"; final JsonLdApi api = new JsonLdApi(options); final RDFDataset dataset = new RDFDataset(api); final Object json = api.fromRDF(dataset); final String jsonStr = JsonUtils.toPrettyString(json); System.out.println(jsonStr); // [ { // "@id" : "http://example.com/test", // "http://example.com/value" : [ { // "@value" : "Test" // } ] // } ] } }
@Test public void prefixUsedToShortenPredicate() throws Exception { final RDFDataset inputRdf = new RDFDataset(); inputRdf.setNamespace("ex", "http://www.a.com/foo/"); inputRdf.addTriple("http://www.a.com/foo/s", "http://www.a.com/foo/p", "http://www.a.com/foo/o"); assertEquals("http://www.a.com/foo/", inputRdf.getNamespace("ex")); final JsonLdOptions options = new JsonLdOptions(); options.useNamespaces = true; final Object fromRDF = JsonLdProcessor.compact(new JsonLdApi(options).fromRDF(inputRdf), inputRdf.getContext(), options); final String toJSONLD = JsonUtils.toPrettyString(fromRDF); // System.out.println(toJSONLD); assertFalse("The lexicographically shortest URI was not chosen", toJSONLD.contains("http://www.a.com/foo/p")); }
@Test public void fromRdfWithNamespaceLexicographicallyShortestChosen2() throws Exception { final RDFDataset inputRdf = new RDFDataset(); inputRdf.setNamespace("aat", "http://vocab.getty.edu/aat/"); inputRdf.setNamespace("aatrev", "http://vocab.getty.edu/aat/rev/"); inputRdf.addTriple("http://vocab.getty.edu/aat/rev/5001065997", JsonLdConsts.RDF_TYPE, "http://vocab.getty.edu/aat/datatype"); final JsonLdOptions options = new JsonLdOptions(); options.useNamespaces = true; final Object fromRDF = JsonLdProcessor.compact(new JsonLdApi(options).fromRDF(inputRdf), inputRdf.getContext(), options); final RDFDataset rdf = (RDFDataset) JsonLdProcessor.toRDF(fromRDF, options); // System.out.println(rdf.getNamespaces()); assertEquals("http://vocab.getty.edu/aat/", rdf.getNamespace("aat")); assertEquals("http://vocab.getty.edu/aat/rev/", rdf.getNamespace("aatrev")); final String toJSONLD = JsonUtils.toPrettyString(fromRDF); // System.out.println(toJSONLD); assertFalse("The lexicographically shortest URI was not chosen", toJSONLD.contains("aat:rev/")); }
@Test public void fromRdfWithNamespaceLexicographicallyShortestChosen() throws Exception { final RDFDataset inputRdf = new RDFDataset(); inputRdf.setNamespace("aat", "http://vocab.getty.edu/aat/"); inputRdf.setNamespace("aat_rev", "http://vocab.getty.edu/aat/rev/"); inputRdf.addTriple("http://vocab.getty.edu/aat/rev/5001065997", JsonLdConsts.RDF_TYPE, "http://vocab.getty.edu/aat/datatype"); final JsonLdOptions options = new JsonLdOptions(); options.useNamespaces = true; final Object fromRDF = JsonLdProcessor.compact(new JsonLdApi(options).fromRDF(inputRdf), inputRdf.getContext(), options); final RDFDataset rdf = (RDFDataset) JsonLdProcessor.toRDF(fromRDF, options); // System.out.println(rdf.getNamespaces()); assertEquals("http://vocab.getty.edu/aat/", rdf.getNamespace("aat")); assertEquals("http://vocab.getty.edu/aat/rev/", rdf.getNamespace("aat_rev")); final String toJSONLD = JsonUtils.toPrettyString(fromRDF); // System.out.println(toJSONLD); assertTrue("The lexicographically shortest URI was not chosen", toJSONLD.contains("aat:rev/")); }