Browse Source

Fix JSON.stringify for empty array

JSON.stringify(new Array(1)) === '[null]'.
Change Str() to match the exactly the spec specially NOTE 5
Frederic Torres 11 years ago
parent
commit
a0e142207d
1 changed files with 16 additions and 15 deletions
  1. 16 15
      Jint/Native/Json/JsonSerializer.cs

+ 16 - 15
Jint/Native/Json/JsonSerializer.cs

@@ -165,10 +165,11 @@ namespace Jint.Native.Json
                 }
             }
             
-            if (value == Undefined.Instance) 
-            {
-                return Undefined.Instance;
-            }
+            //if (value == Undefined.Instance) 
+            //{
+            //    //return Undefined.Instance;
+            //    return "null";
+            //}
 
             if (value == Null.Instance)
             {
@@ -200,21 +201,19 @@ namespace Jint.Native.Json
                 return "null";
             }
 
-            if (value.IsObject())
+            var isCallable = value.IsObject() && value.AsObject() is ICallable;
+
+            if (value.IsObject() && isCallable == false)
             {
-                var valueCallable = value.AsObject() as ICallable;
-                if (valueCallable != null)
+                if (value.AsObject().Class == "Array")
                 {
-                    if (value.AsObject().Class == "Array")
-                    {
-                        return SerializeArray(value.As<ArrayInstance>());
-                    }
-
-                    return SerializeObject(value.AsObject());
+                    return SerializeArray(value.As<ArrayInstance>());
                 }
+
+                return SerializeObject(value.AsObject());
             }
 
-            return "null";
+            return JsValue.Undefined;
         }
 
         private string Quote(string value)
@@ -273,6 +272,8 @@ namespace Jint.Native.Json
             for (int i = 0; i < len; i++)
             {
                 var strP = Str(TypeConverter.ToString(i), value);
+                if (strP == JsValue.Undefined)
+                    strP = "null";
                 partial.Add(strP.AsString());
             }
             if (partial.Count == 0)
@@ -329,7 +330,7 @@ namespace Jint.Native.Json
             foreach (var p in k)
             {
                 var strP = Str(p, value);
-                if (strP != "null")
+                if (strP != JsValue.Undefined)
                 {
                     var member = Quote(p) + ":";
                     if (_gap != "")