Browse Source

Fix JSON indentation (#1158)

* Fix JSON indentation

* fix indentation bug in JsonSerializer
* add regression test

* Fix JSON indentation

* remove test output
Jonathan Resnick 3 năm trước cách đây
mục cha
commit
28e2996866
2 tập tin đã thay đổi với 16 bổ sung1 xóa
  1. 15 1
      Jint.Tests/Runtime/JsonTests.cs
  2. 1 0
      Jint/Native/Json/JsonSerializer.cs

+ 15 - 1
Jint.Tests/Runtime/JsonTests.cs

@@ -48,5 +48,19 @@ namespace Jint.Tests.Runtime
             Assert.NotNull(error);
             Assert.Equal("SyntaxError", error.Get("name"));
         }
+
+        [Theory]
+        [InlineData("[[]]", "[\n  []\n]")]
+        [InlineData("[ { a: [{ x: 0 }], b:[]} ]",
+            "[\n  {\n    \"a\": [\n      {\n        \"x\": 0\n      }\n    ],\n    \"b\": []\n  }\n]")]
+        public void ShouldSerializeWithCorrectIndentation(string script, string expectedJson)
+        {
+            var engine = new Engine();
+            engine.SetValue("x", engine.Evaluate(script));
+
+            var result = engine.Evaluate("JSON.stringify(x, null, 2);").AsString();
+
+            Assert.Equal(expectedJson, result);
+        }
     }
-}
+}

+ 1 - 0
Jint/Native/Json/JsonSerializer.cs

@@ -295,6 +295,7 @@ namespace Jint.Native.Json
             if (partial.Count == 0)
             {
                 _stack.Exit();
+                _indent = stepback;
                 return "[]";
             }