private void trailingContent(String string) throws JsonParseException, IOException { JsonUtils.fromString(string); } }
/** * Avoid resolving a document by instead using the given serialised * representation. * * @param url * The URL this document represents. * @param doc * The serialised document as a String * @return This object for fluent addition of other injected documents. * @throws JsonLdError * If loading of the document failed for any reason. */ public DocumentLoader addInjectedDoc(String url, String doc) throws JsonLdError { try { m_injectedDocs.put(url, JsonUtils.fromString(doc)); return this; } catch (final Exception e) { throw new JsonLdError(JsonLdError.Error.LOADING_INJECTED_CONTEXT_FAILED, url, e); } }
/** * Avoid resolving a document by instead using the given serialised * representation. * * @param url * The URL this document represents. * @param doc * The serialised document as a String * @return This object for fluent addition of other injected documents. * @throws JsonLdError * If loading of the document failed for any reason. */ public DocumentLoader addInjectedDoc(String url, String doc) throws JsonLdError { try { m_injectedDocs.put(url, JsonUtils.fromString(doc)); return this; } catch (final Exception e) { throw new JsonLdError(JsonLdError.Error.LOADING_INJECTED_CONTEXT_FAILED, url, 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(); }
@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); } } }
/** * Load a map of prefixes from the "@context" of a JSON-LD string. * * @param jsonString the JSON-LD string * @return a map from prefix name strings to prefix IRI strings * @throws IOException on any problem */ @SuppressWarnings("unchecked") public static Context parseContext(String jsonString) throws IOException { try { Object jsonObject = JsonUtils.fromString(jsonString); if (!(jsonObject instanceof Map)) { throw new IOException(jsonldContextParseError); } Map<String, Object> jsonMap = (Map<String, Object>) jsonObject; if (!jsonMap.containsKey("@context")) { throw new IOException(jsonldContextParseError); } Object jsonContext = jsonMap.get("@context"); return new Context().parse(jsonContext); } catch (Exception e) { throw new IOException(jsonldContextParseError, e); } }
@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); } });
jsonLd = jsonLd.replaceAll("http://www\\.schema\\.org", "http://schema.org"); 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);
/** * 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); } }
} else if (extension.equals("js") || extension.equals("json") || extension.equals("jsonld")) { String jsonString = FileUtils.readFileToString(ontologyFile); jsonObject = JsonUtils.fromString(jsonString);
frame = JsonUtils.fromString((String) frame); Map<String, Object> map = (Map<String, Object>) obj; if (map.containsKey("@context")) { map.put("@context", JsonUtils.fromString(ctxReplacement.toString()));
@SuppressWarnings("unchecked") @Test public void fromStringTest() { final String testString = "{\"seq\":3,\"id\":\"e48dfa735d9fad88db6b7cd696002df7\",\"changes\":[{\"rev\":\"2-6aebf275bc3f29b67695c727d448df8e\"}]}"; final String testFailure = "{{{{{{{{{{{"; Object obj = null; try { obj = JsonUtils.fromString(testString); } catch (final Exception e) { assertTrue(false); } assertTrue(((Map<String, Object>) obj).containsKey("seq")); assertTrue(((Map<String, Object>) obj).get("seq") instanceof Number); try { obj = JsonUtils.fromString(testFailure); assertTrue(false); } catch (final Exception e) { assertTrue(true); } }
if (o instanceof String) { // supposed to be a json string String jsonString = (String) o; ctx = JsonUtils.fromString(jsonString); } else { ctx = o;
final Object rval = JsonUtils.fromString(manifestFile); if (rval instanceof Map) { this.manifest = (Map<String, Object>) rval;
@Test public void injectContext() throws Exception { final Object jsonObject = JsonUtils.fromString( "{ \"@context\":\"http://nonexisting.example.com/thing\", \"pony\":5 }"); final JsonLdOptions options = new JsonLdOptions(); // Verify fails to find context by default try { JsonLdProcessor.expand(jsonObject, options); fail("Expected exception to occur"); } catch (final JsonLdError e) { // Success } // Inject context final DocumentLoader dl = new DocumentLoader(); dl.addInjectedDoc("http://nonexisting.example.com/thing", "{ \"@context\": { \"pony\":\"http://nonexisting.example.com/thing/pony\" } }"); options.setDocumentLoader(dl); // Execute final List<Object> expand = JsonLdProcessor.expand(jsonObject, options); // Verify result final Object v = ((Map<Object, Object>) ((List<Object>) ((Map<Object, Object>) expand .get(0)).get("http://nonexisting.example.com/thing/pony")).get(0)).get("@value"); assertEquals(5, v); }
jenaCtx.set(JsonLDWriter.JSONLD_FRAME, JsonUtils.fromString(frame.toString())); jsonld = toString(m, RDFFormat.JSONLD_FRAME_FLAT, jenaCtx); m2 = parse(jsonld);