Skip to content

Commit 73e9552

Browse files
committed
Expand JSON literals.
1 parent 28b4191 commit 73e9552

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

lib/context.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,7 @@ api.isKeyword = v => {
11141114
case '@graph':
11151115
case '@id':
11161116
case '@index':
1117+
case '@json':
11171118
case '@language':
11181119
case '@list':
11191120
case '@nest':

lib/expand.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ const {
2323
expandIri: _expandIri,
2424
getContextValue: _getContextValue,
2525
isKeyword: _isKeyword,
26-
process: _processContext
26+
process: _processContext,
27+
processingMode: _processingMode
2728
} = require('./context');
2829

2930
const {
@@ -289,6 +290,8 @@ api.expand = ({
289290
'Invalid JSON-LD syntax; only strings may be language-tagged.',
290291
'jsonld.SyntaxError',
291292
{code: 'invalid language-tagged value', element: rval});
293+
} else if(_processingMode(activeCtx, 1.1) && types.includes('@json') && types.length === 1) {
294+
// Any value of @value is okay if @type: @json
292295
} else if(!types.every(t =>
293296
(_isAbsoluteIri(t) && !(_isString(t) && t.indexOf('_:') === 0) ||
294297
_isEmptyObject(t)))) {
@@ -494,16 +497,7 @@ function _expandObject({
494497
'jsonld.SyntaxError', {code: 'invalid @graph value', value});
495498
}
496499

497-
// @value must not be an object or an array (unless framing)
498500
if(expandedProperty === '@value') {
499-
if((_isObject(value) || _isArray(value)) && !options.isFrame) {
500-
throw new JsonLdError(
501-
'Invalid JSON-LD syntax; "@value" value must not be an ' +
502-
'object or an array.',
503-
'jsonld.SyntaxError',
504-
{code: 'invalid value object value', value});
505-
}
506-
507501
_addValue(
508502
expandedParent, '@value', value, {propertyIsArray: options.isFrame});
509503
continue;
@@ -753,6 +747,20 @@ function _expandObject({
753747
});
754748
}
755749

750+
// @value must not be an object or an array (unless framing) or if @type is @json
751+
if('@value' in element) {
752+
const value = element['@value'];
753+
if(element['@type'] === '@json' && _processingMode(activeCtx, 1.1)) {
754+
// Sllow any value, to be verified when the object is fully expanded and the @type is @json.
755+
} else if((_isObject(value) || _isArray(value)) && !options.isFrame) {
756+
throw new JsonLdError(
757+
'Invalid JSON-LD syntax; "@value" value must not be an ' +
758+
'object or an array.',
759+
'jsonld.SyntaxError',
760+
{code: 'invalid value object value', value});
761+
}
762+
}
763+
756764
// expand each nested key
757765
for(const key of nests) {
758766
const nestedValues = _isArray(element[key]) ? element[key] : [element[key]];

0 commit comments

Comments
 (0)