Skip to content

Commit b2cc749

Browse files
committed
Support literal JSON.
1 parent 865d757 commit b2cc749

File tree

8 files changed

+90
-47
lines changed

8 files changed

+90
-47
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# jsonld ChangeLog
22

3+
### Fixed
4+
- Support literal JSON.
5+
- Use JCS node-es6 canonicalize code from
6+
https://github.com/cyberphone/json-canonicalization/
7+
38
## 1.6.2 - 2019-05-21
49

510
### Fixed

lib/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module.exports = {
1717
RDF_TYPE: RDF + 'type',
1818
RDF_PLAIN_LITERAL: RDF + 'PlainLiteral',
1919
RDF_XML_LITERAL: RDF + 'XMLLiteral',
20+
RDF_JSON_LITERAL: RDF + 'JSON',
2021
RDF_OBJECT: RDF + 'object',
2122
RDF_LANGSTRING: RDF + 'langString',
2223

lib/context.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ api.createTermDefinition = (
499499
{code: 'invalid type mapping', context: localCtx});
500500
}
501501

502-
if(type !== '@id' && type !== '@vocab') {
502+
if(type !== '@id' && type !== '@vocab' && type !== '@json') {
503503
// expand @type to full IRI
504504
type = _expandIri(
505505
activeCtx, type, {vocab: true, base: false}, localCtx, defined,

lib/expand.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,12 @@ function _expandObject({
677677
'Invalid JSON-LD syntax; lists of lists are not permitted.',
678678
'jsonld.SyntaxError', {code: 'list of lists'});
679679
}
680+
} else if(
681+
_getContextValue(activeCtx, key, '@type') === '@json') {
682+
expandedValue = {
683+
'@type': '@json',
684+
'@value': value
685+
};
680686
} else {
681687
// recursively expand value with key as new active property
682688
expandedValue = api.expand({

lib/fromRdf.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const {
1717
RDF_TYPE,
1818
// RDF_PLAIN_LITERAL,
1919
// RDF_XML_LITERAL,
20+
RDF_JSON_LITERAL,
2021
// RDF_OBJECT,
2122
// RDF_LANGSTRING,
2223

@@ -303,6 +304,10 @@ function _RDFToObject(o, useNativeTypes) {
303304
if(!type) {
304305
type = XSD_STRING;
305306
}
307+
if(type === RDF_JSON_LITERAL) {
308+
type = '@json';
309+
rval['@value'] = JSON.parse(rval['@value']);
310+
}
306311
// use native types for certain xsd types
307312
if(useNativeTypes) {
308313
if(type === XSD_BOOLEAN) {

lib/json-canonicalize.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// https://github.com/cyberphone/json-canonicalization
2+
// https://www.apache.org/licenses/LICENSE-2.0
3+
/* eslint-disable */
4+
5+
// ES6 based JSON canonicalizer
6+
'use strict';
7+
var canonicalize = function(object) {
8+
9+
var buffer = '';
10+
serialize(object);
11+
return buffer;
12+
13+
function serialize(object) {
14+
if (object === null || typeof object !== 'object' ||
15+
object.toJSON != null) {
16+
/////////////////////////////////////////////////
17+
// Primitive type or toJSON - Use ES6/JSON //
18+
/////////////////////////////////////////////////
19+
buffer += JSON.stringify(object);
20+
21+
} else if (Array.isArray(object)) {
22+
/////////////////////////////////////////////////
23+
// Array - Maintain element order //
24+
/////////////////////////////////////////////////
25+
buffer += '[';
26+
let next = false;
27+
object.forEach((element) => {
28+
if (next) {
29+
buffer += ',';
30+
}
31+
next = true;
32+
/////////////////////////////////////////
33+
// Array element - Recursive expansion //
34+
/////////////////////////////////////////
35+
serialize(element);
36+
});
37+
buffer += ']';
38+
39+
} else {
40+
/////////////////////////////////////////////////
41+
// Object - Sort properties before serializing //
42+
/////////////////////////////////////////////////
43+
buffer += '{';
44+
let next = false;
45+
Object.keys(object).sort().forEach((property) => {
46+
if (next) {
47+
buffer += ',';
48+
}
49+
next = true;
50+
///////////////////////////////////////////////
51+
// Property names are strings - Use ES6/JSON //
52+
///////////////////////////////////////////////
53+
buffer += JSON.stringify(property);
54+
buffer += ':';
55+
//////////////////////////////////////////
56+
// Property value - Recursive expansion //
57+
//////////////////////////////////////////
58+
serialize(object[property]);
59+
});
60+
buffer += '}';
61+
}
62+
}
63+
};
64+
65+
module.exports = canonicalize;

lib/toRdf.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
const {createNodeMap} = require('./nodeMap');
77
const {isKeyword} = require('./context');
88
const graphTypes = require('./graphTypes');
9+
const jsonCanonicalize = require('./json-canonicalize');
910
const types = require('./types');
1011
const util = require('./util');
1112

@@ -18,6 +19,7 @@ const {
1819
RDF_TYPE,
1920
// RDF_PLAIN_LITERAL,
2021
// RDF_XML_LITERAL,
22+
RDF_JSON_LITERAL,
2123
// RDF_OBJECT,
2224
RDF_LANGSTRING,
2325

@@ -219,8 +221,11 @@ function _objectToRDF(item) {
219221
let value = item['@value'];
220222
const datatype = item['@type'] || null;
221223

222-
// convert to XSD datatypes as appropriate
223-
if(types.isBoolean(value)) {
224+
// convert to XSD/JSON datatypes as appropriate
225+
if(datatype === '@json') {
226+
object.value = jsonCanonicalize(value);
227+
object.datatype.value = RDF_JSON_LITERAL;
228+
} else if(types.isBoolean(value)) {
224229
object.value = value.toString();
225230
object.datatype.value = datatype || XSD_BOOLEAN;
226231
} else if(types.isDouble(value) || datatype === XSD_DOUBLE) {

tests/test-common.js

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,6 @@ const TEST_TYPES = {
6868
/compact-manifest.jsonld#tpi04$/,
6969
/compact-manifest.jsonld#tpi05$/,
7070
/compact-manifest.jsonld#tpi06$/,
71-
// JSON literals
72-
/compact-manifest.jsonld#tjs01$/,
73-
/compact-manifest.jsonld#tjs02$/,
74-
/compact-manifest.jsonld#tjs03$/,
75-
/compact-manifest.jsonld#tjs04$/,
76-
/compact-manifest.jsonld#tjs05$/,
77-
/compact-manifest.jsonld#tjs06$/,
78-
/compact-manifest.jsonld#tjs07$/,
79-
/compact-manifest.jsonld#tjs08$/,
80-
/compact-manifest.jsonld#tjs09$/,
8171
// IRI confusion
8272
/compact-manifest.jsonld#te002$/,
8373
// @propogate
@@ -173,18 +163,6 @@ const TEST_TYPES = {
173163
/expand-manifest.jsonld#tpi09$/,
174164
/expand-manifest.jsonld#tpi10$/,
175165
/expand-manifest.jsonld#tpi11$/,
176-
// JSON literals
177-
/expand-manifest.jsonld#tjs01$/,
178-
/expand-manifest.jsonld#tjs02$/,
179-
/expand-manifest.jsonld#tjs03$/,
180-
/expand-manifest.jsonld#tjs04$/,
181-
/expand-manifest.jsonld#tjs05$/,
182-
/expand-manifest.jsonld#tjs06$/,
183-
/expand-manifest.jsonld#tjs07$/,
184-
/expand-manifest.jsonld#tjs08$/,
185-
/expand-manifest.jsonld#tjs09$/,
186-
/expand-manifest.jsonld#tjs10$/,
187-
/expand-manifest.jsonld#tjs11$/,
188166
// misc
189167
/expand-manifest.jsonld#te043$/,
190168
/expand-manifest.jsonld#te044$/,
@@ -337,14 +315,6 @@ const TEST_TYPES = {
337315
/fromRdf-manifest.jsonld#tli01$/,
338316
/fromRdf-manifest.jsonld#tli02$/,
339317
/fromRdf-manifest.jsonld#tli03$/,
340-
// JSON literals
341-
/fromRdf-manifest.jsonld#tjs01$/,
342-
/fromRdf-manifest.jsonld#tjs02$/,
343-
/fromRdf-manifest.jsonld#tjs03$/,
344-
/fromRdf-manifest.jsonld#tjs04$/,
345-
/fromRdf-manifest.jsonld#tjs05$/,
346-
/fromRdf-manifest.jsonld#tjs06$/,
347-
/fromRdf-manifest.jsonld#tjs07$/,
348318
]
349319
},
350320
fn: 'fromRDF',
@@ -400,20 +370,6 @@ const TEST_TYPES = {
400370
/html-manifest.jsonld#tr020$/,
401371
/html-manifest.jsonld#tr021$/,
402372
/html-manifest.jsonld#tr022$/,
403-
// JSON literal
404-
/toRdf-manifest.jsonld#tjs01$/,
405-
/toRdf-manifest.jsonld#tjs02$/,
406-
/toRdf-manifest.jsonld#tjs03$/,
407-
/toRdf-manifest.jsonld#tjs04$/,
408-
/toRdf-manifest.jsonld#tjs05$/,
409-
/toRdf-manifest.jsonld#tjs06$/,
410-
/toRdf-manifest.jsonld#tjs07$/,
411-
/toRdf-manifest.jsonld#tjs08$/,
412-
/toRdf-manifest.jsonld#tjs09$/,
413-
/toRdf-manifest.jsonld#tjs10$/,
414-
/toRdf-manifest.jsonld#tjs11$/,
415-
/toRdf-manifest.jsonld#tjs12$/,
416-
/toRdf-manifest.jsonld#tjs13$/,
417373
// number fixes
418374
/toRdf-manifest.jsonld#trt01$/,
419375
// IRI resolution

0 commit comments

Comments
 (0)