Skip to content
Merged
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
94 changes: 0 additions & 94 deletions core/reports/json-ld-api-tests-skip

Large diffs are not rendered by default.

151 changes: 117 additions & 34 deletions core/src/main/java/com/github/jsonldjava/core/Context.java

Large diffs are not rendered by default.

66 changes: 59 additions & 7 deletions core/src/main/java/com/github/jsonldjava/core/JsonLdApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,12 @@ public Object compact(Context activeCtx, String activeProperty, Object element,
// 4
if (elem.containsKey(JsonLdConsts.VALUE) || elem.containsKey(JsonLdConsts.ID)) {
final Object compactedValue = activeCtx.compactValue(activeProperty, elem);
if (!(compactedValue instanceof Map || compactedValue instanceof List)) {
boolean isScalar = !(compactedValue instanceof Map || compactedValue instanceof List);
// jsonld 1.1: 7 in https://w3c.github.io/json-ld-api/#algorithm-6
boolean isJson = activeCtx.getTermDefinition(activeProperty) != null
&& JsonLdConsts.JSON.equals(
activeCtx.getTermDefinition(activeProperty).get(JsonLdConsts.TYPE));
if (isScalar || isJson) {
return compactedValue;
}
}
Expand Down Expand Up @@ -312,7 +317,9 @@ else if (JsonLdConsts.INDEX.equals(expandedProperty)

// NOTE: expanded value must be an array due to expansion
// algorithm.

if (!(expandedValue instanceof List)) {
throw new JsonLdError(Error.NOT_IMPLEMENTED, "no array: " + expandedValue);
}
// 7.5)
if (((List<Object>) expandedValue).size() == 0) {
// 7.5.1)
Expand Down Expand Up @@ -514,6 +521,7 @@ public Object expand(Context activeCtx, String activeProperty, Object element)
return null;
}

// GK: This would be the point to set `propertyScopedContext` to the `@context` entry for any term definition associated with `activeProperty`.
// 3)
if (element instanceof List) {
// 3.1)
Expand Down Expand Up @@ -546,14 +554,19 @@ else if (element instanceof Map) {
// access helper
final Map<String, Object> elem = (Map<String, Object>) element;
// 5)
// This would be the place to revert the active context from any previous type-scoped context if the active context has a `previousContext` entry (with some exceptions when called from a map, or if it's a value object or a subject reference).
// GK: If we found a `propertyScopedContext` above, we can parse it to create a new activeCtx using the `override protected` option
if (elem.containsKey(JsonLdConsts.CONTEXT)) {
activeCtx = activeCtx.parse(elem.get(JsonLdConsts.CONTEXT));
}
// GK: This would be the place to remember this version of activeCtx as `typeScopedContext`.
// 6)
Map<String, Object> result = newMap();
// 7)
final List<String> keys = new ArrayList<String>(elem.keySet());
Collections.sort(keys);
// 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) {
final Object value = elem.get(key);
// 7.1)
Expand All @@ -580,6 +593,8 @@ else if (element instanceof Map) {
throw new JsonLdError(Error.COLLIDING_KEYWORDS,
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 @@ -645,11 +660,23 @@ else if (JsonLdConsts.GRAPH.equals(expandedProperty)) {
}
// 7.4.6)
else if (JsonLdConsts.VALUE.equals(expandedProperty)) {
if (value != null && (value instanceof Map || value instanceof List)) {
// 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)) {
throw new JsonLdError(Error.INVALID_VALUE_OBJECT_VALUE,
"value of " + expandedProperty + " must be a scalar or null");
"value of " + expandedProperty + " must be a scalar or null, but was: " + value);
}
expandedValue = value;
// jsonld 1.1: 13.4.7.3 in https://w3c.github.io/json-ld-api/#algorithm-3
else {
expandedValue = value;
}
// jsonld 1.1: 13.4.7.4 in https://w3c.github.io/json-ld-api/#algorithm-3
if (expandedValue == null) {
result.put(JsonLdConsts.VALUE, null);
continue;
Expand Down Expand Up @@ -766,6 +793,7 @@ else if (JsonLdConsts.REVERSE.equals(expandedProperty)) {
}
}
}
// GK: Also, `@included`, `@graph`, and `@direction`
// 7.4.11.4)
continue;
}
Expand All @@ -780,13 +808,26 @@ else if (frameExpansion && (JsonLdConsts.EXPLICIT.equals(expandedProperty)
}
// 7.4.12)
if (expandedValue != null) {
/* jsonld 1.1: 13.4.16 in https://w3c.github.io/json-ld-api/#algorithm-3
if (!(expandedValue == null && JsonLdConsts.VALUE.equals(expandedProperty)
&& (inputType == null || JsonLdConsts.JSON.equals(inputType)))) { */
result.put(expandedProperty, expandedValue);
}
// 7.4.13)
continue;
}
// jsonld 1.1: 13.5 in https://w3c.github.io/json-ld-api/#algorithm-3
String containerMapping = activeCtx.getContainer(key);
// jsonld 1.1: 13.6 in https://w3c.github.io/json-ld-api/#algorithm-3
if (activeCtx.getTermDefinition(key) != null
&& JsonLdConsts.JSON.equals(activeCtx.getTermDefinition(key).get(JsonLdConsts.TYPE))) {
Map<String, Object> newMap = newMap();
newMap.put(JsonLdConsts.VALUE, value);
newMap.put(JsonLdConsts.TYPE, JsonLdConsts.JSON);
expandedValue = newMap;
}
// 7.5
else if (JsonLdConsts.LANGUAGE.equals(activeCtx.getContainer(key))
else if (JsonLdConsts.LANGUAGE.equals(containerMapping)
&& value instanceof Map) {
// 7.5.1)
expandedValue = new ArrayList<Object>();
Expand All @@ -801,6 +842,10 @@ else if (JsonLdConsts.LANGUAGE.equals(activeCtx.getContainer(key))
}
// 7.5.2.2)
for (final Object item : (List<Object>) languageValue) {
// jsonld 1.1: 13.7.4.2.1 in https://w3c.github.io/json-ld-api/#expansion-algorithm
if(item == null) {
continue;
}
// 7.5.2.2.1)
if (!(item instanceof String)) {
throw new JsonLdError(Error.INVALID_LANGUAGE_MAP_VALUE,
Expand All @@ -815,9 +860,12 @@ else if (JsonLdConsts.LANGUAGE.equals(activeCtx.getContainer(key))
}
}
// 7.6)
// GK: Also a place to see if key is `@json` for JSON literals.
else if (JsonLdConsts.INDEX.equals(activeCtx.getContainer(key))
&& value instanceof Map) {
// 7.6.1)
// GK: `@index` also supports property indexing, if the term definition includes `@index`.
// GK: A map can also include `@none`.
expandedValue = new ArrayList<Object>();
// 7.6.2)
final List<String> indexKeys = new ArrayList<String>(
Expand Down Expand Up @@ -865,6 +913,7 @@ else if (JsonLdConsts.INDEX.equals(activeCtx.getContainer(key))
((Map<String, Object>) expandedValue).put(JsonLdConsts.LIST, tmp);
}
}
// GK: Other container possibilities including `@graph`, `@id`, and `@type` along with variations.
// 7.10)
if (activeCtx.isReverseProperty(key)) {
// 7.10.1)
Expand Down Expand Up @@ -937,8 +986,11 @@ else if (JsonLdConsts.INDEX.equals(activeCtx.getContainer(key))
// 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)
if (!(rval instanceof String) && result.containsKey(JsonLdConsts.LANGUAGE)) {
else if (!(rval instanceof String) && result.containsKey(JsonLdConsts.LANGUAGE)) {
throw new JsonLdError(Error.INVALID_LANGUAGE_TAGGED_VALUE,
"when @language is used, @value must be a string");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public final class JsonLdConsts {
public static final String RDF_OBJECT = RDF_SYNTAX_NS + "object";
public static final String RDF_LANGSTRING = RDF_SYNTAX_NS + "langString";
public static final String RDF_LIST = RDF_SYNTAX_NS + "List";
public static final String RDF_JSON = RDF_SYNTAX_NS + "JSON";

public static final String TEXT_TURTLE = "text/turtle";
public static final String APPLICATION_NQUADS = "application/n-quads"; // https://www.w3.org/TR/n-quads/#sec-mediatype
Expand Down Expand Up @@ -58,6 +59,13 @@ public final class JsonLdConsts {
public static final String VOCAB = "@vocab";
public static final String BASE = "@base";
public static final String REQUIRE_ALL = "@requireAll";
public static final String VERSION = "@version";
public static final String PROTECTED = "@protected";
public static final String PROPAGATE = "@propagate";
public static final String IMPORT = "@import";
public static final String DIRECTION = "@direction";
public static final String JSON = "@json";
public static final String ANY = "@any";

public enum Embed {
ALWAYS, NEVER, LAST, LINK;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,23 @@ public enum Error {

INVALID_EMBED_VALUE("invalid @embed value"),

INVALID_VERSION_VALUE("invalid @version value"),

PROCESSING_MODE_CONFLICT("processing mode conflict"),

// non spec related errors
SYNTAX_ERROR("syntax error"),

NOT_IMPLEMENTED("not implemnted"),
NOT_IMPLEMENTED("not implemented"),

UNKNOWN_FORMAT("unknown format"),

INVALID_INPUT("invalid input"),

PARSE_ERROR("parse error"),

INVALID_JSON_LITERAL("invalid JSON literal"),

UNKNOWN_ERROR("unknown error");

private final String error;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public JsonLdOptions copy() {
* http://www.w3.org/TR/json-ld-api/#widl-JsonLdOptions-processingMode
* jsonld 1.1: https://www.w3.org/TR/json-ld11/#dfn-processing-mode
*/
private String processingMode = JSON_LD_1_0;
private String processingMode = JSON_LD_1_1;
/**
* http://www.w3.org/TR/json-ld-api/#widl-JsonLdOptions-documentLoader
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ static boolean isKeyword(Object key) {
if (!isString(key)) {
return false;
}
// GK: Note that this set is somewhat dependent on the processing mode.
return "@base".equals(key) || "@context".equals(key) || "@container".equals(key)
|| "@default".equals(key) || "@embed".equals(key) || "@explicit".equals(key)
|| "@graph".equals(key) || "@id".equals(key) || "@index".equals(key)
|| "@language".equals(key) || "@list".equals(key) || "@omitDefault".equals(key)
|| "@reverse".equals(key) || "@preserve".equals(key) || "@set".equals(key)
|| "@type".equals(key) || "@value".equals(key) || "@vocab".equals(key)
|| "@requireAll".equals(key);
|| "@requireAll".equals(key) || "@version".equals(key)|| "@protected".equals(key)
|| "@propagate".equals(key)|| "@import".equals(key)|| "@direction".equals(key)
|| "@json".equals(key) || "@none".equals(key) || "@included".equals(key)
|| "@nest".equals(key) || "@prefix".equals(key);
}

public static Boolean deepCompare(Object v1, Object v2, Boolean listOrderMatters) {
Expand Down
29 changes: 27 additions & 2 deletions core/src/main/java/com/github/jsonldjava/core/RDFDataset.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static com.github.jsonldjava.core.JsonLdConsts.RDF_NIL;
import static com.github.jsonldjava.core.JsonLdConsts.RDF_REST;
import static com.github.jsonldjava.core.JsonLdConsts.RDF_TYPE;
import static com.github.jsonldjava.core.JsonLdConsts.RDF_JSON;
import static com.github.jsonldjava.core.JsonLdConsts.XSD_BOOLEAN;
import static com.github.jsonldjava.core.JsonLdConsts.XSD_DECIMAL;
import static com.github.jsonldjava.core.JsonLdConsts.XSD_DOUBLE;
Expand All @@ -17,6 +18,7 @@
import static com.github.jsonldjava.core.JsonLdUtils.isValue;
import static com.github.jsonldjava.utils.Obj.newMap;

import java.io.IOException;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
Expand All @@ -28,6 +30,8 @@
import java.util.Set;
import java.util.regex.Pattern;

import com.github.jsonldjava.utils.JsonUtils;

/**
* Starting to migrate away from using plain java Maps as the internal RDF
* dataset store. Currently each item just wraps a Map based on the old format
Expand Down Expand Up @@ -240,7 +244,18 @@ Map<String, Object> toObject(Boolean useNativeTypes) throws JsonLdError {
else {
rval.put("@type", type);
}
} else if (!XSD_STRING.equals(type)) {
}
// jsonld 1.1: 2.5 in https://w3c.github.io/json-ld-api/#algorithm-16
else if(RDF_JSON.equals(type)) {
rval.put("@type", "@json");
try {
rval.put("@value", JsonUtils.fromString(value));
} catch (IOException e) {
e.printStackTrace();
throw new JsonLdError(JsonLdError.Error.INVALID_JSON_LITERAL, value, e);
}
}
else if (!XSD_STRING.equals(type)) {
rval.put("@type", type);
}
}
Expand Down Expand Up @@ -684,7 +699,17 @@ private Node objectToRDF(Object item) {
return new Literal((String) value,
datatype == null ? RDF_LANGSTRING : (String) datatype,
(String) ((Map<String, Object>) item).get("@language"));
} else {
}
// 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