Added support for @json @type by umbreak · Pull Request #310 · jsonld-java/jsonld-java · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions core/reports/json-ld-api-tests-skip
40 changes: 27 additions & 13 deletions core/src/main/java/com/github/jsonldjava/core/JsonLdApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static com.github.jsonldjava.core.JsonLdUtils.isKeyword;
import static com.github.jsonldjava.utils.Obj.newMap;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -20,6 +21,7 @@
import java.util.Set;
import java.util.TreeMap;

import com.github.jsonldjava.utils.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -565,6 +567,7 @@ else if (element instanceof Map) {
// 7)
final List<String> keys = new ArrayList<String>(elem.keySet());
Collections.sort(keys);
Map<String, Object> valueKeys = newMap();
// GK: This is the place to check for a type-scoped context by checking any key that expands to `@type` to see the current context has a term that equals that key where the term definition includes `@context`, updating the activeCtx as you go (but using termScopedContext when checking the keys).
// GK: 1.1 made the following loop somewhat recursive, due to nesting, so might want to extract into a method.
for (final String key : keys) {
Expand Down Expand Up @@ -594,7 +597,6 @@ else if (element instanceof Map) {
expandedProperty + " already exists in result");
}
// jsonld 1.1: 12 in https://w3c.github.io/json-ld-api/#algorithm-3
Object inputType = elem.get(JsonLdConsts.TYPE);
// 7.4.3)
if (JsonLdConsts.ID.equals(expandedProperty)) {
if (value instanceof String) {
Expand Down Expand Up @@ -660,15 +662,9 @@ else if (JsonLdConsts.GRAPH.equals(expandedProperty)) {
}
// 7.4.6)
else if (JsonLdConsts.VALUE.equals(expandedProperty)) {
// jsonld 1.1: 13.4.7.1 in https://w3c.github.io/json-ld-api/#algorithm-3
if(JsonLdConsts.JSON.equals(inputType)) {
expandedValue = value;
if(opts.getProcessingMode().equals(JsonLdOptions.JSON_LD_1_0)) {
throw new JsonLdError(Error.INVALID_VALUE_OBJECT_VALUE, value);
}
}
// jsonld 1.1: 13.4.7.2 in https://w3c.github.io/json-ld-api/#algorithm-3
else if (value != null && (value instanceof Map || value instanceof List)) {
if (value != null && (value instanceof Map || value instanceof List) &&
!opts.isProcessingMode11()) {
throw new JsonLdError(Error.INVALID_VALUE_OBJECT_VALUE,
"value of " + expandedProperty + " must be a scalar or null, but was: " + value);
}
Expand Down Expand Up @@ -981,14 +977,20 @@ else if (JsonLdConsts.INDEX.equals(activeCtx.getContainer(key))
}
// 8.2)
final Object rval = result.get(JsonLdConsts.VALUE);
if (rval == null) {
if (result.getOrDefault(JsonLdConsts.TYPE,"").equals(JsonLdConsts.JSON)) {
// jsonld 1.1: 14.3 in https://w3c.github.io/json-ld-api/#algorithm-3
}
else if(opts.isProcessingMode11() && (rval instanceof Map || rval instanceof List)) {
// jsonld 1.1: 13.4.7.1 https://w3c.github.io/json-ld-api/#algorithm-3
throw new JsonLdError(Error.INVALID_VALUE_OBJECT_VALUE,
"value of " + activeProperty + " must be a scalar or null");
}

else if (rval == null) {
// nothing else is possible with result if we set it to
// null, so simply return it
return null;
}
else if (result.getOrDefault(JsonLdConsts.TYPE,"").equals(JsonLdConsts.JSON)) {
// jsonld 1.1: 14.3 in https://w3c.github.io/json-ld-api/#algorithm-3
}
// 8.3)
else if (!(rval instanceof String) && result.containsKey(JsonLdConsts.LANGUAGE)) {
throw new JsonLdError(Error.INVALID_LANGUAGE_TAGGED_VALUE,
Expand Down Expand Up @@ -2051,6 +2053,18 @@ public List<Object> fromRDF(final RDFDataset dataset, boolean noDuplicatesInData
nodeMap.computeIfAbsent(object.getValue(), k -> new NodeMapNode(k));
}

if (opts.isProcessingMode11() && JsonLdConsts.RDF_JSON.equals(object.getDatatype())) {
try {
final Object json = JsonUtils.fromString(object.getValue());
Map<String, Object> entries = newMap(JsonLdConsts.TYPE, JsonLdConsts.JSON);
entries.put(JsonLdConsts.VALUE, json);
JsonLdUtils.mergeValue(node, predicate, entries);
} catch (IOException e) {
throw new JsonLdError(JsonLdError.Error.INVALID_JSON_LITERAL);
}
continue;
}

// 3.5.4)
if (RDF_TYPE.equals(predicate) && (object.isIRI() || object.isBlankNode())
&& !opts.getUseRdfType() && !nodes.containsKey(object.getValue())) {
Expand Down
10 changes: 10 additions & 0 deletions core/src/main/java/com/github/jsonldjava/core/JsonLdConsts.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.github.jsonldjava.core;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

/**
* URI Constants used in the JSON-LD parser.
*/
Expand Down Expand Up @@ -70,4 +74,10 @@ public final class JsonLdConsts {
public enum Embed {
ALWAYS, NEVER, LAST, LINK;
}

public final static DecimalFormat DOUBLE_DECIMAL_FORMAT = new DecimalFormat("0.0###############E0",
DecimalFormatSymbols.getInstance(Locale.US));

public final static DecimalFormat INT_DECIMAL_FORMAT = new DecimalFormat("0",
DecimalFormatSymbols.getInstance(Locale.US));
}
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ public String getProcessingMode() {
return processingMode;
}

public boolean isProcessingMode11() {
return JSON_LD_1_1.equals(getProcessingMode());
}

public void setProcessingMode(String processingMode) {
this.processingMode = processingMode;
if (processingMode.equals(JSON_LD_1_1)) {
Expand Down
30 changes: 12 additions & 18 deletions core/src/main/java/com/github/jsonldjava/core/RDFDataset.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,6 @@ public int compareTo(Node o) {
/**
* Converts an RDF triple object to a JSON-LD object.
*
* @param o
* the RDF triple object to convert.
* @param useNativeTypes
* true to output native types, false not to.
*
Expand Down Expand Up @@ -251,7 +249,6 @@ else if(RDF_JSON.equals(type)) {
try {
rval.put("@value", JsonUtils.fromString(value));
} catch (IOException e) {
e.printStackTrace();
throw new JsonLdError(JsonLdError.Error.INVALID_JSON_LITERAL, value, e);
}
}
Expand Down Expand Up @@ -667,7 +664,16 @@ private Node objectToRDF(Object item) {
final Object datatype = ((Map<String, Object>) item).get("@type");

// convert to XSD datatypes as appropriate
if (value instanceof Boolean || value instanceof Number) {
if(JsonLdConsts.JSON.equals(datatype)) {
try {
// jsonld 1.1: 8 in https://w3c.github.io/json-ld-api/#algorithm-13
return new Literal(JsonUtils.toJcsString(value), RDF_JSON, null);
} catch (IOException e) {
e.printStackTrace();
throw new JsonLdError(JsonLdError.Error.INVALID_JSON_LITERAL);
}
}
else if (value instanceof Boolean || value instanceof Number) {
// convert to XSD datatype
if (value instanceof Boolean) {
return new Literal(value.toString(),
Expand All @@ -685,30 +691,18 @@ private Node objectToRDF(Object item) {
if (XSD_DECIMAL.equals(datatype)) {
return new Literal(value.toString(), XSD_DECIMAL, null);
}
final DecimalFormat df = new DecimalFormat("0.0###############E0");
df.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance(Locale.US));
return new Literal(df.format(value),
return new Literal(JsonLdConsts.DOUBLE_DECIMAL_FORMAT.format(value),
datatype == null ? XSD_DOUBLE : (String) datatype, null);
}
} else {
final DecimalFormat df = new DecimalFormat("0");
return new Literal(df.format(value),
return new Literal(JsonLdConsts.INT_DECIMAL_FORMAT.format(value),
datatype == null ? XSD_INTEGER : (String) datatype, null);
}
} else if (((Map<String, Object>) item).containsKey("@language")) {
return new Literal((String) value,
datatype == null ? RDF_LANGSTRING : (String) datatype,
(String) ((Map<String, Object>) item).get("@language"));
}
// jsonld 1.1: 8 in https://w3c.github.io/json-ld-api/#algorithm-13
else if(JsonLdConsts.JSON.equals(datatype)) {
try {
return new Literal(JsonUtils.toString(value), RDF_JSON, null);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
else {
return new Literal((String) value,
datatype == null ? XSD_STRING : (String) datatype, null);
Expand Down
83 changes: 51 additions & 32 deletions core/src/main/java/com/github/jsonldjava/core/RDFDatasetUtils.java
Loading