-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMyExtensions.FW46.linq
68 lines (47 loc) · 1.4 KB
/
MyExtensions.FW46.linq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<Query Kind="Program">
<NuGetReference>Newtonsoft.Json</NuGetReference>
<Namespace>Newtonsoft.Json</Namespace>
</Query>
void Main()
{
// Write code to test your extensions here. Press F5 to compile and run.
}
public static class MyExtensions
{
public static object DumpJson(this object value, string description = null)
{
return GetJsonDumpTarget(value).Dump(description);
}
public static object DumpJson(this object value, string description, int depth)
{
return GetJsonDumpTarget(value).Dump(description, depth);
}
public static object DumpJson(this object value, string description, bool toDataGrid)
{
return GetJsonDumpTarget(value).Dump(description, toDataGrid);
}
private static object GetJsonDumpTarget(object value)
{
object dumpTarget = value;
//if this is a string that contains a JSON object, do a round-trip serialization to format it:
var stringValue = value as string;
if (stringValue != null)
{
if (stringValue.Trim().StartsWith("{") || stringValue.Trim().StartsWith("["))
{
var obj = JsonConvert.DeserializeObject(stringValue);
dumpTarget = JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented);
}
else
{
dumpTarget = stringValue;
}
}
else
{
dumpTarget = JsonConvert.SerializeObject(value, Newtonsoft.Json.Formatting.Indented);
}
return dumpTarget;
}
}
// You can also define non-static classes, enums, etc.