/** * Parses a JSON-LD document from the given {@link InputStream} to an object * that can be used as input for the {@link JsonLdApi} and * {@link JsonLdProcessor} methods.<br> * Uses UTF-8 as the character encoding when decoding the InputStream. * * @param input * The JSON-LD document in an InputStream. * @return A JSON Object. * @throws JsonParseException * If there was a JSON related error during parsing. * @throws IOException * If there was an IO error during parsing. */ public static Object fromInputStream(InputStream input) throws IOException { // no readers from inputstreams w.o. encoding!! return fromInputStream(input, "UTF-8"); }
/** * Parses a JSON-LD document from the given {@link InputStream} to an object * that can be used as input for the {@link JsonLdApi} and * {@link JsonLdProcessor} methods. * * @param input * The JSON-LD document in an InputStream. * @param enc * The character encoding to use when interpreting the characters * in the InputStream. * @return A JSON Object. * @throws JsonParseException * If there was a JSON related error during parsing. * @throws IOException * If there was an IO error during parsing. */ public static Object fromInputStream(InputStream input, String enc) throws IOException { return fromInputStream(input, Charset.forName(enc)); }
/** * Parses a JSON-LD document from the given {@link InputStream} to an object * that can be used as input for the {@link JsonLdApi} and * {@link JsonLdProcessor} methods. * * @param input * The JSON-LD document in an InputStream. * @param enc * The character encoding to use when interpreting the characters * in the InputStream. * @return A JSON Object. * @throws JsonParseException * If there was a JSON related error during parsing. * @throws IOException * If there was an IO error during parsing. */ public static Object fromInputStream(InputStream input, String enc) throws IOException { return fromInputStream(input, Charset.forName(enc)); }
@Nullable public Object nextEntry() throws IOException { try (InputStream input = nextStream()) { // Use the JSON-LD API's method for parsing JSON, assumes (properly) UTF-8 return input != null ? JsonUtils.fromInputStream(input) : null; } }
@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); } }
/** * Generate a compact representation of the input stream * * @param input The input JSON document * @param context the context document as an InputStream * @return the compact JSON Object */ public String compact(final InputStream input, final InputStream context) { try { return doCompact(input, fromInputStream(context)); } catch (final JsonParseException ex) { throw new RuntimeException("Error parsing JSON", ex); } catch (final JsonGenerationException ex) { throw new RuntimeException("Error generating JSON", ex); } catch (final IOException ex) { throw new RuntimeException("Error reading/writing JSON document", ex); } }
/** * Generate an expanded representation of the input document * * @param input the input JSON document * @return the expanded JSON */ public String expand(final InputStream input) { try { return "[" + JsonLdProcessor.expand(fromInputStream(input), options) .stream().findFirst().map(stringify::apply).orElse("") + "]"; } catch (final JsonParseException ex) { throw new RuntimeException("Error parsing JSON", ex); } catch (final JsonGenerationException ex) { throw new RuntimeException("Error generating JSON", ex); } catch (final JsonLdError ex) { throw new RuntimeException("Error converting JsonLd", ex); } catch (final IOException ex) { throw new RuntimeException("Error reading/writing JSON document", ex); } }
private Object readSource() throws IOException { // Due to checked IOException we can't easily // do this with .map and .orElseGet() if (getSourceInputStream().isPresent()) { return JsonUtils.fromInputStream(getSourceInputStream().get()); } if (getSourceIri().isPresent()) { // TODO: propagate @base from content return JsonUtils.fromURL(asURL(getSourceIri().get()), JsonUtils.getDefaultHttpClient()); } if (getSourceFile().isPresent()) { try (InputStream inputStream = Files.newInputStream(getSourceFile().get())) { return JsonUtils.fromInputStream(inputStream); } } throw new IllegalStateException("No known source found"); }
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); }
@Test public void testFrame0007() throws IOException, JsonLdError { final Object frame = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0007-frame.jsonld")); final Object in = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0007-in.jsonld")); final JsonLdOptions opts = new JsonLdOptions(); opts.setCompactArrays(true); final Map<String, Object> frame2 = JsonLdProcessor.frame(in, frame, opts); final Object out = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0007-out.jsonld")); assertEquals(out, frame2); }
@Test public void testFrame0008() throws IOException, JsonLdError { final Object frame = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0008-frame.jsonld")); final Object in = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0008-in.jsonld")); final JsonLdOptions opts = new JsonLdOptions(); opts.setEmbed("@always"); final Map<String, Object> frame2 = JsonLdProcessor.frame(in, frame, opts); final Object out = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0008-out.jsonld")); assertEquals(out, frame2); }
@Test public void testFrame0001() throws IOException, JsonLdError { final Object frame = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0001-frame.jsonld")); final Object in = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0001-in.jsonld")); final Map<String, Object> frame2 = JsonLdProcessor.frame(in, frame, new JsonLdOptions()); assertEquals(2, frame2.size()); }
@Test public void testFrame0004() throws IOException, JsonLdError { final Object frame = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0004-frame.jsonld")); final Object in = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0004-in.jsonld")); final JsonLdOptions opts = new JsonLdOptions(); opts.setCompactArrays(true); final Map<String, Object> frame2 = JsonLdProcessor.frame(in, frame, opts); final Object out = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0004-out.jsonld")); assertEquals(out, frame2); }
@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); }
@Test public void testFrame0006() throws IOException, JsonLdError { final Object frame = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0006-frame.jsonld")); final Object in = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0006-in.jsonld")); final JsonLdOptions opts = new JsonLdOptions(); opts.setCompactArrays(true); final Map<String, Object> frame2 = JsonLdProcessor.frame(in, frame, opts); final Object out = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0006-out.jsonld")); assertEquals(out, frame2); }
@Test public void testFrame0005() throws IOException, JsonLdError { final Object frame = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0005-frame.jsonld")); final Object in = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0005-in.jsonld")); final JsonLdOptions opts = new JsonLdOptions(); opts.setCompactArrays(true); final Map<String, Object> frame2 = JsonLdProcessor.frame(in, frame, opts); final Object out = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0005-out.jsonld")); assertEquals(out, frame2); }
@Test public void testFrame0009() throws IOException, JsonLdError { final Object frame = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0009-frame.jsonld")); final Object in = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0009-in.jsonld")); final JsonLdOptions opts = new JsonLdOptions(); opts.setProcessingMode("json-ld-1.1"); final Map<String, Object> frame2 = JsonLdProcessor.frame(in, frame, opts); final Object out = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0009-out.jsonld")); assertEquals(out, frame2); }
@Test public void testFrame0003() 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); opts.setProcessingMode("json-ld-1.1"); final Map<String, Object> frame2 = JsonLdProcessor.frame(in, frame, opts); assertFalse("Result should contain no blank nodes", frame2.toString().contains("_:")); final Object out = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0003-out.jsonld")); assertEquals(out, frame2); }
@Override public void parse(final InputStream in, 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.fromInputStream(in), 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; } }
/** Test using the jena Context mechanism to pass the jsonld "@context" */ @Test public final void overrideAtContextTest() throws JsonGenerationException, IOException { // some jsonld using schema.org's URI as "@context" String jsonld = someSchemaDorOrgJsonld(); // a subset of schema.org that can be used as @context for jsonld String jsonldContext = "{\"name\":{\"@id\":\"http://schema.org/name\"},\"Person\": {\"@id\": \"http://schema.org/Person\"}}"; // pass the jsonldContext to the read using a jena Context Context jenaCtx = new Context(); Object jsonldContextAsMap = JsonUtils.fromInputStream(new ByteArrayInputStream(jsonldContext.getBytes(StandardCharsets.UTF_8))); jenaCtx.set(RIOT.JSONLD_CONTEXT, jsonldContextAsMap); // read the jsonld, replacing its "@context" Dataset ds = jsonld2dataset(jsonld, jenaCtx); // check ds is correct assertJohnDoeIsOK(ds.getDefaultModel()); }