Refine search
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); }
@Activate protected void activate(ComponentContext ctx) { opts = new JsonLdOptions(); @SuppressWarnings("unchecked") Dictionary<String,Object> config = ctx.getProperties(); //boolean properties opts.setUseRdfType(getState(config.get(PROP_USE_RDF_TYPE), false)); opts.setUseNativeTypes(getState(config.get(PROP_USE_NATIVE_TYPES), false)); prettyPrint = getState(config.get(PROP_PRETTY_PRINT),true); //parse the string mode Object value = config.get(PROP_MODE); mode = value == null ? null : value.toString(); }
/** * Returns a new (mutable) JSON-LD options instance based on this context. */ public JsonLdOptions jsonLdOptions() { // TODO Technically both the documentLoader and potentially expandContext are mutable JsonLdOptions options = new JsonLdOptions(); options.setDocumentLoader(documentLoader); options.setBase(base); options.setExpandContext(expandContext); options.setOmitDefault(Boolean.TRUE); return options; }
static private JsonLdOptions defaultJsonLdOptions(String baseURI) { JsonLdOptions opts = new JsonLdOptions(baseURI); opts.useNamespaces = true ; // this is NOT jsonld-java's default // opts.setUseRdfType(true); // false -> use "@type" opts.setUseNativeTypes(true); // this is NOT jsonld-java's default opts.setCompactArrays(true); // this is jsonld-java's default return opts; }
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); JsonUtils.writePrettyPrint(writer, output); } else { JsonUtils.write(writer, output);
@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")); }
final JsonLdOptions options = new JsonLdOptions( "http://json-ld.org/test-suite/tests/" + test.get("input")); final TestDocumentLoader testLoader = new TestDocumentLoader( "http://json-ld.org/test-suite/tests/"); options.setDocumentLoader(testLoader); if (test.containsKey("option")) { final Map<String, Object> test_opts = (Map<String, Object>) test.get("option"); if (test_opts.containsKey("base")) { options.setBase((String) test_opts.get("base")); options.setExpandContext(JsonUtils.fromInputStream(contextStream)); options.setCompactArrays((Boolean) test_opts.get("compactArrays")); options.setUseNativeTypes((Boolean) test_opts.get("useNativeTypes")); options.setUseRdfType((Boolean) test_opts.get("useRdfType")); options.setProcessingMode((String) test_opts.get("processingMode")); options.setOmitGraph((Boolean) test_opts.get("omitGraph")); options.setProduceGeneralizedRdf((Boolean) test_opts.get("produceGeneralizedRdf")); result = JsonLdProcessor.flatten(input, contextJson, options);
@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); }
Object jsonObject = JsonUtils.fromInputStream(new ByteArrayInputStream(obj.toString().getBytes())); JsonLdOptions options = new JsonLdOptions(); options.setExpandContext(context); options.format = JsonLdConsts.APPLICATION_NQUADS; String rdfString = (String)JsonLdProcessor.toRDF(jsonObject, options); return rdfString;
/** * 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("")); }
final RDFDataset inputRdf = new RDFDataset(); final String ns = "http://www.example.com/foo/"; inputRdf.setNamespace("ex", ns); inputRdf.addTriple(ns + "s", ns + "p", ns + "o"); inputRdf.addTriple(ns + "s", ns + "p", ns + "o"); final JsonLdOptions options = new JsonLdOptions(); options.useNamespaces = true; final Object fromRDF1 = JsonLdProcessor.compact(new JsonLdApi(options).fromRDF(inputRdf), inputRdf.getContext(), options); final String jsonld1 = JsonUtils.toPrettyString(fromRDF1); final Object fromRDF2 = JsonLdProcessor.compact( new JsonLdApi(options).fromRDF(inputRdf, true), inputRdf.getContext(), options); final String jsonld2 = JsonUtils.toPrettyString(fromRDF2);
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();
/** * 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); } } }
/** * Instantiate a JsonLdService object */ public JsonLdServiceImpl() { options = new JsonLdOptions(); }
Collections.shuffle(potentialPredicates, prng); final RDFDataset testData = new RDFDataset(); final JsonLdOptions options = new JsonLdOptions(); final JsonLdApi jsonLdApi = new JsonLdApi(options); final int[] hashCodes = new int[rounds]; final JsonLdOptions optionsLax = new JsonLdOptions(); final JsonLdApi jsonLdApiLax = new JsonLdApi(optionsLax); final int[] hashCodesLax = new int[rounds]; final JsonLdOptions options2 = new JsonLdOptions(); final JsonLdApi jsonLdApi2 = new JsonLdApi(options2); final LongSummaryStatistics statsFirst5000Part2 = new LongSummaryStatistics(); final JsonLdOptions options3 = new JsonLdOptions(); final JsonLdApi jsonLdApi3 = new JsonLdApi(options3); final LongSummaryStatistics statsFirst5000Part3 = new LongSummaryStatistics(); final JsonLdOptions options4 = new JsonLdOptions(); final JsonLdApi jsonLdApi4 = new JsonLdApi(options4); final LongSummaryStatistics statsFirst5000Part4 = new LongSummaryStatistics();
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); }
@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; }
@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); } }