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

Commit b56127f

Browse files
committed
Add support for parsing bytes as int array
1 parent 597433d commit b56127f

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

src/ServiceStack.Text/Common/DeserializeArray.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,14 @@ public static string[] ParseStringArray(string value)
180180

181181
public static byte[] ParseByteArray(string value)
182182
{
183+
var isArray = !string.IsNullOrEmpty(value) && value.Length > 1 && value[0] == '[';
183184
if ((value = DeserializeListWithElements<TSerializer>.StripList(value)) == null) return null;
184185
if ((value = Serializer.UnescapeString(value)) == null) return null;
185186
return value == string.Empty
186187
? TypeConstants.EmptyByteArray
187-
: Convert.FromBase64String(value);
188+
: !isArray
189+
? Convert.FromBase64String(value)
190+
: DeserializeListWithElements<TSerializer>.ParseByteList(value).ToArray();
188191
}
189192
}
190193
}

src/ServiceStack.Text/Common/DeserializeListWithElements.cs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,37 @@ public static List<int> ParseIntList(string value)
102102
if ((value = StripList(value)) == null) return null;
103103
if (value == string.Empty) return new List<int>();
104104

105-
var intParts = value.Split(JsWriter.ItemSeperator);
106-
var intValues = new List<int>(intParts.Length);
107-
foreach (var intPart in intParts)
105+
var to = new List<int>();
106+
var valueLength = value.Length;
107+
108+
var i = 0;
109+
while (i < valueLength)
108110
{
109-
intValues.Add(int.Parse(intPart));
111+
var elementValue = Serializer.EatValue(value, ref i);
112+
to.Add(int.Parse(elementValue));
113+
Serializer.EatItemSeperatorOrMapEndChar(value, ref i);
110114
}
111-
return intValues;
115+
116+
return to;
117+
}
118+
119+
public static List<byte> ParseByteList(string value)
120+
{
121+
if ((value = StripList(value)) == null) return null;
122+
if (value == string.Empty) return new List<byte>();
123+
124+
var to = new List<byte>();
125+
var valueLength = value.Length;
126+
127+
var i = 0;
128+
while (i < valueLength)
129+
{
130+
var elementValue = Serializer.EatValue(value, ref i);
131+
to.Add(byte.Parse(elementValue));
132+
Serializer.EatItemSeperatorOrMapEndChar(value, ref i);
133+
}
134+
135+
return to;
112136
}
113137
}
114138

tests/ServiceStack.Text.Tests/SpecialTypesTests.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ public void Can_Serialize_Type_with_ByteArray()
4848
Assert.That(json, Is.EquivalentTo("{\"Name\":\"Test\",\"Data\":\"AQIDBAU=\"}"));
4949
}
5050

51+
class PocoWithBytes
52+
{
53+
public string Name { get; set; }
54+
public byte[] Data { get; set; }
55+
}
56+
57+
[Test]
58+
public void Can_Serialize_Type_with_ByteArray_as_Int_Array()
59+
{
60+
var test = "{\"Name\":\"Test\",\"Data\":[1,2,3,4,5]}".FromJson<PocoWithBytes>();
61+
Assert.That(test.Data, Is.EquivalentTo(new byte[] { 1, 2, 3, 4, 5 }));
62+
}
63+
5164
[Test]
5265
public void Can_Serialize_ByteArray()
5366
{
@@ -93,11 +106,7 @@ public void Does_dump_delegate_info()
93106
Assert.That(methodWithArgs.Dump(), Is.EqualTo("String MethodWithArgs(Int32 arg1, String arg2)"));
94107

95108
Action x = () => { };
96-
Assert.That(x.Dump(), Is.EqualTo("Void <Does_dump_delegate_info>b__4()") //VS 2012
97-
.Or.EqualTo("Void <Does_dump_delegate_info>b__10_0()") //VS 2015
98-
.Or.EqualTo("Void <Does_dump_delegate_info>b__9_0()") //NET Core
99-
.Or.EqualTo("Void <Does_dump_delegate_info>m__1()") //Mono
100-
);
109+
Assert.That(x.Dump(), Does.StartWith("Void <Does_dump_delegate_info>"));
101110
}
102111

103112

0 commit comments

Comments
 (0)