Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit abbf930

Browse files
committed
Fix empty collections
1 parent 1f9539b commit abbf930

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

src/ServiceStack.Text/Common/DeserializeListWithElements.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ public static List<string> ParseStringList(ReadOnlySpan<char> value)
110110

111111
public static List<int> ParseIntList(ReadOnlySpan<char> value)
112112
{
113-
if ((value = StripList(value)).IsEmpty) return null;
114-
if (value.Length == 0) return new List<int>();
113+
if ((value = StripList(value)).IsNullOrEmpty())
114+
return value.IsEmpty ? null : new List<int>();
115115

116116
var to = new List<int>();
117117
var valueLength = value.Length;
@@ -131,8 +131,8 @@ public static List<int> ParseIntList(ReadOnlySpan<char> value)
131131

132132
public static List<byte> ParseByteList(ReadOnlySpan<char> value)
133133
{
134-
if ((value = StripList(value)).IsEmpty) return null;
135-
if (value.Length == 0) return new List<byte>();
134+
if ((value = StripList(value)).IsNullOrEmpty())
135+
return value.IsEmpty ? null : new List<byte>();
136136

137137
var to = new List<byte>();
138138
var valueLength = value.Length;
@@ -161,7 +161,8 @@ public static ICollection<T> ParseGenericList(string value, Type createListType,
161161

162162
public static ICollection<T> ParseGenericList(ReadOnlySpan<char> value, Type createListType, ParseStringSpanDelegate parseFn)
163163
{
164-
if ((value = DeserializeListWithElements<TSerializer>.StripList(value)).IsEmpty) return null;
164+
if ((value = DeserializeListWithElements<TSerializer>.StripList(value)).IsEmpty)
165+
return null;
165166

166167
var isReadOnly = createListType != null
167168
&& (createListType.IsGenericType && createListType.GetGenericTypeDefinition() == typeof(ReadOnlyCollection<>));

src/ServiceStack.Text/PclExport.Net40.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -683,10 +683,10 @@ public static Hashtable ParseHashtable(ReadOnlySpan<char> value)
683683

684684
public static StringCollection ParseStringCollection<TS>(ReadOnlySpan<char> value) where TS : ITypeSerializer
685685
{
686-
if ((value = DeserializeListWithElements<TS>.StripList(value)) == null) return null;
687-
return value.Length == 0
688-
? new StringCollection()
689-
: ToStringCollection(DeserializeListWithElements<TSerializer>.ParseStringList(value));
686+
if ((value = DeserializeListWithElements<TS>.StripList(value)).IsNullOrEmpty())
687+
return value.IsEmpty ? null : new StringCollection();
688+
689+
return ToStringCollection(DeserializeListWithElements<TSerializer>.ParseStringList(value));
690690
}
691691

692692
public static StringCollection ToStringCollection(List<string> items)

src/ServiceStack.Text/PclExport.NetStandard.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,8 @@ public override ParseStringSpanDelegate GetSpecializedCollectionParseStringSpanM
335335

336336
private static StringCollection ParseStringCollection<TSerializer>(ReadOnlySpan<char> value) where TSerializer : ITypeSerializer
337337
{
338-
if ((value = DeserializeListWithElements<TSerializer>.StripList(value)).IsEmpty) return null;
338+
if ((value = DeserializeListWithElements<TSerializer>.StripList(value)).IsNullOrEmpty())
339+
return value.IsEmpty ? null : new StringCollection();
339340

340341
var result = new StringCollection();
341342

tests/ServiceStack.Text.Tests/BasicStringSerializerTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,16 +628,19 @@ public class EmptyCollections
628628
{
629629
public string[] Strings { get; set; }
630630
public int[] Ints { get; set; }
631+
public List<int> IntList { get; set; }
631632
}
632633

633634
[Test]
634635
public void Can_deserialize_empty_array()
635636
{
636637
Assert.That("[]".FromJson<string[]>(), Is.EquivalentTo(new string[0]));
637638
Assert.That("[]".FromJson<int[]>(), Is.EquivalentTo(new int[0]));
639+
Assert.That("[]".FromJson<List<int>>(), Is.EquivalentTo(new List<int>()));
638640

639641
Assert.That("{\"Strings\":[]}".FromJson<EmptyCollections>().Strings, Is.EquivalentTo(new string[0]));
640642
Assert.That("{\"Ints\":[]}".FromJson<EmptyCollections>().Ints, Is.EquivalentTo(new int[0]));
643+
Assert.That("{\"IntList\":[]}".FromJson<EmptyCollections>().IntList, Is.EquivalentTo(new List<int>()));
641644
}
642645
}
643646
}

0 commit comments

Comments
 (0)