GeezerVison is showing github.com — Support JSON literals · jsonld-java/jsonld-java@7aeae88
File treeExpand file treeCollapse file tree Open diff view settings core/src/main/java/com/github/jsonldjava/core
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: core/src/main/java/com/github/jsonldjava/core/Context.java +7-5Lines changed: 7 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -384,9 +384,14 @@ private void createTermDefinition(Map<String, Object> context, String term, | 384 | 384 | } | 385 | 385 | throw new JsonLdError(Error.INVALID_TYPE_MAPPING, type, error); | 386 | 386 | } | | 387 | + // jsonld 1.1: 13.3 in https://w3c.github.io/json-ld-api/#algorithm-0 | | 388 | + if (JsonLdOptions.JSON_LD_1_0.equals(options.getProcessingMode()) | | 389 | + && (JsonLdConsts.NONE.equals(type) || JsonLdConsts.JSON.equals(type))) { | | 390 | + throw new JsonLdError(Error.INVALID_TYPE_MAPPING, type); | | 391 | + } | 387 | 392 | // TODO: fix check for absoluteIri (blank nodes shouldn't count, at | 388 | 393 | // least not here!) | 389 | | - if (JsonLdConsts.ID.equals(type) || JsonLdConsts.VOCAB.equals(type) | | 394 | + if (JsonLdConsts.ID.equals(type) || JsonLdConsts.VOCAB.equals(type) || JsonLdConsts.JSON.equals(type) | 390 | 395 | || (!type.startsWith(JsonLdConsts.BLANK_NODE_PREFIX) | 391 | 396 | && JsonLdUtils.isAbsoluteIri(type))) { | 392 | 397 | definition.put(JsonLdConsts.TYPE, type); | @@ -522,8 +527,7 @@ private String selectContainer(final List<?> allContainers) { | 522 | 527 | return (String) supportedContainer.orElse(null); | 523 | 528 | } | 524 | 529 |
| 525 | | - // jsonld 1.1: 22.1 in | 526 | | - // https://w3c.github.io/json-ld-api/#create-term-definition | | 530 | + // jsonld 1.1: 22.1 in https://w3c.github.io/json-ld-api/#create-term-definition | 527 | 531 | private List<?> checkValidContainerEntry(final Object containerObject) { | 528 | 532 | List<?> container = (List<?>) (containerObject instanceof List ? containerObject | 529 | 533 | : Arrays.asList(containerObject)); | @@ -607,8 +611,6 @@ String expandIri(String value, boolean relative, boolean vocab, Map<String, Obje | 607 | 611 | // 6) | 608 | 612 | else if (relative) { | 609 | 613 | return JsonLdUrl.resolve((String) this.get(JsonLdConsts.BASE), value); | 610 | | - } else if (context != null && JsonLdUtils.isRelativeIri(value)) { | 611 | | - throw new JsonLdError(Error.INVALID_IRI_MAPPING, "not an absolute IRI: " + value); | 612 | 614 | } | 613 | 615 | // 7) | 614 | 616 | return value; | |
Collapse file Copy file name to clipboardExpand all lines: core/src/main/java/com/github/jsonldjava/core/JsonLdApi.java +33-4Lines changed: 33 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -188,7 +188,12 @@ public Object compact(Context activeCtx, String activeProperty, Object element, | 188 | 188 | // 4 | 189 | 189 | if (elem.containsKey(JsonLdConsts.VALUE) || elem.containsKey(JsonLdConsts.ID)) { | 190 | 190 | final Object compactedValue = activeCtx.compactValue(activeProperty, elem); | 191 | | - if (!(compactedValue instanceof Map || compactedValue instanceof List)) { | | 191 | + boolean isScalar = !(compactedValue instanceof Map || compactedValue instanceof List); | | 192 | + // jsonld 1.1: 7 in https://w3c.github.io/json-ld-api/#algorithm-6 | | 193 | + boolean isJson = activeCtx.getTermDefinition(activeProperty) != null | | 194 | + && JsonLdConsts.JSON.equals( | | 195 | + activeCtx.getTermDefinition(activeProperty).get(JsonLdConsts.TYPE)); | | 196 | + if (isScalar || isJson) { | 192 | 197 | return compactedValue; | 193 | 198 | } | 194 | 199 | } | @@ -580,6 +585,8 @@ else if (element instanceof Map) { | 580 | 585 | throw new JsonLdError(Error.COLLIDING_KEYWORDS, | 581 | 586 | expandedProperty + " already exists in result"); | 582 | 587 | } | | 588 | + // jsonld 1.1: 12 in https://w3c.github.io/json-ld-api/#algorithm-3 | | 589 | + Object inputType = elem.get(JsonLdConsts.TYPE); | 583 | 590 | // 7.4.3) | 584 | 591 | if (JsonLdConsts.ID.equals(expandedProperty)) { | 585 | 592 | if (value instanceof String) { | @@ -645,6 +652,13 @@ else if (JsonLdConsts.GRAPH.equals(expandedProperty)) { | 645 | 652 | } | 646 | 653 | // 7.4.6) | 647 | 654 | else if (JsonLdConsts.VALUE.equals(expandedProperty)) { | | 655 | + // jsonld 1.1: 13.4.7.1 in https://w3c.github.io/json-ld-api/#algorithm-3 | | 656 | + if(JsonLdConsts.JSON.equals(inputType)) { | | 657 | + expandedValue = value; | | 658 | + if(opts.getProcessingMode().equals(JsonLdOptions.JSON_LD_1_0)) { | | 659 | + throw new JsonLdError(Error.INVALID_VALUE_OBJECT_VALUE, value); | | 660 | + } | | 661 | + } | 648 | 662 | if (value != null && (value instanceof Map || value instanceof List)) { | 649 | 663 | throw new JsonLdError(Error.INVALID_VALUE_OBJECT_VALUE, | 650 | 664 | "value of " + expandedProperty + " must be a scalar or null"); | @@ -779,14 +793,26 @@ else if (frameExpansion && (JsonLdConsts.EXPLICIT.equals(expandedProperty) | 779 | 793 | expandedValue = expand(activeCtx, expandedProperty, value); | 780 | 794 | } | 781 | 795 | // 7.4.12) | 782 | | - if (expandedValue != null) { | | 796 | + // jsonld 1.1: 13.4.16 in https://w3c.github.io/json-ld-api/#algorithm-3 | | 797 | + if (!(expandedValue == null && JsonLdConsts.VALUE.equals(expandedProperty) | | 798 | + && (inputType == null || JsonLdConsts.JSON.equals(inputType)))) { | 783 | 799 | result.put(expandedProperty, expandedValue); | 784 | 800 | } | 785 | 801 | // 7.4.13) | 786 | 802 | continue; | 787 | 803 | } | | 804 | + // jsonld 1.1: 13.5 in https://w3c.github.io/json-ld-api/#algorithm-3 | | 805 | + String containerMapping = activeCtx.getContainer(key); | | 806 | + // jsonld 1.1: 13.6 in https://w3c.github.io/json-ld-api/#algorithm-3 | | 807 | + if (activeCtx.getTermDefinition(key) != null | | 808 | + && JsonLdConsts.JSON.equals(activeCtx.getTermDefinition(key).get(JsonLdConsts.TYPE))) { | | 809 | + Map<String, Object> newMap = newMap(); | | 810 | + newMap.put(JsonLdConsts.VALUE, value); | | 811 | + newMap.put(JsonLdConsts.TYPE, JsonLdConsts.JSON); | | 812 | + expandedValue = newMap; | | 813 | + } | 788 | 814 | // 7.5 | 789 | | - else if (JsonLdConsts.LANGUAGE.equals(activeCtx.getContainer(key)) | | 815 | + else if (JsonLdConsts.LANGUAGE.equals(containerMapping) | 790 | 816 | && value instanceof Map) { | 791 | 817 | // 7.5.1) | 792 | 818 | expandedValue = new ArrayList<Object>(); | @@ -937,8 +963,11 @@ else if (JsonLdConsts.INDEX.equals(activeCtx.getContainer(key)) | 937 | 963 | // null, so simply return it | 938 | 964 | return null; | 939 | 965 | } | | 966 | + else if (result.getOrDefault(JsonLdConsts.TYPE,"").equals(JsonLdConsts.JSON)) { | | 967 | + // jsonld 1.1: 14.3 in https://w3c.github.io/json-ld-api/#algorithm-3 | | 968 | + } | 940 | 969 | // 8.3) | 941 | | - if (!(rval instanceof String) && result.containsKey(JsonLdConsts.LANGUAGE)) { | | 970 | + else if (!(rval instanceof String) && result.containsKey(JsonLdConsts.LANGUAGE)) { | 942 | 971 | throw new JsonLdError(Error.INVALID_LANGUAGE_TAGGED_VALUE, | 943 | 972 | "when @language is used, @value must be a string"); | 944 | 973 | } | |
Collapse file Copy file name to clipboardExpand all lines: core/src/main/java/com/github/jsonldjava/core/JsonLdConsts.java +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -27,6 +27,7 @@ public final class JsonLdConsts { | 27 | 27 | public static final String RDF_OBJECT = RDF_SYNTAX_NS + "object"; | 28 | 28 | public static final String RDF_LANGSTRING = RDF_SYNTAX_NS + "langString"; | 29 | 29 | public static final String RDF_LIST = RDF_SYNTAX_NS + "List"; | | 30 | + public static final String RDF_JSON = RDF_SYNTAX_NS + "JSON"; | 30 | 31 |
| 31 | 32 | public static final String TEXT_TURTLE = "text/turtle"; | 32 | 33 | public static final String APPLICATION_NQUADS = "application/n-quads"; // https://www.w3.org/TR/n-quads/#sec-mediatype | @@ -63,6 +64,7 @@ public final class JsonLdConsts { | 63 | 64 | public static final String PROPAGATE = "@propagate"; | 64 | 65 | public static final String IMPORT = "@import"; | 65 | 66 | public static final String DIRECTION = "@direction"; | | 67 | + public static final String JSON = "@json"; | 66 | 68 |
| 67 | 69 | public enum Embed { | 68 | 70 | ALWAYS, NEVER, LAST, LINK; | |
Collapse file Copy file name to clipboardExpand all lines: core/src/main/java/com/github/jsonldjava/core/JsonLdError.java +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -117,6 +117,8 @@ public enum Error { | 117 | 117 |
| 118 | 118 | PARSE_ERROR("parse error"), | 119 | 119 |
| | 120 | + INVALID_JSON_LITERAL("invalid JSON literal"), | | 121 | + | 120 | 122 | UNKNOWN_ERROR("unknown error"); | 121 | 123 |
| 122 | 124 | private final String error; | |
Collapse file Copy file name to clipboardExpand all lines: core/src/main/java/com/github/jsonldjava/core/JsonLdUtils.java +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,7 +32,9 @@ static boolean isKeyword(Object key) { | 32 | 32 | || "@language".equals(key) || "@list".equals(key) || "@omitDefault".equals(key) | 33 | 33 | || "@reverse".equals(key) || "@preserve".equals(key) || "@set".equals(key) | 34 | 34 | || "@type".equals(key) || "@value".equals(key) || "@vocab".equals(key) | 35 | | - || "@requireAll".equals(key); | | 35 | + || "@requireAll".equals(key) || "@version".equals(key)|| "@protected".equals(key) | | 36 | + || "@propagate".equals(key)|| "@import".equals(key)|| "@direction".equals(key) | | 37 | + || "@json".equals(key); | 36 | 38 | } | 37 | 39 |
| 38 | 40 | public static Boolean deepCompare(Object v1, Object v2, Boolean listOrderMatters) { | |
Collapse file Copy file name to clipboardExpand all lines: core/src/main/java/com/github/jsonldjava/core/RDFDataset.java +27-2Lines changed: 27 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 5 | 5 | import static com.github.jsonldjava.core.JsonLdConsts.RDF_NIL; | 6 | 6 | import static com.github.jsonldjava.core.JsonLdConsts.RDF_REST; | 7 | 7 | import static com.github.jsonldjava.core.JsonLdConsts.RDF_TYPE; | | 8 | +import static com.github.jsonldjava.core.JsonLdConsts.RDF_JSON; | 8 | 9 | import static com.github.jsonldjava.core.JsonLdConsts.XSD_BOOLEAN; | 9 | 10 | import static com.github.jsonldjava.core.JsonLdConsts.XSD_DOUBLE; | 10 | 11 | import static com.github.jsonldjava.core.JsonLdConsts.XSD_INTEGER; | | 16 | 17 | import static com.github.jsonldjava.core.JsonLdUtils.isValue; | 17 | 18 | import static com.github.jsonldjava.utils.Obj.newMap; | 18 | 19 |
| | 20 | +import java.io.IOException; | 19 | 21 | import java.text.DecimalFormat; | 20 | 22 | import java.text.DecimalFormatSymbols; | 21 | 23 | import java.util.ArrayList; | | 27 | 29 | import java.util.Set; | 28 | 30 | import java.util.regex.Pattern; | 29 | 31 |
| | 32 | +import com.github.jsonldjava.utils.JsonUtils; | | 33 | + | 30 | 34 | /** | 31 | 35 | * Starting to migrate away from using plain java Maps as the internal RDF | 32 | 36 | * dataset store. Currently each item just wraps a Map based on the old format | @@ -239,7 +243,18 @@ Map<String, Object> toObject(Boolean useNativeTypes) throws JsonLdError { | 239 | 243 | else { | 240 | 244 | rval.put("@type", type); | 241 | 245 | } | 242 | | - } else if (!XSD_STRING.equals(type)) { | | 246 | + } | | 247 | + // jsonld 1.1: 2.5 in https://w3c.github.io/json-ld-api/#algorithm-16 | | 248 | + else if(RDF_JSON.equals(type)) { | | 249 | + rval.put("@type", "@json"); | | 250 | + try { | | 251 | + rval.put("@value", JsonUtils.fromString(value)); | | 252 | + } catch (IOException e) { | | 253 | + e.printStackTrace(); | | 254 | + throw new JsonLdError(JsonLdError.Error.INVALID_JSON_LITERAL, value, e); | | 255 | + } | | 256 | + } | | 257 | + else if (!XSD_STRING.equals(type)) { | 243 | 258 | rval.put("@type", type); | 244 | 259 | } | 245 | 260 | } | @@ -680,7 +695,17 @@ private Node objectToRDF(Object item) { | 680 | 695 | return new Literal((String) value, | 681 | 696 | datatype == null ? RDF_LANGSTRING : (String) datatype, | 682 | 697 | (String) ((Map<String, Object>) item).get("@language")); | 683 | | - } else { | | 698 | + } | | 699 | + // jsonld 1.1: 8 in https://w3c.github.io/json-ld-api/#algorithm-13 | | 700 | + else if(JsonLdConsts.JSON.equals(datatype)) { | | 701 | + try { | | 702 | + return new Literal(JsonUtils.toString(value), RDF_JSON, null); | | 703 | + } catch (IOException e) { | | 704 | + e.printStackTrace(); | | 705 | + return null; | | 706 | + } | | 707 | + } | | 708 | + else { | 684 | 709 | return new Literal((String) value, | 685 | 710 | datatype == null ? XSD_STRING : (String) datatype, null); | 686 | 711 | } | |
|