static private JsonLdOptions defaultJsonLdOptions(String baseURI) { JsonLdOptions opts = new JsonLdOptions(baseURI); opts.useNamespaces = true ; // this is NOT jsonld-java's default // opts.setUseRdfType(true); // false -> use "@type" opts.setUseNativeTypes(true); // this is NOT jsonld-java's default opts.setCompactArrays(true); // this is jsonld-java's default return opts; }
@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); }
@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 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 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 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 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); }
options.setCompactArrays((Boolean) test_opts.get("compactArrays"));
/** Test passing a JsonLdOptions through Context */ @Test public final void jsonldOptions() { Model m = ModelFactory.createDefaultModel(); String ns = "http://schema.org/"; Resource s = m.createResource(); m.add(s, m.createProperty(ns + "name"), "Jane Doe"); m.add(s, m.createProperty(ns + "url"), "http://www.janedoe.com"); m.add(s, m.createProperty(ns + "jobTitle"), "Professor"); // our default uses true for compactArrays String jsonld = toString(m, RDFFormat.JSONLD, null); // compactArrays is true -> no "@graph" assertFalse(jsonld.contains("@graph")); // compactArrays is true -> string, not an array for props with one value assertTrue(jsonld.contains("\"jobTitle\" : \"Professor\"")); // now output using a value for JsonLdOptions in Context that sets compactArrays to false JsonLDWriteContext jenaCtx = new JsonLDWriteContext(); JsonLdOptions opts = new JsonLdOptions(null); opts.setCompactArrays(false); jenaCtx.setOptions(opts); jsonld = toString(m, RDFFormat.JSONLD, jenaCtx); // compactArrays is false -> a "@graph" node assertTrue(jsonld.contains("@graph")); // compactArrays is false -> an array for all props, even when there's only one value assertTrue(jsonld.contains("\"jobTitle\" : [ \"Professor\" ]")); }