From d571c204768d6ec24ae16785e2e24af662aa1d2b Mon Sep 17 00:00:00 2001 From: Jimmy Date: Mon, 5 Aug 2024 12:57:07 +0800 Subject: [PATCH 1/2] null operation --- tests/Neo.Json.UnitTests/UT_JArray.cs | 68 +++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/tests/Neo.Json.UnitTests/UT_JArray.cs b/tests/Neo.Json.UnitTests/UT_JArray.cs index 1538dfe558..474ce7587f 100644 --- a/tests/Neo.Json.UnitTests/UT_JArray.cs +++ b/tests/Neo.Json.UnitTests/UT_JArray.cs @@ -325,5 +325,73 @@ public void TestReadOnlyBehavior() var jArray = new JArray(); jArray.IsReadOnly.Should().BeFalse(); } + + [TestMethod] + public void TestAddNull() + { + var jArray = new JArray(); + jArray.Add(null); + + jArray.Count.Should().Be(1); + jArray[0].Should().BeNull(); + } + + [TestMethod] + public void TestSetNull() + { + var jArray = new JArray { alice }; + jArray[0] = null; + + jArray.Count.Should().Be(1); + jArray[0].Should().BeNull(); + } + + [TestMethod] + public void TestInsertNull() + { + var jArray = new JArray { alice }; + jArray.Insert(0, null); + + jArray.Count.Should().Be(2); + jArray[0].Should().BeNull(); + jArray[1].Should().Be(alice); + } + + [TestMethod] + public void TestRemoveNull() + { + var jArray = new JArray { null, alice }; + jArray.Remove(null); + + jArray.Count.Should().Be(1); + jArray[0].Should().Be(alice); + } + + [TestMethod] + public void TestContainsNull() + { + var jArray = new JArray { null, alice }; + jArray.Contains(null).Should().BeTrue(); + jArray.Contains(bob).Should().BeFalse(); + } + + [TestMethod] + public void TestIndexOfNull() + { + var jArray = new JArray { null, alice }; + jArray.IndexOf(null).Should().Be(0); + jArray.IndexOf(alice).Should().Be(1); + } + + [TestMethod] + public void TestCopyToWithNull() + { + var jArray = new JArray { null, alice }; + JObject[] jObjects = new JObject[2]; + jArray.CopyTo(jObjects, 0); + + jObjects[0].Should().BeNull(); + jObjects[1].Should().Be(alice); + } } } From 58f597162c8e633967dc0698aec3a81af588df46 Mon Sep 17 00:00:00 2001 From: Jimmy Date: Mon, 5 Aug 2024 15:37:03 +0800 Subject: [PATCH 2/2] fix array --- src/Neo.Json/JArray.cs | 2 +- tests/Neo.Json.UnitTests/UT_JArray.cs | 30 ++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/Neo.Json/JArray.cs b/src/Neo.Json/JArray.cs index 903f29941b..5ac193e1eb 100644 --- a/src/Neo.Json/JArray.cs +++ b/src/Neo.Json/JArray.cs @@ -67,7 +67,7 @@ public void Add(JToken? item) public override string AsString() { - return string.Join(",", items.Select(p => p?.AsString())); + return ToString(); } public override void Clear() diff --git a/tests/Neo.Json.UnitTests/UT_JArray.cs b/tests/Neo.Json.UnitTests/UT_JArray.cs index 474ce7587f..768f9a8f89 100644 --- a/tests/Neo.Json.UnitTests/UT_JArray.cs +++ b/tests/Neo.Json.UnitTests/UT_JArray.cs @@ -252,7 +252,7 @@ public void TestAsString() bob, }; var s = jArray.AsString(); - Assert.AreEqual(s, "{\"name\":\"alice\",\"age\":30,\"score\":100.001,\"gender\":\"female\",\"isMarried\":true,\"pet\":{\"name\":\"Tom\",\"type\":\"cat\"}},{\"name\":\"bob\",\"age\":100000,\"score\":0.001,\"gender\":\"male\",\"isMarried\":false,\"pet\":{\"name\":\"Paul\",\"type\":\"dog\"}}"); + Assert.AreEqual(s, "[{\"name\":\"alice\",\"age\":30,\"score\":100.001,\"gender\":\"female\",\"isMarried\":true,\"pet\":{\"name\":\"Tom\",\"type\":\"cat\"}},{\"name\":\"bob\",\"age\":100000,\"score\":0.001,\"gender\":\"male\",\"isMarried\":false,\"pet\":{\"name\":\"Paul\",\"type\":\"dog\"}}]"); } [TestMethod] @@ -329,8 +329,7 @@ public void TestReadOnlyBehavior() [TestMethod] public void TestAddNull() { - var jArray = new JArray(); - jArray.Add(null); + var jArray = new JArray { null }; jArray.Count.Should().Be(1); jArray[0].Should().BeNull(); @@ -393,5 +392,30 @@ public void TestCopyToWithNull() jObjects[0].Should().BeNull(); jObjects[1].Should().Be(alice); } + + [TestMethod] + public void TestToStringWithNull() + { + var jArray = new JArray { null, alice, bob }; + var jsonString = jArray.ToString(); + var asString = jArray.AsString(); + // JSON string should properly represent the null value + jsonString.Should().Be("[null,{\"name\":\"alice\",\"age\":30,\"score\":100.001,\"gender\":\"female\",\"isMarried\":true,\"pet\":{\"name\":\"Tom\",\"type\":\"cat\"}},{\"name\":\"bob\",\"age\":100000,\"score\":0.001,\"gender\":\"male\",\"isMarried\":false,\"pet\":{\"name\":\"Paul\",\"type\":\"dog\"}}]"); + asString.Should().Be("[null,{\"name\":\"alice\",\"age\":30,\"score\":100.001,\"gender\":\"female\",\"isMarried\":true,\"pet\":{\"name\":\"Tom\",\"type\":\"cat\"}},{\"name\":\"bob\",\"age\":100000,\"score\":0.001,\"gender\":\"male\",\"isMarried\":false,\"pet\":{\"name\":\"Paul\",\"type\":\"dog\"}}]"); + } + + [TestMethod] + public void TestFromStringWithNull() + { + var jsonString = "[null,{\"name\":\"alice\",\"age\":30,\"score\":100.001,\"gender\":\"female\",\"isMarried\":true,\"pet\":{\"name\":\"Tom\",\"type\":\"cat\"}},{\"name\":\"bob\",\"age\":100000,\"score\":0.001,\"gender\":\"male\",\"isMarried\":false,\"pet\":{\"name\":\"Paul\",\"type\":\"dog\"}}]"; + var jArray = (JArray)JArray.Parse(jsonString); + + jArray.Count.Should().Be(3); + jArray[0].Should().BeNull(); + + // Checking the second and third elements + jArray[1]["name"].AsString().Should().Be("alice"); + jArray[2]["name"].AsString().Should().Be("bob"); + } } }