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
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ https://w3c.github.io/json-ld-api/tests/compact-manifest#tin02
https://w3c.github.io/json-ld-api/tests/compact-manifest#tin03
https://w3c.github.io/json-ld-api/tests/compact-manifest#tin04
https://w3c.github.io/json-ld-api/tests/compact-manifest#tin05
https://w3c.github.io/json-ld-api/tests/compact-manifest#tjs11
https://w3c.github.io/json-ld-api/tests/compact-manifest#tli01
https://w3c.github.io/json-ld-api/tests/compact-manifest#tli02
https://w3c.github.io/json-ld-api/tests/compact-manifest#tli03
Expand Down Expand Up @@ -290,9 +289,6 @@ https://w3c.github.io/json-ld-api/tests/expand-manifest#tin06
https://w3c.github.io/json-ld-api/tests/expand-manifest#tin07
https://w3c.github.io/json-ld-api/tests/expand-manifest#tin08
https://w3c.github.io/json-ld-api/tests/expand-manifest#tin09
https://w3c.github.io/json-ld-api/tests/expand-manifest#tjs16
https://w3c.github.io/json-ld-api/tests/expand-manifest#tjs19
https://w3c.github.io/json-ld-api/tests/expand-manifest#tjs22
https://w3c.github.io/json-ld-api/tests/expand-manifest#tli01
https://w3c.github.io/json-ld-api/tests/expand-manifest#tli02
https://w3c.github.io/json-ld-api/tests/expand-manifest#tli03
Expand Down Expand Up @@ -401,8 +397,6 @@ https://w3c.github.io/json-ld-api/tests/fromRdf-manifest#tdi05
https://w3c.github.io/json-ld-api/tests/fromRdf-manifest#tdi06
https://w3c.github.io/json-ld-api/tests/fromRdf-manifest#tdi11
https://w3c.github.io/json-ld-api/tests/fromRdf-manifest#tdi12
https://w3c.github.io/json-ld-api/tests/fromRdf-manifest#tjs08
https://w3c.github.io/json-ld-api/tests/fromRdf-manifest#tjs09
https://w3c.github.io/json-ld-api/tests/fromRdf-manifest#tli01
https://w3c.github.io/json-ld-api/tests/fromRdf-manifest#tli02
https://w3c.github.io/json-ld-api/tests/fromRdf-manifest#tli03
Expand Down Expand Up @@ -573,24 +567,12 @@ https://w3c.github.io/json-ld-api/tests/toRdf-manifest#tin06
https://w3c.github.io/json-ld-api/tests/toRdf-manifest#tin07
https://w3c.github.io/json-ld-api/tests/toRdf-manifest#tin08
https://w3c.github.io/json-ld-api/tests/toRdf-manifest#tin09
https://w3c.github.io/json-ld-api/tests/toRdf-manifest#tjs01
https://w3c.github.io/json-ld-api/tests/toRdf-manifest#tjs02
https://w3c.github.io/json-ld-api/tests/toRdf-manifest#tjs03
https://w3c.github.io/json-ld-api/tests/toRdf-manifest#tjs04
https://w3c.github.io/json-ld-api/tests/toRdf-manifest#tjs05
https://w3c.github.io/json-ld-api/tests/toRdf-manifest#tjs08
https://w3c.github.io/json-ld-api/tests/toRdf-manifest#tjs10
https://w3c.github.io/json-ld-api/tests/toRdf-manifest#tjs11
https://w3c.github.io/json-ld-api/tests/toRdf-manifest#tjs12
https://w3c.github.io/json-ld-api/tests/toRdf-manifest#tjs13
https://w3c.github.io/json-ld-api/tests/toRdf-manifest#tjs14
https://w3c.github.io/json-ld-api/tests/toRdf-manifest#tjs15
https://w3c.github.io/json-ld-api/tests/toRdf-manifest#tjs16
https://w3c.github.io/json-ld-api/tests/toRdf-manifest#tjs17
https://w3c.github.io/json-ld-api/tests/toRdf-manifest#tjs18
https://w3c.github.io/json-ld-api/tests/toRdf-manifest#tjs19
https://w3c.github.io/json-ld-api/tests/toRdf-manifest#tjs21
https://w3c.github.io/json-ld-api/tests/toRdf-manifest#tjs22
https://w3c.github.io/json-ld-api/tests/toRdf-manifest#tli01
https://w3c.github.io/json-ld-api/tests/toRdf-manifest#tli02
https://w3c.github.io/json-ld-api/tests/toRdf-manifest#tli03
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ else if (bnode != null) {
}
} else {
output.append("\"");
escape(o.getValue(), output);
if(JsonLdConsts.RDF_JSON.equals(o.getDatatype())) {
escapeJcs(o.getValue(), output);
}else {
escape(o.getValue(), output);
}
output.append("\"");
if (RDF_LANGSTRING.equals(o.getDatatype())) {
output.append("@").append(o.getLanguage());
Expand Down Expand Up @@ -192,6 +196,51 @@ public static String unescape(String str) {
return rval;
}

private static void specialCharactersEscaped(char c, StringBuilder rval) {
switch (c) {
case '\b':
rval.append("\\b");
break;
case '\n':
rval.append("\\n");
break;
case '\t':
rval.append("\\t");
break;
case '\f':
rval.append("\\f");
break;
case '\r':
rval.append("\\r");
break;
case '\"':
rval.append("\\\"");
break;
case '\\':
rval.append("\\\\");
break;
default:
rval.append(c);
break;
}
}


/**
* Escapes the given JSON Canonicalization Scheme string
*
* @param str
* The string to escape
* @param rval
* The {@link StringBuilder} to append to.
*/
public static void escapeJcs(String str, StringBuilder rval) {
for (int i = 0; i < str.length(); i++) {
final char hi = str.charAt(i);
specialCharactersEscaped(hi, rval);
}
}

/**
* Escapes the given string according to the N-Quads escape rules
*
Expand Down Expand Up @@ -221,37 +270,7 @@ public static void escape(String str, StringBuilder rval) {
final int c = (hi << 10) + lo + (0x10000 - (0xD800 << 10) - 0xDC00);
rval.append(String.format("\\U%08x", c));
} else {
switch (hi) {
case '\b':
rval.append("\\b");
break;
case '\n':
rval.append("\\n");
break;
case '\t':
rval.append("\\t");
break;
case '\f':
rval.append("\\f");
break;
case '\r':
rval.append("\\r");
break;
// case '\'':
// rval += "\\'";
// break;
case '\"':
rval.append("\\\"");
// rval += "\\u0022";
break;
case '\\':
rval.append("\\\\");
break;
default:
// just put the char as is
rval.append(hi);
break;
}
specialCharactersEscaped(hi, rval);
}
}
// return rval;
Expand Down
Loading