Skip to content

Commit 469fe0c

Browse files
committed
Support literal JSON.
1 parent b6ea7c3 commit 469fe0c

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+
### Added
4+
- Support literal JSON.
5+
- Use JCS node-es6 canonicalize code from
6+
https://github.com/cyberphone/json-canonicalization/
7+
38
## 1.7.0 - 2019-08-30
49

510
### Added

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
@@ -669,6 +669,12 @@ function _expandObject({
669669
insideList: isList,
670670
expansionMap
671671
});
672+
} else if(
673+
_getContextValue(activeCtx, key, '@type') === '@json') {
674+
expandedValue = {
675+
'@type': '@json',
676+
'@value': value
677+
};
672678
} else {
673679
// recursively expand value with key as new active property
674680
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

@@ -285,6 +286,10 @@ function _RDFToObject(o, useNativeTypes) {
285286
if(!type) {
286287
type = XSD_STRING;
287288
}
289+
if(type === RDF_JSON_LITERAL) {
290+
type = '@json';
291+
rval['@value'] = JSON.parse(rval['@value']);
292+
}
288293
// use native types for certain xsd types
289294
if(useNativeTypes) {
290295
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

@@ -224,8 +226,11 @@ function _objectToRDF(item, issuer, dataset, graphTerm) {
224226
let value = item['@value'];
225227
const datatype = item['@type'] || null;
226228

227-
// convert to XSD datatypes as appropriate
228-
if(types.isBoolean(value)) {
229+
// convert to XSD/JSON datatypes as appropriate
230+
if(datatype === '@json') {
231+
object.value = jsonCanonicalize(value);
232+
object.datatype.value = RDF_JSON_LITERAL;
233+
} else if(types.isBoolean(value)) {
229234
object.value = value.toString();
230235
object.datatype.value = datatype || XSD_BOOLEAN;
231236
} 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
@@ -60,16 +60,6 @@ const TEST_TYPES = {
6060
/compact-manifest.jsonld#tpi04$/,
6161
/compact-manifest.jsonld#tpi05$/,
6262
/compact-manifest.jsonld#tpi06$/,
63-
// JSON literals
64-
/compact-manifest.jsonld#tjs01$/,
65-
/compact-manifest.jsonld#tjs02$/,
66-
/compact-manifest.jsonld#tjs03$/,
67-
/compact-manifest.jsonld#tjs04$/,
68-
/compact-manifest.jsonld#tjs05$/,
69-
/compact-manifest.jsonld#tjs06$/,
70-
/compact-manifest.jsonld#tjs07$/,
71-
/compact-manifest.jsonld#tjs08$/,
72-
/compact-manifest.jsonld#tjs09$/,
7363
// IRI confusion
7464
/compact-manifest.jsonld#te002$/,
7565
// @propogate
@@ -171,18 +161,6 @@ const TEST_TYPES = {
171161
/expand-manifest.jsonld#tpi09$/,
172162
/expand-manifest.jsonld#tpi10$/,
173163
/expand-manifest.jsonld#tpi11$/,
174-
// JSON literals
175-
/expand-manifest.jsonld#tjs01$/,
176-
/expand-manifest.jsonld#tjs02$/,
177-
/expand-manifest.jsonld#tjs03$/,
178-
/expand-manifest.jsonld#tjs04$/,
179-
/expand-manifest.jsonld#tjs05$/,
180-
/expand-manifest.jsonld#tjs06$/,
181-
/expand-manifest.jsonld#tjs07$/,
182-
/expand-manifest.jsonld#tjs08$/,
183-
/expand-manifest.jsonld#tjs09$/,
184-
/expand-manifest.jsonld#tjs10$/,
185-
/expand-manifest.jsonld#tjs11$/,
186164
// misc
187165
/expand-manifest.jsonld#te043$/,
188166
/expand-manifest.jsonld#te044$/,
@@ -336,14 +314,6 @@ const TEST_TYPES = {
336314
specVersion: ['json-ld-1.0'],
337315
// FIXME
338316
idRegex: [
339-
// JSON literals
340-
/fromRdf-manifest.jsonld#tjs01$/,
341-
/fromRdf-manifest.jsonld#tjs02$/,
342-
/fromRdf-manifest.jsonld#tjs03$/,
343-
/fromRdf-manifest.jsonld#tjs04$/,
344-
/fromRdf-manifest.jsonld#tjs05$/,
345-
/fromRdf-manifest.jsonld#tjs06$/,
346-
/fromRdf-manifest.jsonld#tjs07$/,
347317
]
348318
},
349319
fn: 'fromRDF',
@@ -406,20 +376,6 @@ const TEST_TYPES = {
406376
/html-manifest.jsonld#tr020$/,
407377
/html-manifest.jsonld#tr021$/,
408378
/html-manifest.jsonld#tr022$/,
409-
// JSON literal
410-
/toRdf-manifest.jsonld#tjs01$/,
411-
/toRdf-manifest.jsonld#tjs02$/,
412-
/toRdf-manifest.jsonld#tjs03$/,
413-
/toRdf-manifest.jsonld#tjs04$/,
414-
/toRdf-manifest.jsonld#tjs05$/,
415-
/toRdf-manifest.jsonld#tjs06$/,
416-
/toRdf-manifest.jsonld#tjs07$/,
417-
/toRdf-manifest.jsonld#tjs08$/,
418-
/toRdf-manifest.jsonld#tjs09$/,
419-
/toRdf-manifest.jsonld#tjs10$/,
420-
/toRdf-manifest.jsonld#tjs11$/,
421-
/toRdf-manifest.jsonld#tjs12$/,
422-
/toRdf-manifest.jsonld#tjs13$/,
423379
// number fixes
424380
/toRdf-manifest.jsonld#trt01$/,
425381
// IRI resolution

0 commit comments

Comments
 (0)