Refine search
Object output = JsonLdProcessor.fromRDF(triplesDocument.toString()); JsonLdOptions jlo = new JsonLdOptions(); String result = JsonUtils.toString(JsonLdProcessor.compact(output, null, jlo)); reusableOutputValue.set(result); String id = key.toString().trim();
public void parse(final InputStream in, final String sourceURI) throws JsonLdError, IOException { _callback = new JsonLDtripleCallback(sourceURI); final JsonLdOptions options = new JsonLdOptions(sourceURI); JsonLdProcessor .toRDF(JsonUtils.fromInputStream(in), _callback, options); }
@Override public Object resolve(Context c, Map<String, String[]> parameters, Map<String, InputStream> dataStreams) throws JSONException { try { Object obj = getObj(c, parameters, dataStreams); if (obj == null) return null; // 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(new ByteArrayInputStream(obj.toString().getBytes())); // Create an instance of JsonLdOptions with the standard JSON-LD options JsonLdOptions options = new JsonLdOptions(); // Call whichever JSONLD function you want! (e.g. compact) Object compact = JsonLdProcessor.expand(jsonObject,options); // Print out the result (or don't, it's your call!) return new JSONArray(JsonUtils.toString(compact)); } catch (IOException ex) { Logger.getLogger(CruncherJsonLdExpand.class.getName()).log(Level.SEVERE, null, ex); } catch (JsonLdError ex) { Logger.getLogger(CruncherJsonLdExpand.class.getName()).log(Level.SEVERE, null, ex); } return null; }
@Override public Object resolve(Context c, Map<String, String[]> parameters, Map<String, InputStream> dataStreams) throws JSONException { try { String obj = getObjAsString(c, parameters, dataStreams); if (obj == null) return null; JsonLdProcessor.registerRDFParser("RDF/XML", new RdfXmlParser(true)); JsonLdOptions options = new JsonLdOptions(); options.outputForm=JsonLdConsts.COMPACTED; options.format="RDF/XML"; Object output = JsonLdProcessor.fromRDF(obj, options); return new JSONObject(JsonUtils.toString(output)); } catch (IOException ex) { Logger.getLogger(CruncherJsonLdExpand.class.getName()).log(Level.SEVERE, null, ex); } catch (JsonLdError ex) { Logger.getLogger(CruncherJsonLdExpand.class.getName()).log(Level.SEVERE, null, ex); } return null; }
final SesameRDFParser serialiser = new SesameRDFParser(); try { Object output = JsonLdProcessor.fromRDF(model, serialiser); final JsonLdOptions opts = new JsonLdOptions(); opts.setUseRdfType(getWriterConfig().get(JSONLDSettings.USE_RDF_TYPE)); opts.setUseNativeTypes(getWriterConfig().get(JSONLDSettings.USE_NATIVE_TYPES)); output = JsonLdProcessor.expand(output, opts); output = JsonLdProcessor.flatten(output, inframe, opts); localCtx.put("@context", ctx); output = JsonLdProcessor.compact(output, localCtx, opts); JsonUtils.writePrettyPrint(writer, output); } else { JsonUtils.write(writer, output);
options.setExpandContext(JsonUtils.fromInputStream(contextStream)); result = JsonLdProcessor.expand(input, options); } else if (testType.contains("jld:CompactTest")) { final InputStream contextStream = cl result = JsonLdProcessor.compact(input, contextJson, options); } else if (testType.contains("jld:FlattenTest")) { if (test.containsKey("context")) { result = JsonLdProcessor.flatten(input, contextJson, options); } else { result = JsonLdProcessor.flatten(input, options); result = JsonLdProcessor.frame(input, frameJson, options); } else if (testType.contains("jld:FromRDFTest")) { result = JsonLdProcessor.fromRDF(input, options); } else if (testType.contains("jld:ToRDFTest")) { options.format = JsonLdConsts.APPLICATION_NQUADS; result = JsonLdProcessor.toRDF(input, options); result = ((String) result).trim(); } else if (testType.contains("jld:NormalizeTest")) { options.format = JsonLdConsts.APPLICATION_NQUADS; result = JsonLdProcessor.normalize(input, options); result = ((String) result).trim(); } else {
@Test public void testFrame0002() throws IOException, JsonLdError { final Object frame = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0002-frame.jsonld")); final Object in = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0002-in.jsonld")); final JsonLdOptions opts = new JsonLdOptions(); opts.setCompactArrays(false); final Map<String, Object> frame2 = JsonLdProcessor.frame(in, frame, opts); final Object out = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0002-out.jsonld")); assertEquals(out, frame2); }
try { final long start = System.currentTimeMillis(); Object output = JsonLdProcessor.fromRDF(tc, serializer); output = JsonLdProcessor.expand(output, opts); output = JsonLdProcessor.flatten(output, inframe, opts); output = JsonLdProcessor.compact(output, localCtx, opts); JsonUtils.writePrettyPrint(writer, output); } else { JsonUtils.write(writer, output);
/** * Outputs the RDF dataset found in the given JSON-LD object, using the * default {@link JsonLdOptions}. * * @param input * the JSON-LD input. * @return A JSON-LD object. * @throws JsonLdError * If there is an error converting the dataset to JSON-LD. */ public static Object toRDF(Object input) throws JsonLdError { return toRDF(input, new JsonLdOptions("")); }
@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/")); }
/** * Converts an RDF dataset to JSON-LD, using the default * {@link JsonLdOptions}. * * @param dataset * a serialized string of RDF in a format specified by the format * option or an RDF dataset to convert. * @return The JSON-LD object represented by the given RDF dataset * @throws JsonLdError * If there was an error converting from RDF to JSON-LD */ public static Object fromRDF(Object dataset) throws JsonLdError { return fromRDF(dataset, new JsonLdOptions("")); }
@Override public void map(Writable key, Text value, Context context) throws IOException, InterruptedException { try { String valueString = value.toString(); JSONObject obj = new JSONObject(valueString); Object outobj = JsonLdProcessor.compact(JsonUtils.fromString(valueString), jsonLdContext, new JsonLdOptions("")); if(outobj instanceof Map && jsonLdContextURL != null) { Map outjsonobj = (Map) outobj; outjsonobj.put("@context", jsonLdContextURL); } outputText.set(JsonUtils.toString(outobj)); if (obj.has("uri")) { context.write(new Text(obj.getString("uri")), outputText); } else if (obj.has("@id")) { context.write(new Text(obj.getString("@id")), outputText); } else { context.write(new Text(obj.toString()), outputText); } }catch(Exception e) { LOG.error("something is wrong", e); } } }
Object obj = getObj(c, parameters, dataStreams); if (obj == null) return null; Object jsonObject = JsonUtils.fromInputStream(new ByteArrayInputStream(obj.toString().getBytes())); context = jsonObject; JsonLdOptions options = new JsonLdOptions(); Object compact = JsonLdProcessor.compact(jsonObject, context, options); return new JSONObject(JsonUtils.toString(compact)); } catch (IOException ex) { Logger.getLogger(CruncherJsonLdCompact.class.getName()).log(Level.SEVERE, null, ex);
/** * 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); }
@Test public void toRdfWithNamespace() throws Exception { final URL contextUrl = getClass().getResource("/custom/contexttest-0003.jsonld"); assertNotNull(contextUrl); final Object context = JsonUtils.fromURL(contextUrl, JsonUtils.getDefaultHttpClient()); assertNotNull(context); final JsonLdOptions options = new JsonLdOptions(); options.useNamespaces = true; final RDFDataset rdf = (RDFDataset) JsonLdProcessor.toRDF(context, 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")); }
try { Map<String, Object> rval = (Map<String, Object>) JsonLdProcessor .fromRDF(manifestFile, new JsonLdOptions(manifestURL) { rval = JsonLdProcessor.frame(rval, frame, new JsonLdOptions(manifestURL)); this.manifest = JsonLdProcessor.compact(rval, frame.get("@context"), new JsonLdOptions(manifestURL)); this.tests = (List<Map<String, Object>>) Obj.get(this.manifest, "mf:entries", "@list"); final Object rval = JsonUtils.fromString(manifestFile); if (rval instanceof Map) { this.manifest = (Map<String, Object>) rval;
/** * Return the current prefixes as a JSON-LD string. * * @return the current prefixes as a JSON-LD string * @throws IOException on any error */ public String getContextString() throws IOException { try { Object compact = JsonLdProcessor.compact( JsonUtils.fromString("{}"), context.getPrefixes(false), new JsonLdOptions()); return JsonUtils.toPrettyString(compact); } catch (Exception e) { throw new IOException(jsonldContextCreationError, e); } }
@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")); }
@Override public void parse(final Reader reader, final String baseURI) throws IOException, RDFParseException, RDFHandlerException { final SesameTripleCallback callback = new SesameTripleCallback(getRDFHandler(), valueFactory, getParserConfig(), getParseErrorListener()); final JsonLdOptions options = new JsonLdOptions(baseURI); options.useNamespaces = true; try { JsonLdProcessor.toRDF(JsonUtils.fromReader(reader), callback, options); } catch (final JsonLdError e) { throw new RDFParseException("Could not parse JSONLD", e); } catch (final JsonParseException e) { throw new RDFParseException("Could not parse JSONLD", e); } catch (final RuntimeException e) { if (e.getCause() != null && e.getCause() instanceof RDFParseException) { throw (RDFParseException) e.getCause(); } throw e; } }
public String convertJSONLD(String json) throws IOException, JsonLdError { JsonLdOptions jsonLdOptions = new JsonLdOptions(); jsonLdOptions.format = "application/nquads"; Object jsonObject = JsonUtils.fromString(json); return JsonLdProcessor.toRDF(jsonObject, jsonLdOptions).toString(); }