-
Notifications
You must be signed in to change notification settings - Fork 1
/
JsonExtensions.cs
133 lines (113 loc) · 4.31 KB
/
JsonExtensions.cs
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using DictionaryObject = System.Collections.Generic.Dictionary<string, object>;
// ReSharper disable StringIndexOfIsCultureSpecific.1
namespace WavesCS.Core
{
public static class JsonExtensions
{
private static readonly JsonSerializer Serializer = new JsonSerializer();
public static string ToJson(this DictionaryObject data)
{
var builder = new StringBuilder();
var writer = new StringWriter(builder);
Serializer.Serialize(writer, data);
return builder.ToString();
}
public static string ToJson(this DictionaryObject[] data)
{
var builder = new StringBuilder();
var writer = new StringWriter(builder);
Serializer.Serialize(writer, data);
return builder.ToString();
}
public static DictionaryObject ParseJsonObject(this string json)
{
return JsonConvert.DeserializeObject<DictionaryObject>(json);
}
public static DictionaryObject[] ParseJsonObjects(this string json)
{
return JsonConvert.DeserializeObject<DictionaryObject[]>(json);
}
public static DictionaryObject[] ParseFlatObjects(this string json)
{
return JsonConvert.DeserializeObject<JArray[]>(json).Single().ToObject<DictionaryObject[]>();
}
public static string ParseJsonString(this string json)
{
return JsonConvert.DeserializeObject<string>(json);
}
public static DictionaryObject GetObject(this DictionaryObject d, string field)
{
return d.Get<DictionaryObject>(field);
}
public static object GetValue(this DictionaryObject d, string field)
{
try
{
if (field.Contains("."))
return d.GetObject(field.Substring(0, field.IndexOf("."))).GetValue(field.Substring(field.IndexOf(".") + 1));
else
return d[field];
} catch(Exception)
{
throw new Exception(String.Format("Cannot get value of \"{0}\" field", field));
}
}
public static T Get<T>(this DictionaryObject d, string field)
{
var value = d.GetValue(field);
if (value is JContainer j)
return j.ToObject<T>();
else
return (T) value;
}
public static IEnumerable<DictionaryObject> GetObjects(this DictionaryObject d, string field)
{
return d.Get<DictionaryObject[]>(field);
}
public static string GetString(this DictionaryObject d, string field)
{
return d.Get<string>(field);
}
public static DateTime GetDate(this DictionaryObject d, string field)
{
var timestamp = d.GetLong(field);
return new DateTime(1970, 1, 1).AddMilliseconds(timestamp);
}
public static DateTime ToDate(this long t)
{
return new DateTime(1970, 1, 1).AddMilliseconds(t);
}
public static long GetLong(this DictionaryObject d, string field)
{
return long.Parse(d.GetValue(field).ToString());
}
public static decimal GetDecimal(this DictionaryObject d, string field, Asset asset)
{
return asset.LongToAmount(long.Parse(d.GetValue(field).ToString()));
}
public static int GetInt(this DictionaryObject d, string field)
{
return int.Parse(d.GetValue(field).ToString());
}
public static byte GetByte(this DictionaryObject d, string field)
{
return byte.Parse(d.GetValue(field).ToString());
}
public static char GetChar(this DictionaryObject d, string field)
{
Char.TryParse(d.GetValue(field).ToString(), out char result);
return result;
}
public static bool GetBool(this DictionaryObject d, string field)
{
return (bool) d.GetValue(field);
}
}
}