@Override protected List<Object> applyOnce(Object input, JsonLdOptions options) throws JsonLdError { if (input instanceof Map<?, ?>) { Map<?, ?> inputMap = (Map<?, ?>) input; if (inputMap.size() == 1 && inputMap.containsKey(JsonLdConsts.ID)) { // Preserve inputs that are a single identifier List<Object> result = new ArrayList<>(); result.add(new LinkedHashMap<>(inputMap)); return result; } } return JsonLdProcessor.expand(input, options); } }
/** * Expands the given input according to the steps in the <a * href="http://www.w3.org/TR/json-ld-api/#expansion-algorithm">Expansion * algorithm</a>, using the default {@link JsonLdOptions}. * * @param input * The input JSON-LD object. * @return The expanded JSON-LD document * @throws JsonLdError * If there is an error while expanding. */ public static List<Object> expand(Object input) throws JsonLdError { return expand(input, new JsonLdOptions("")); }
/** * Expands the given input according to the steps in the * <a href="http://www.w3.org/TR/json-ld-api/#expansion-algorithm">Expansion * algorithm</a>, using the default {@link JsonLdOptions}. * * @param input * The input JSON-LD object. * @return The expanded JSON-LD document * @throws JsonLdError * If there is an error while expanding. */ public static List<Object> expand(Object input) throws JsonLdError { return expand(input, new JsonLdOptions("")); }
/** * Expands the given input according to the steps in the * <a href="http://www.w3.org/TR/json-ld-api/#expansion-algorithm">Expansion * algorithm</a>, using the default {@link JsonLdOptions}. * * @param input * The input JSON-LD object. * @return The expanded JSON-LD document * @throws JsonLdError * If there is an error while expanding. */ public static List<Object> expand(Object input) throws JsonLdError { return expand(input, new JsonLdOptions("")); }
/** * 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); } }
@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; }
output = JsonLdProcessor.expand(output, opts);
output = JsonLdProcessor.expand(output,opts);
output = JsonLdProcessor.expand(output,opts);
final Object expanded = expand(input, opts);
final Object expanded = expand(input, opts);
throws JsonLdError { final Object expandedInput = expand(input, options);
throws JsonLdError { final Object expandedInput = expand(input, options);
@Test public void testMixedLocalRemoteBaseRemoteContextFirst() throws Exception { final Reader reader = new BufferedReader(new InputStreamReader( this.getClass().getResourceAsStream("/custom/base-0001-in.jsonld"), Charset.forName("UTF-8"))); final Object context = JsonUtils.fromReader(reader); assertNotNull(context); final JsonLdOptions options = new JsonLdOptions(); final Object expanded = JsonLdProcessor.expand(context, options); // System.out.println(JsonUtils.toPrettyString(expanded)); final Reader outReader = new BufferedReader(new InputStreamReader( this.getClass().getResourceAsStream("/custom/base-0001-out.jsonld"), Charset.forName("UTF-8"))); final Object output = JsonUtils.fromReader(outReader); assertNotNull(output); assertEquals(expanded, output); }
@Test public void testMixedLocalRemoteBaseLocalContextFirst() throws Exception { final Reader reader = new BufferedReader(new InputStreamReader( this.getClass().getResourceAsStream("/custom/base-0002-in.jsonld"), Charset.forName("UTF-8"))); final Object context = JsonUtils.fromReader(reader); assertNotNull(context); final JsonLdOptions options = new JsonLdOptions(); final Object expanded = JsonLdProcessor.expand(context, options); // System.out.println(JsonUtils.toPrettyString(expanded)); final Reader outReader = new BufferedReader(new InputStreamReader( this.getClass().getResourceAsStream("/custom/base-0002-out.jsonld"), Charset.forName("UTF-8"))); final Object output = JsonUtils.fromReader(outReader); assertNotNull(output); assertEquals(expanded, output); }
throws JsonLdError { final Object expandedInput = expand(input, options);
public BdioMetadata retrieveMetadata() { GraphTraversalSource g = traversal(); return options().metadataLabel() .flatMap(label -> g.V().hasLabel(label).tryNext()) .map(vertex -> { BdioMetadata metadata = new BdioMetadata(); options().identifierKey().ifPresent(key -> { metadata.id(vertex.value(key)); }); Map<String, Object> metadataProperties = new LinkedHashMap<>(); vertex.properties().forEachRemaining(vp -> { // TODO Some properties come back without type information; how do we restore it? // e.g. a product list will get serialized without it's type metadataProperties.put(vp.key(), frame().context().toFieldValue(vp.key(), vp.value())); }); for (Object expanded : JsonLdProcessor.expand(metadataProperties, frame().context().jsonLdOptions())) { if (expanded instanceof Map<?, ?>) { ((Map<?, ?>) expanded).forEach((k, v) -> { metadata.put((String) k, v); }); } } return metadata; }) .orElseGet(BdioMetadata::createRandomUUID); }
@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); }
output = JsonLdProcessor.expand(output, opts);