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

Commit a74bb50

Browse files
committed
Add JsonObject.GetArray<T>
1 parent bd0c529 commit a74bb50

File tree

2 files changed

+81
-4
lines changed

2 files changed

+81
-4
lines changed

src/ServiceStack.Text/JsonObject.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ public static T Get<T>(this Dictionary<string, string> map, string key)
2323
return map.TryGetValue(key, out strVal) ? JsonSerializer.DeserializeFromString<T>(strVal) : default(T);
2424
}
2525

26+
public static T[] GetArray<T>(this Dictionary<string, string> map, string key)
27+
{
28+
var obj = map as JsonObject;
29+
string value;
30+
return map.TryGetValue(key, out value)
31+
? (obj != null ? value.FromJson<T[]>() : value.FromJsv<T[]>())
32+
: TypeConstants<T>.EmptyArray;
33+
}
34+
2635
/// <summary>
2736
/// Get JSON string value
2837
/// </summary>

tests/ServiceStack.Text.Tests/JsonObjectTests.cs

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void Does_escape_string_access()
4444
Assert.That(jsonObject.Get("a"), Is.EqualTo(test));
4545
Assert.That(jsonObject.Get<string>("a"), Is.EqualTo(test));
4646

47-
Assert.That(jsonObject.GetUnescaped("a"), Is.EqualTo(test.Replace("\"","\\\"")));
47+
Assert.That(jsonObject.GetUnescaped("a"), Is.EqualTo(test.Replace("\"", "\\\"")));
4848
}
4949

5050
[Test]
@@ -174,7 +174,7 @@ public class TypeObject
174174
public bool Prop3 { get; set; }
175175
public double Prop4 { get; set; }
176176
public string[] Prop5 { get; set; }
177-
public Dictionary<string,string> Prop6 { get; set; }
177+
public Dictionary<string, string> Prop6 { get; set; }
178178
}
179179

180180
[Test]
@@ -195,8 +195,8 @@ public void Can_parse_dynamic_json()
195195
Assert.That(typeObj.Prop2, Is.EqualTo(33));
196196
Assert.That(typeObj.Prop3, Is.EqualTo(true));
197197
Assert.That(typeObj.Prop4, Is.EqualTo(6.3d));
198-
Assert.That(typeObj.Prop5, Is.EquivalentTo(new[] {"A","B","C"}));
199-
Assert.That(typeObj.Prop6, Is.EquivalentTo(new Dictionary<string,string> { { "A", "a" } }));
198+
Assert.That(typeObj.Prop5, Is.EquivalentTo(new[] { "A", "B", "C" }));
199+
Assert.That(typeObj.Prop6, Is.EquivalentTo(new Dictionary<string, string> { { "A", "a" } }));
200200

201201
var obj = JsonObject.Parse(json);
202202

@@ -217,5 +217,73 @@ public void Can_parse_dynamic_json()
217217
Assert.That(o.Prop5, Is.EquivalentTo(new[] { "A", "B", "C" }));
218218
Assert.That(o.Prop6, Is.EquivalentTo(new Dictionary<string, string> { { "A", "a" } }));
219219
}
220+
221+
[Test]
222+
public void Can_deserialize_array_string_in_Map()
223+
{
224+
var json = "{\"name\":\"foo\",\"roles\":[\"Role1\",\"Role 2\"]}";
225+
var obj = JsonObject.Parse(json);
226+
Assert.That(obj.GetArray<string>("roles"), Is.EqualTo(new[] { "Role1", "Role 2" }));
227+
228+
var map = json.FromJson<Dictionary<string, string>>();
229+
Assert.That(map.GetArray<string>("roles"), Is.EqualTo(new[] { "Role1", "Role 2" }));
230+
}
231+
232+
[Test]
233+
public void Can_deserialize_array_numbers_in_Map()
234+
{
235+
var json = "{\"name\":\"foo\",\"roles\":[1,2]}";
236+
var obj = JsonObject.Parse(json);
237+
Assert.That(obj.GetArray<int>("roles"), Is.EqualTo(new[] { 1, 2 }));
238+
239+
var map = json.FromJson<Dictionary<string, string>>();
240+
Assert.That(map.GetArray<int>("roles"), Is.EqualTo(new[] { 1, 2 }));
241+
}
242+
243+
public class TestJArray
244+
{
245+
public int Id { get; set; }
246+
public string Name { get; set; }
247+
248+
protected bool Equals(TestJArray other)
249+
{
250+
return Id == other.Id && string.Equals(Name, other.Name);
251+
}
252+
253+
public override bool Equals(object obj)
254+
{
255+
if (ReferenceEquals(null, obj)) return false;
256+
if (ReferenceEquals(this, obj)) return true;
257+
if (obj.GetType() != this.GetType()) return false;
258+
return Equals((TestJArray) obj);
259+
}
260+
261+
public override int GetHashCode()
262+
{
263+
unchecked
264+
{
265+
return (Id*397) ^ (Name != null ? Name.GetHashCode() : 0);
266+
}
267+
}
268+
}
269+
270+
[Test]
271+
public void Can_deserialize_array_objects_in_Map()
272+
{
273+
var json = "{\"name\":\"foo\",\"roles\":[{\"Id\":1,\"Name\":\"Role1\"},{\"Id\":2,\"Name\":\"Role 2\"}]}";
274+
var obj = JsonObject.Parse(json);
275+
Assert.That(obj.GetArray<TestJArray>("roles"), Is.EqualTo(new[]
276+
{
277+
new TestJArray { Id = 1, Name = "Role1" },
278+
new TestJArray { Id = 2, Name = "Role 2" },
279+
}));
280+
281+
var map = json.FromJson<Dictionary<string, string>>();
282+
Assert.That(map.GetArray<TestJArray>("roles"), Is.EqualTo(new[]
283+
{
284+
new TestJArray { Id = 1, Name = "Role1" },
285+
new TestJArray { Id = 2, Name = "Role 2" },
286+
}));
287+
}
220288
}
221289
}

0 commit comments

Comments
 (0)