Skip to content

Commit 04c6977

Browse files
authored
Merge pull request #115 from kdw9502/develop
Fix empty array ToJson issues
2 parents 6ddf16e + fb2f104 commit 04c6977

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

src/LitJson/JsonMapper.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,8 @@ private static object ReadValue (Type inst_type, JsonReader reader)
405405
list = new ArrayList ();
406406
elem_type = inst_type.GetElementType ();
407407
}
408+
409+
list.Clear();
408410

409411
while (true) {
410412
object item = ReadValue (elem_type, reader);

test/JsonMapperTest.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,17 @@ public class NullableEnumTest
202202
public NullableEnum? TestEnum;
203203
}
204204

205+
class EmptyArrayInJsonDataTest
206+
{
207+
public int[] array;
208+
public string name;
209+
}
210+
211+
class EmptyArrayInJsonDataTestWrapper
212+
{
213+
public JsonData data;
214+
}
215+
205216
[TestFixture]
206217
public class JsonMapperTest
207218
{
@@ -1059,5 +1070,23 @@ public void NullableEnumExportTest()
10591070
expectedJson = "{\"TestEnum\":null}";
10601071
Assert.AreEqual(expectedJson, JsonMapper.ToJson(value));
10611072
}
1073+
1074+
[Test]
1075+
public void EmptyArrayInJsonDataExportTest()
1076+
{
1077+
string testJson = @"
1078+
{
1079+
""data"":
1080+
{
1081+
""array"": [],
1082+
""name"": ""testName""
1083+
}
1084+
}";
1085+
1086+
var response = JsonMapper.ToObject<EmptyArrayInJsonDataTestWrapper>(testJson);
1087+
var toJsonResult = response.data.ToJson();
1088+
string expectedJson = "{\"array\":[],\"name\":\"testName\"}";
1089+
Assert.AreEqual(toJsonResult, expectedJson);
1090+
}
10621091
}
10631092
}

0 commit comments

Comments
 (0)