Skip to content

Commit 01a2cc6

Browse files
committed
fix #240
1 parent b1b2ea6 commit 01a2cc6

File tree

7 files changed

+55
-37
lines changed

7 files changed

+55
-37
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,9 @@ Alternatively, we can also host your repository in the jsonld-java organisation
449449
CHANGELOG
450450
=========
451451

452+
### 2018-09-05
453+
* handle omit graph flag
454+
452455
### 2018-07-07
453456
* make pruneBlankNodeIdentifiers false by default in 1.0 mode and always true in 1.1 mode
454457
* fix issue with blank node identifier pruning when @id is aliased

core/src/main/java/com/github/jsonldjava/core/JsonLdApi.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,8 +505,7 @@ public Object compact(Context activeCtx, String activeProperty, Object element)
505505
*/
506506
public Object expand(Context activeCtx, String activeProperty, Object element)
507507
throws JsonLdError {
508-
final boolean frameExpansion = this.opts.getProcessingMode()
509-
.equals(JsonLdOptions.JSON_LD_1_1_FRAME);
508+
final boolean frameExpansion = this.opts.getFrameExpansion();
510509
// 1)
511510
if (element == null) {
512511
return null;

core/src/main/java/com/github/jsonldjava/core/JsonLdOptions.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ public class JsonLdOptions {
1616

1717
public static final String JSON_LD_1_1 = "json-ld-1.1";
1818

19-
public static final String JSON_LD_1_1_FRAME = "json-ld-1.1-expand-frame";
20-
2119
public static final boolean DEFAULT_COMPACT_ARRAYS = true;
2220

2321
/**
@@ -66,6 +64,8 @@ public JsonLdOptions(String base) {
6664
private Embed embed = Embed.LAST;
6765
private Boolean explicit = null;
6866
private Boolean omitDefault = null;
67+
private Boolean omitGraph = false;
68+
private Boolean frameExpansion = false;
6969
private Boolean pruneBlankNodeIdentifiers = false;
7070
private Boolean requireAll = false;
7171

@@ -132,14 +132,27 @@ public void setOmitDefault(Boolean omitDefault) {
132132
this.omitDefault = omitDefault;
133133
}
134134

135+
public Boolean getFrameExpansion() {
136+
return frameExpansion;
137+
}
138+
139+
public void setFrameExpansion(Boolean frameExpansion) {
140+
this.frameExpansion = frameExpansion;
141+
}
142+
143+
public Boolean getOmitGraph() {
144+
return omitGraph;
145+
}
146+
147+
public void setOmitGraph(Boolean omitGraph) {
148+
this.omitGraph = omitGraph;
149+
}
150+
135151
public Boolean getPruneBlankNodeIdentifiers() {
136-
return pruneBlankNodeIdentifiers || getProcessingMode().equals(JSON_LD_1_1);
152+
return pruneBlankNodeIdentifiers;
137153
}
138154

139155
public void setPruneBlankNodeIdentifiers(Boolean pruneBlankNodeIdentifiers) {
140-
if (pruneBlankNodeIdentifiers) {
141-
setProcessingMode(JSON_LD_1_1);
142-
}
143156
this.pruneBlankNodeIdentifiers = pruneBlankNodeIdentifiers;
144157
}
145158

@@ -173,6 +186,10 @@ public String getProcessingMode() {
173186

174187
public void setProcessingMode(String processingMode) {
175188
this.processingMode = processingMode;
189+
if (processingMode.equals(JSON_LD_1_1)) {
190+
this.omitGraph = true;
191+
this.pruneBlankNodeIdentifiers = true;
192+
}
176193
}
177194

178195
public String getBase() {

core/src/main/java/com/github/jsonldjava/core/JsonLdProcessor.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -307,15 +307,12 @@ public static Map<String, Object> frame(Object input, Object frame, JsonLdOption
307307
final Object expandedInput = expand(input, opts);
308308

309309
// 3. Set expanded frame to the result of using the expand method using
310-
// frame and options
311-
// with expandContext set to null and processingMode set to
312-
// json-ld-1.1-expand-frame.
313-
final String savedProcessingMode = opts.getProcessingMode();
310+
// frame and options with expandContext set to null and the
311+
// frameExpansion option set to true.
314312
final Object savedExpandedContext = opts.getExpandContext();
315-
opts.setProcessingMode(JsonLdOptions.JSON_LD_1_1_FRAME);
316313
opts.setExpandContext(null);
314+
opts.setFrameExpansion(true);
317315
final List<Object> expandedFrame = expand(frame, opts);
318-
opts.setProcessingMode(savedProcessingMode);
319316
opts.setExpandContext(savedExpandedContext);
320317

321318
// 4. Set context to the value of @context from frame, if it exists, or
@@ -329,14 +326,19 @@ public static Map<String, Object> frame(Object input, Object frame, JsonLdOption
329326
JsonLdUtils.pruneBlankNodes(framed);
330327
}
331328
Object compacted = api.compact(activeCtx, null, framed, opts.getCompactArrays());
332-
if (!(compacted instanceof List)) {
329+
final Map<String, Object> rval = activeCtx.serialize();
330+
final boolean addGraph = ((!(compacted instanceof List)) && !opts.getOmitGraph());
331+
if (addGraph && !(compacted instanceof List)) {
333332
final List<Object> tmp = new ArrayList<Object>();
334333
tmp.add(compacted);
335334
compacted = tmp;
336335
}
337-
final String alias = activeCtx.compactIri(JsonLdConsts.GRAPH);
338-
final Map<String, Object> rval = activeCtx.serialize();
339-
rval.put(alias, compacted);
336+
if (addGraph || (compacted instanceof List)) {
337+
final String alias = activeCtx.compactIri(JsonLdConsts.GRAPH);
338+
rval.put(alias, compacted);
339+
} else if (!addGraph && (compacted instanceof Map)) {
340+
rval.putAll((Map) compacted);
341+
}
340342
JsonLdUtils.removePreserve(activeCtx, rval, opts);
341343
return rval;
342344
}

core/src/test/java/com/github/jsonldjava/core/JsonLdFramingTest.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -138,22 +138,6 @@ public void testFrame0008() throws IOException, JsonLdError {
138138
assertEquals(out, frame2);
139139
}
140140

141-
@Test
142-
public void testFramep050() throws IOException, JsonLdError {
143-
final Object frame = JsonUtils
144-
.fromInputStream(getClass().getResourceAsStream("/json-ld.org/frame-p050-frame.jsonld"));
145-
final Object in = JsonUtils
146-
.fromInputStream(getClass().getResourceAsStream("/json-ld.org/frame-p050-in.jsonld"));
147-
148-
final JsonLdOptions opts = new JsonLdOptions();
149-
opts.setProcessingMode("json-ld-1.1");
150-
final Map<String, Object> frame2 = JsonLdProcessor.frame(in, frame, opts);
151-
152-
final Object out = JsonUtils
153-
.fromInputStream(getClass().getResourceAsStream("/json-ld.org/frame-p050-out.jsonld"));
154-
assertEquals(out, frame2);
155-
}
156-
157141
@Test
158142
public void testFrame0009() throws IOException, JsonLdError {
159143
final Object frame = JsonUtils

core/src/test/java/com/github/jsonldjava/core/JsonLdProcessorTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,9 @@ public void runTest() throws URISyntaxException, IOException, JsonLdError {
419419
if (test_opts.containsKey("processingMode")) {
420420
options.setProcessingMode((String) test_opts.get("processingMode"));
421421
}
422+
if (test_opts.containsKey("omitGraph")) {
423+
options.setOmitGraph((Boolean) test_opts.get("omitGraph"));
424+
}
422425
if (test_opts.containsKey("produceGeneralizedRdf")) {
423426
options.setProduceGeneralizedRdf((Boolean) test_opts.get("produceGeneralizedRdf"));
424427
}

core/src/test/resources/json-ld.org/frame-manifest.jsonld

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,21 @@
167167
"input": "frame-0030-in.jsonld",
168168
"frame": "frame-0030-frame.jsonld",
169169
"expect": "frame-0030-out.jsonld"
170+
}, {
171+
"@id": "#tg001",
172+
"@type": ["jld:PositiveEvaluationTest", "jld:FrameTest"],
173+
"name": "Library framing example with @graph and omitGraph is true.",
174+
"purpose": "Basic example used in playground and spec examples.",
175+
"input": "frame-g001-in.jsonld",
176+
"frame": "frame-g001-frame.jsonld",
177+
"expect": "frame-g001-out.jsonld",
178+
"option": {"specVersion": "json-ld-1.1", "omitGraph": true}
170179
}, {
171180
"@id": "#tp010",
172181
"@type": ["jld:PositiveEvaluationTest", "jld:FrameTest"],
173182
"name": "Property CURIE conflict (prune bnodes)",
174-
"option": {"processingMode": "json-ld-1.1", "specVersion": "json-ld-1.1"},
183+
"purpose": "(Not really framing) A term looking like a CURIE becomes a CURIE when framing/compacting if defined as such in frame/context.",
184+
"option": {"processingMode": "json-ld-1.1", "omitGraph" : false, "specVersion": "json-ld-1.1"},
175185
"input": "frame-0010-in.jsonld",
176186
"frame": "frame-0010-frame.jsonld",
177187
"expect": "frame-p010-out.jsonld"
@@ -195,7 +205,7 @@
195205
"@id": "#tp046",
196206
"@type": ["jld:PositiveEvaluationTest", "jld:FrameTest"],
197207
"name": "Merge graphs if no outer @graph is used (prune bnodes)",
198-
"option": {"processingMode": "json-ld-1.1", "specVersion": "json-ld-1.1"},
208+
"option": {"processingMode": "json-ld-1.1", "omitGraph" : false, "specVersion": "json-ld-1.1"},
199209
"input": "frame-0046-in.jsonld",
200210
"frame": "frame-0046-frame.jsonld",
201211
"expect": "frame-p046-out.jsonld"
@@ -207,6 +217,6 @@
207217
"input": "frame-p050-in.jsonld",
208218
"frame": "frame-p050-frame.jsonld",
209219
"expect": "frame-p050-out.jsonld",
210-
"option": {"processingMode": "json-ld-1.1", "specVersion": "json-ld-1.1"}
220+
"option": {"processingMode": "json-ld-1.1", "omitGraph" : false, "specVersion": "json-ld-1.1"}
211221
}]
212222
}

0 commit comments

Comments
 (0)