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

Commit 07d8ed0

Browse files
committed
Add JsonObject tests
1 parent fa49d78 commit 07d8ed0

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed

src/ServiceStack.Text/JsonObject.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,13 @@ public static T JsonTo<T>(this Dictionary<string, string> map, string key)
1919
/// </summary>
2020
public static T Get<T>(this Dictionary<string, string> map, string key, T defaultValue = default(T))
2121
{
22-
string strVal;
23-
return map.TryGetValue(key, out strVal) ? JsonSerializer.DeserializeFromString<T>(strVal) : defaultValue;
22+
return map.TryGetValue(key, out var strVal) ? JsonSerializer.DeserializeFromString<T>(strVal) : defaultValue;
2423
}
2524

2625
public static T[] GetArray<T>(this Dictionary<string, string> map, string key)
2726
{
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[]>())
27+
return map.TryGetValue(key, out var value)
28+
? (map is JsonObject obj ? value.FromJson<T[]>() : value.FromJsv<T[]>())
3229
: TypeConstants<T>.EmptyArray;
3330
}
3431

tests/ServiceStack.Text.Tests/JsonObjectTests.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,66 @@ public void Does_escape_strings_in_JsonObject_DTO()
371371
var dto = "{\"Prop\":{\"text\":\"line\nbreak\"}}".FromJson<JsonObjectWrapper>();
372372
Assert.That(dto.Prop["text"], Is.EqualTo("line\nbreak"));
373373

374+
Assert.That(dto.ToJson(), Is.EqualTo("{\"Prop\":{\"text\":\"line\\nbreak\"}}"));
375+
374376
dto = "{\"Prop\":{\"a\":{\"text\":\"line\nbreak\"}}}".FromJson<JsonObjectWrapper>();
375377
var a = dto.Prop.Object("a");
376378
Assert.That(a["text"], Is.EqualTo("line\nbreak"));
379+
380+
//
381+
//Assert.That(dto.ToJson(), Is.EqualTo("{\"Prop\":{\"a\":{\"text\":\"line\\nbreak\"}}}"));
382+
}
383+
384+
public class StringDictionaryWrapper
385+
{
386+
public Dictionary<string,string> Prop { get; set; }
387+
}
388+
389+
public class NestedStringDictionaryWrapper
390+
{
391+
public Dictionary<string,Dictionary<string,string>> Prop { get; set; }
377392
}
393+
394+
[Test]
395+
public void Does_serialize_StringDictionaryWrapper_line_breaks()
396+
{
397+
var prop = new Dictionary<string,string> {
398+
["text"] = "line\nbreak"
399+
};
400+
401+
Assert.That(prop.ToJson(), Is.EqualTo("{\"text\":\"line\\nbreak\"}"));
402+
403+
var dto = new StringDictionaryWrapper { Prop = prop };
404+
Assert.That(dto.ToJson(), Is.EqualTo("{\"Prop\":{\"text\":\"line\\nbreak\"}}"));
405+
406+
var nested = new NestedStringDictionaryWrapper { Prop = new Dictionary<string, Dictionary<string, string>> {
407+
["a"] = prop
408+
}
409+
};
410+
Assert.That(nested.ToJson(), Is.EqualTo("{\"Prop\":{\"a\":{\"text\":\"line\\nbreak\"}}}"));
411+
}
412+
413+
public class ObjectDictionaryWrapper
414+
{
415+
public object Prop { get; set; }
416+
}
417+
418+
[Test]
419+
public void Does_serialize_ObjectDictionaryWrapper_line_breaks()
420+
{
421+
var prop = new Dictionary<string,string> {
422+
["text"] = "line\nbreak"
423+
};
424+
425+
var dto = new ObjectDictionaryWrapper { Prop = prop };
426+
Assert.That(dto.ToJson(), Is.EqualTo("{\"Prop\":{\"text\":\"line\\nbreak\"}}"));
427+
428+
var nested = new ObjectDictionaryWrapper { Prop = new Dictionary<string, Dictionary<string, string>> {
429+
["a"] = prop
430+
}
431+
};
432+
Assert.That(nested.ToJson(), Is.EqualTo("{\"Prop\":{\"a\":{\"text\":\"line\\nbreak\"}}}"));
433+
}
434+
378435
}
379436
}

0 commit comments

Comments
 (0)