@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();
@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; }
@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); } });
return new JSONObject(JsonUtils.toString(compact)); } catch (IOException ex) { Logger.getLogger(CruncherJsonLdCompact.class.getName()).log(Level.SEVERE, null, ex);
return new JSONObject(JsonUtils.toString(output));
logger.debug("Converting from JSON to RDF"); jsonObject = new JsonLdApi().expand(getContext(), jsonObject); String jsonString = JsonUtils.toString(jsonObject); Model model = ModelFactory.createDefaultModel(); model.read(IOUtils.toInputStream(jsonString), null, "JSON-LD");
/** * Supports "reverse injection" from an existing remote document. * <p> * Generally the document contents are already parsed JSON content, however this method will also accept * document contents represented as a string of JSON data. */ public Builder injectDocument(RemoteDocument document) { String url = document.getDocumentUrl(); if (document.getDocument() instanceof CharSequence) { // Avoid unnecessary JSON serialization return injectDocument(url, (CharSequence) document.getDocument()); } else { try { documentLoader.addInjectedDoc(url, JsonUtils.toString(document.getDocument())); return this; } catch (IOException e) { throw new JsonLdError(JsonLdError.Error.LOADING_INJECTED_CONTEXT_FAILED, url, e); } } }
for (int i = 0; i < rounds; i++) { final long start = System.nanoTime(); JsonUtils.toString(fromRDF2); if (i < 5000) { statsFirst5000Part2.accept(System.nanoTime() - start);