@Override protected Map<String, Object> applyOnce(Object input, JsonLdOptions options) throws JsonLdError { return JsonLdProcessor.compact(input, context, options); } }
@SuppressWarnings("unchecked") private String doCompact(final InputStream input, final Object context) throws IOException { try { final List<Map<String, Object>> document = ((List<Map<String, Object>>)fromInputStream(input)).stream() .filter(filterExport::test) .collect(Collectors.toList()); return JsonUtils.toString( JsonLdProcessor.compact(document, context, options)); } catch (final JsonLdError ex) { throw new RuntimeException("Error converting JsonLd", ex); } }
@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 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();
protected void getMetadataProperties(Map<String, Object> metadata, BiConsumer<Object, Object> properties) { // A BdioMetadata instance should effectively be expanded data so we need to compact it using the graph context JsonLdOptions opts = frame().context().jsonLdOptions(); Object expandContext = opts.getExpandContext(); opts.setExpandContext(null); Map<String, Object> metadataNode = JsonLdProcessor.compact(metadata, expandContext, opts); options().metadataLabel().ifPresent(type -> metadataNode.put(JsonLdConsts.TYPE, type)); if (metadata.size() == 1 && metadata.containsKey(JsonLdConsts.ID)) { // JSON-LD compaction will remove the "@id" if it is the only field, put it back metadataNode.put(JsonLdConsts.ID, metadata.get(JsonLdConsts.ID)); } // Use the regular node property extraction logic getNodeProperties(metadataNode, properties); }
jsonLd = jsonLd.replaceAll("\"http://schema\\.org\"", "\"http://schema.org/\""); Object jsonObject = JsonUtils.fromString(jsonLd); Map<String, Object> data = JsonLdProcessor.compact(jsonObject, new HashMap(), new JsonLdOptions()); String type = Objects.toString(data.get("@type"), null); if (type == null && data.get(SCHEMA_ATTR_ARTICLE) instanceof Map) {
output = JsonLdProcessor.compact(output, localCtx, opts);
output = JsonLdProcessor.compact(output, localCtx, opts);
@SuppressWarnings("unchecked") @Override public Tuple2<String, String> call(Tuple2<String, String> t) throws Exception { String key = t._1(); String value = t._2(); JSONObject obj = new JSONObject(value); Object outobj = JsonLdProcessor.compact(JsonUtils.fromString(value), context.getValue(), new JsonLdOptions("")); if(outobj instanceof Map) { @SuppressWarnings("rawtypes") Map outjsonobj = (Map) outobj; outjsonobj.put("@context", contextUrl); } value = JsonUtils.toString(outobj); if (obj.has("uri")) { key = obj.getString("uri"); } else if (obj.has("@id")) { key = obj.getString("@id"); } else { key = obj.toString(); } return new Tuple2<>(key, value); } });
Object compact = JsonLdProcessor.compact(jsonObject, context, options);
/** * 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); } }
return rval; } else if ("compacted".equals(options.outputForm)) { return compact(rval, dataset.getContext(), options); } else if ("flattened".equals(options.outputForm)) { return flatten(rval, dataset.getContext(), options);
return rval; } else if (JsonLdConsts.COMPACTED.equals(options.outputForm)) { return compact(rval, dataset.getContext(), options); } else if (JsonLdConsts.FLATTENED.equals(options.outputForm)) { return flatten(rval, dataset.getContext(), options);
return rval; } else if (JsonLdConsts.COMPACTED.equals(options.outputForm)) { return compact(rval, dataset.getContext(), options); } else if (JsonLdConsts.FLATTENED.equals(options.outputForm)) { return flatten(rval, dataset.getContext(), options);
@Test public void testCompaction() throws Exception { final Map<String, Object> contextAbbrevs = new HashMap<String, Object>(); contextAbbrevs.put("so", "http://schema.org/"); final Map<String, Object> json = new HashMap<String, Object>(); json.put("@context", contextAbbrevs); json.put("@id", "http://example.org/my_work"); final List<Object> types = new LinkedList<Object>(); types.add("so:CreativeWork"); json.put("@type", types); json.put("so:name", "My Work"); json.put("so:url", "http://example.org/my_work"); final JsonLdOptions options = new JsonLdOptions(); options.setBase("http://schema.org/"); options.setCompactArrays(true); // System.out.println("Before compact"); // System.out.println(JsonUtils.toPrettyString(json)); final List<String> newContexts = new LinkedList<String>(); newContexts.add("http://schema.org/"); final Map<String, Object> compacted = JsonLdProcessor.compact(json, newContexts, options); // System.out.println("\n\nAfter compact:"); // System.out.println(JsonUtils.toPrettyString(compacted)); assertTrue("Compaction removed the context", compacted.containsKey("@context")); assertFalse("Compaction of context should be a string, not a list", compacted.get("@context") instanceof List); }
private void testCompaction(String label, InputStream nextInputStream) throws IOException, FileNotFoundException, JsonLdError { final File testFile = File.createTempFile("jsonld-perf-source-", ".jsonld", testDir); FileUtils.copyInputStreamToFile(nextInputStream, testFile); final LongSummaryStatistics parseStats = new LongSummaryStatistics(); final LongSummaryStatistics compactStats = new LongSummaryStatistics(); for (int i = 0; i < 1000; i++) { final InputStream testInput = new BufferedInputStream(new FileInputStream(testFile)); try { final long parseStart = System.currentTimeMillis(); final Object inputObject = JsonUtils.fromInputStream(testInput); parseStats.accept(System.currentTimeMillis() - parseStart); final JsonLdOptions opts = new JsonLdOptions("urn:test:"); final long compactStart = System.currentTimeMillis(); JsonLdProcessor.compact(inputObject, null, opts); compactStats.accept(System.currentTimeMillis() - compactStart); } finally { testInput.close(); } } System.out.println("(" + label + ") Parse average : " + parseStats.getAverage()); System.out.println("(" + label + ") Compact average : " + compactStats.getAverage()); }
@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 Object fromRDF1 = JsonLdProcessor.compact(new JsonLdApi(options).fromRDF(inputRdf), inputRdf.getContext(), options); final Object fromRDF2 = JsonLdProcessor.compact( new JsonLdApi(options).fromRDF(inputRdf, true), inputRdf.getContext(), options); final String jsonld2 = JsonUtils.toPrettyString(fromRDF2);
@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/")); }