Updated to latest jsonld.js. Closes #39.
authorDave Longley <dlongley@digitalbazaar.com>
Tue, 01 Nov 2011 14:31:09 -0400
changeset 251 4024a2d44d9d
parent 250 10b84f283486
child 254 730bd58bef2d
Updated to latest jsonld.js. Closes #39.
playground/jsonld.js
--- a/playground/jsonld.js	Mon Oct 31 23:29:58 2011 +0800
+++ b/playground/jsonld.js	Tue Nov 01 14:31:09 2011 -0400
@@ -318,6 +318,41 @@
    return rval;
 };
 
+/**
+ * Sorts the keys in a context.
+ * 
+ * @param ctx the context to sort.
+ * 
+ * @return the sorted context.
+ */
+var _sortContextKeys = function(ctx)
+{
+   var rval = {};
+   
+   // sort keys
+   var keys = Object.keys(ctx).sort();
+   for(var k in keys)
+   {
+      var key = keys[k];
+      if(key !== '@coerce')
+      {
+         rval[key] = ctx[key];
+      }
+   }
+   if('@coerce' in ctx)
+   {
+      rval['@coerce'] = {};
+      keys = Object.keys(ctx['@coerce']).sort();
+      for(var k in keys)
+      {
+         var key = keys[k];
+         rval['@coerce'][key] = ctx['@coerce'][key];
+      }
+   }
+   
+   return rval;
+};
+
 /*
  * JSON-LD API.
  */
@@ -395,7 +430,25 @@
          // add context if used
          if(Object.keys(ctxOut).length > 0)
          {
+            // 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)