Updated to latest jsonld.js from Forge.
authorDave Longley <dlongley@digitalbazaar.com>
Fri, 29 Jul 2011 16:21:33 -0400
changeset 80 331e0da528ea
parent 79 390a52145e4f
child 81 b6aa96d3ed63
Updated to latest jsonld.js from Forge.
playground/jsonld.js
--- a/playground/jsonld.js	Fri Jul 29 14:42:22 2011 -0400
+++ b/playground/jsonld.js	Fri Jul 29 16:21:33 2011 -0400
@@ -412,7 +412,9 @@
    
    if(value === null)
    {
+      // return null, but check coerce type to add to usedCtx
       rval = null;
+      _getCoerceType(ctx, property, usedCtx);
    }
    else if(value.constructor === Array)
    {
@@ -442,10 +444,13 @@
       {
          if(value[key] !== '@context')
          {
-            // set object to compacted property
-            _setProperty(
-               rval, _compactIri(ctx, key, usedCtx),
-               _compact(ctx, key, value[key], usedCtx));
+            // set object to compacted property, only overwrite existing
+            // properties if the property actually compacted
+            var p = _compactIri(ctx, key, usedCtx);
+            if(p !== key || !(p in rval))
+            {
+               rval[p] = _compact(ctx, key, value[key], usedCtx);
+            }
          }
       }
    }
@@ -1001,7 +1006,11 @@
 {
    var flattened = null;
    
-   if(value.constructor === Array)
+   if(value === null)
+   {
+      // drop null values
+   }
+   else if(value.constructor === Array)
    {
       // list of objects or a disjoint graph
       for(var i in value)
@@ -2288,12 +2297,12 @@
                   }
                   else
                   {
-                     // add null property to value
-                     value[key] = null;
+                     // add empty array/null property to value
+                     value[key] = (f.constructor === Array) ? [] : null;
                   }
                   
-                  // handle setting default value(s)
-                  if(key in value)
+                  // handle setting default value
+                  if(value[key] === null)
                   {
                      // use first subframe if frame is an array
                      if(f.constructor === Array)
@@ -2304,37 +2313,13 @@
                      // determine if omit default is on
                      var omitOn = ('@omitDefault' in f) ?
                         f['@omitDefault'] : options.defaults.omitDefaultOn;
-                     
-                     if(value[key] === null)
-                     {
-                        if(omitOn)
-                        {
-                           delete value[key];
-                        }
-                        else if('@default' in f)
-                        {
-                           value[key] = f['@default'];
-                        }
-                     }
-                     else if(value[key].constructor === Array)
+                     if(omitOn)
                      {
-                        var tmp = [];
-                        for(var i in value[key])
-                        {
-                           if(value[key][i] === null)
-                           {
-                              // do not auto-include null in arrays
-                              if(!omitOn && '@default' in f)
-                              {
-                                 tmp.push(f['@default']);
-                              }
-                           }
-                           else
-                           {
-                              tmp.push(value[key][i]);
-                           }
-                        }
-                        value[key] = tmp;
+                        delete value[key];
+                     }
+                     else if('@default' in f)
+                     {
+                        value[key] = f['@default'];
                      }
                   }
                }