Update to latest jsonld.js, revert tests, add new expand test.
--- a/playground/jsonld.js Wed Jan 11 16:26:56 2012 -0800
+++ b/playground/jsonld.js Wed Jan 11 21:51:27 2012 -0500
@@ -3,7 +3,7 @@
*
* @author Dave Longley
*
- * Copyright (c) 2011 Digital Bazaar, Inc. All rights reserved.
+ * Copyright (c) 2011-2012 Digital Bazaar, Inc. All rights reserved.
*/
(function()
{
@@ -156,7 +156,7 @@
{
'@id': '@id',
'@language': '@language',
- '@literal': '@literal',
+ '@value': '@value',
'@type': '@type'
};
@@ -349,21 +349,35 @@
};
/**
- * Sorts the keys in a context.
+ * Sorts the keys in a object.
*
- * @param ctx the context to sort.
+ * @param obj the object to sort.
*
- * @return the sorted context.
+ * @return the sorted object.
*/
-var _sortContextKeys = function(ctx)
+var _sortKeys = function(obj)
{
- // sort keys
- var rval = {};
- var keys = Object.keys(ctx).sort();
- for(var k in keys)
+ var rval = obj;
+ if(obj !== null)
{
- var key = keys[k];
- rval[key] = ctx[key];
+ if(obj.constructor === Array)
+ {
+ rval = [];
+ for(var i in obj)
+ {
+ rval.push(_sortKeys(obj[i]));
+ }
+ }
+ else if(obj.constructor === Object)
+ {
+ rval = {};
+ var keys = Object.keys(obj).sort();
+ for(var k in keys)
+ {
+ var key = keys[k];
+ rval[key] = _sortKeys(obj[key]);
+ }
+ }
}
return rval;
};
@@ -401,9 +415,9 @@
// Note: A value is a subject if all of these hold true:
// 1. It is an Object.
- // 2. It is not a literal.
+ // 2. It is not a literal (@value).
// 3. It has more than 1 key OR any existing key is not '@id'.
- if(value !== null && value.constructor === Object && !('@literal' in value))
+ if(value !== null && value.constructor === Object && !('@value' in value))
{
var keyCount = Object.keys(value).length;
rval = (keyCount > 1 || !('@id' in value));
@@ -412,6 +426,8 @@
return rval;
};
+var _orderKeys =
+
/*
* JSON-LD API.
*/
@@ -461,62 +477,39 @@
// fully expand input
input = jsonld.expand(input);
- var tmp;
- if(input.constructor === Array)
- {
- rval = [];
- tmp = input;
- }
- else
- {
- tmp = [input];
- }
-
// merge context if it is an array
if(ctx.constructor === Array)
{
ctx = jsonld.mergeContexts({}, ctx);
}
- for(var i in tmp)
+ // setup output context
+ var ctxOut = {};
+
+ // compact and sort keys
+ var out = new Processor().compact(_clone(ctx), null, input, ctxOut);
+ rval = out = _sortKeys(out);
+
+ // add context if used
+ if(Object.keys(ctxOut).length > 0)
{
- // setup output context
- var ctxOut = {};
+ // sort context keys
+ ctxOut = _sortKeys(ctxOut);
- // compact
- var out = new Processor().compact(_clone(ctx), null, tmp[i], ctxOut);
-
- // add context if used
- if(Object.keys(ctxOut).length > 0)
+ // put @context first
+ rval = {'@context': ctxOut};
+ if(out.constructor === Array)
{
- // sort context keys
- ctxOut = _sortContextKeys(ctxOut);
-
- // sort keys
- var keys = Object.keys(out);
- keys.sort();
-
- // put @context first
- keys.unshift('@context');
- out['@context'] = ctxOut;
-
- // order keys in output
- var ordered = {};
- for(var k in keys)
- {
- var key = keys[k];
- ordered[key] = out[key];
- }
- out = ordered;
- }
-
- if(rval === null)
- {
- rval = out;
+ rval['@id'] = out;
}
else
{
- rval.push(out);
+ var keys = Object.keys(out);
+ for(var i in keys)
+ {
+ var key = keys[i];
+ rval[key] = out[key];
+ }
}
}
}
@@ -967,9 +960,9 @@
{
rval = value['@id'];
}
- else if('@literal' in value)
+ else if('@value' in value)
{
- rval = value['@literal'];
+ rval = value['@value'];
}
}
else
@@ -1114,7 +1107,8 @@
}
// special-case expand @id and @type (skips '@id' expansion)
- if(property === keywords['@id'] || property === keywords['@type'])
+ if(property === '@id' || property === keywords['@id'] ||
+ property === '@type' || property === keywords['@type'])
{
rval = _expandTerm(ctx, value, null);
}
@@ -1138,7 +1132,7 @@
value = value.toExponential(6).replace(
/(e(?:\+|-))([0-9])$/, '$10$2');
}
- rval['@literal'] = '' + value;
+ rval['@value'] = '' + value;
}
}
// nothing to coerce
@@ -1356,10 +1350,10 @@
}
else
{
- rval = _compareObjectKeys(o1, o2, '@literal');
+ rval = _compareObjectKeys(o1, o2, '@value');
if(rval === 0)
{
- if('@literal' in o1)
+ if('@value' in o1)
{
rval = _compareObjectKeys(o1, o2, '@type');
if(rval === 0)
@@ -1393,12 +1387,12 @@
/*
3. For each property, compare sorted object values.
3.1. The bnode with fewer objects is first.
- 3.2. For each object value, compare only literals and non-bnodes.
+ 3.2. For each object value, compare only literals (@value) and non-bnodes.
3.2.1. The bnode with fewer non-bnodes is first.
3.2.2. The bnode with a string object is first.
3.2.3. The bnode with the alphabetically-first string is first.
- 3.2.4. The bnode with a @literal is first.
- 3.2.5. The bnode with the alphabetically-first @literal is first.
+ 3.2.4. The bnode with a @value is first.
+ 3.2.5. The bnode with the alphabetically-first @value is first.
3.2.6. The bnode with the alphabetically-first @type is first.
3.2.7. The bnode with a @language is first.
3.2.8. The bnode with the alphabetically-first @language is first.
@@ -1562,7 +1556,7 @@
else if(value.constructor === Object)
{
// already-expanded value or special-case reference-only @type
- if('@literal' in value || parentProperty === '@type')
+ if('@value' in value || parentProperty === '@type')
{
flattened = _clone(value);
}
@@ -2037,9 +2031,9 @@
// literal
else
{
- rval += '"' + o['@literal'] + '"';
+ rval += '"' + o['@value'] + '"';
- // type literal
+ // typed literal
if('@type' in o)
{
rval += '^^<' + o['@type'] + '>';
@@ -3058,7 +3052,50 @@
// apply context
if(ctx !== null && rval !== null)
{
- rval = jsonld.compact(ctx, rval);
+ // preserve top-level array by compacting individual entries
+ if(rval.constructor === Array)
+ {
+ var arr = rval;
+ rval = [];
+ for(var i in arr)
+ {
+ rval.push(jsonld.compact(ctx, arr[i]));
+ }
+ }
+ else
+ {
+ rval = jsonld.compact(ctx, rval);
+ }
+/*
+ // preserve top-level array
+ if(isArray && rval.constructor !== Array)
+ {
+ ctx = rval['@context'];
+ var idKeyword = '@id';
+ for(var key in ctx)
+ {
+ if(ctx[key] === '@id')
+ {
+ idKeyword = key;
+ }
+ }
+ var arr = rval['@id'];
+ rval = [];
+ for(var i in arr)
+ {
+ // make '@context' first key
+ var value = {
+ '@context': ctx,
+ };
+ var keys = Object.keys(arr[i]);
+ for(var k in keys)
+ {
+ var key = keys[k];
+ value[key] = arr[i][key];
+ }
+ rval.push(value);
+ }
+ }*/
}
return rval;
--- a/test-suite/tests/compact-0010-out.jsonld Wed Jan 11 16:26:56 2012 -0800
+++ b/test-suite/tests/compact-0010-out.jsonld Wed Jan 11 21:51:27 2012 -0500
@@ -1,8 +1,5 @@
{
"@context": {
- "http://example.org/test#property1": {
- "@type": "uri"
- },
"http://example.org/test#property2": {
"@type": "uri"
},
@@ -18,4 +15,4 @@
"http://example.org/test#property2": "http://example.org/test#example3",
"http://example.org/test#property3": "http://example.org/test#example4",
"uri": "http://example.org/test#example1"
-}
\ No newline at end of file
+}
--- a/test-suite/tests/expand-0005-out.jsonld Wed Jan 11 16:26:56 2012 -0800
+++ b/test-suite/tests/expand-0005-out.jsonld Wed Jan 11 21:51:27 2012 -0500
@@ -5,15 +5,15 @@
"@type": "http://www.w3.org/2001/XMLSchema#boolean"
},
"http://example.org/vocab#double": {
- "@value": "1.23",
+ "@value": "1.230000e+00",
"@type": "http://www.w3.org/2001/XMLSchema#double"
},
"http://example.org/vocab#double-zero": {
- "@value": "0.0",
+ "@value": "0.000000e+00",
"@type": "http://www.w3.org/2001/XMLSchema#double"
},
"http://example.org/vocab#int": {
"@value": "123",
"@type": "http://www.w3.org/2001/XMLSchema#integer"
}
-}
\ No newline at end of file
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test-suite/tests/expand-0010-in.jsonld Wed Jan 11 21:51:27 2012 -0500
@@ -0,0 +1,23 @@
+{
+ "@context": {
+ "name": "http://xmlns.com/foaf/0.1/name",
+ "homepage": {
+ "@id": "http://xmlns.com/foaf/0.1/homepage",
+ "@type": "@id"
+ },
+ "know": "http://xmlns.com/foaf/0.1/knows",
+ "@iri": "@id"
+ },
+ "@id": "#me",
+ "know": [
+ {
+ "@id": "http://example.com/bob#me",
+ "name": "Bob",
+ "homepage": "http://example.com/bob"
+ }, {
+ "@id": "http://example.com/alice#me",
+ "name": "Alice",
+ "homepage": "http://example.com/alice"
+ }
+ ]
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test-suite/tests/expand-0010-out.jsonld Wed Jan 11 21:51:27 2012 -0500
@@ -0,0 +1,18 @@
+{
+ "@id": "#me",
+ "http://xmlns.com/foaf/0.1/knows": [
+ {
+ "@id": "http://example.com/bob#me",
+ "http://xmlns.com/foaf/0.1/name": "Bob",
+ "http://xmlns.com/foaf/0.1/homepage": {
+ "@id": "http://example.com/bob"
+ }
+ }, {
+ "@id": "http://example.com/alice#me",
+ "http://xmlns.com/foaf/0.1/name": "Alice",
+ "http://xmlns.com/foaf/0.1/homepage": {
+ "@id": "http://example.com/alice"
+ }
+ }
+ ]
+}
--- a/test-suite/tests/expand-manifest.jsonld Wed Jan 11 16:26:56 2012 -0800
+++ b/test-suite/tests/expand-manifest.jsonld Wed Jan 11 21:51:27 2012 -0500
@@ -49,5 +49,10 @@
"name": "terms",
"input": "expand-0009-in.jsonld",
"expect": "expand-0009-out.jsonld"
+ }, {
+ "@type": ["test:TestCase", "jld:ExpandTest"],
+ "name": "do not expand aliased @id/@type",
+ "input": "expand-0010-in.jsonld",
+ "expect": "expand-0010-out.jsonld"
}]
}