-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonHelper.cs
145 lines (131 loc) · 4.62 KB
/
JsonHelper.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
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace BaiduSpeechDemo
{
/// <summary>
/// JSON操作助手类
/// </summary>
/// <remarks>
/// 2013-11-18 18:56 Created By iceStone
/// </remarks>
public static class JsonHelper
{
private static readonly JsonSerializer JsonSerializer = new JsonSerializer();
/// <summary>
/// 将一个对象序列化JSON字符串
/// </summary>
/// <remarks>
/// 2013-11-18 18:56 Created By iceStone
/// </remarks>
/// <param name="obj">待序列化的对象</param>
/// <returns>JSON字符串</returns>
public static string Serialize(object obj)
{
var sw = new StringWriter();
JsonSerializer.Serialize(new JsonTextWriter(sw), obj);
return sw.GetStringBuilder().ToString();
}
public static string SerializeIndented(object obj) => ConvertJsonStringIndented(Serialize(obj));
/// <summary>
/// 将JSON字符串反序列化为一个Object对象
/// </summary>
/// <remarks>
/// 2013-11-18 18:56 Created By iceStone
/// </remarks>
/// <param name="json">JSON字符串</param>
/// <returns>Object对象</returns>
public static object Deserialize(string json)
{
var sr = new StringReader(json);
return JsonSerializer.Deserialize(new JsonTextReader(sr));
}
/// <summary>
/// 将JSON字符串反序列化为一个指定类型对象
/// </summary>
/// <remarks>
/// 2013-11-18 18:56 Created By iceStone
/// </remarks>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="json">JSON字符串</param>
/// <returns>指定类型对象</returns>
public static T Deserialize<T>(string json) where T : class
{
var sr = new StringReader(json);
return JsonSerializer.Deserialize(new JsonTextReader(sr), typeof(T)) as T;
}
/// <summary>
/// 将JSON字符串反序列化为一个JObject对象
/// </summary>
/// <remarks>
/// 2013-11-18 18:56 Created By iceStone
/// </remarks>
/// <param name="json">JSON字符串</param>
/// <returns>JObject对象</returns>
public static JObject DeserializeObject(string json)
{
return JsonConvert.DeserializeObject(json) as JObject;
}
/// <summary>
/// 将JSON字符串反序列化为一个JArray数组
/// </summary>
/// <remarks>
/// 2013-11-18 18:56 Created By iceStone
/// </remarks>
/// <param name="json">JSON字符串</param>
/// <returns>JArray对象</returns>
public static JArray DeserializeArray(string json)
{
return JsonConvert.DeserializeObject(json) as JArray;
}
private static string ConvertJsonStringIndented(string str)
{
TextReader tr = new StringReader(str);
var jtr = new JsonTextReader(tr);
var obj = JsonSerializer.Deserialize(jtr);
if (obj == null) return str;
var textWriter = new StringWriter();
var jsonWriter = new JsonTextWriter(textWriter)
{
Formatting = Formatting.Indented,
Indentation = 4,
IndentChar = ' '
};
JsonSerializer.Serialize(jsonWriter, obj);
return textWriter.ToString();
}
}
static class JsonMethods
{
public static string Serialize(this object obj) => JsonHelper.Serialize(obj);
public static string SerializeIndented(this object obj) => JsonHelper.SerializeIndented(obj);
public static bool TrySerialize(this object obj, out string str)
{
try
{
str = JsonHelper.Serialize(obj);
return true;
}
catch (Exception e)
{
str = string.Empty;
return false;
}
}
public static T Deserialize<T>(this string json) where T : class => JsonHelper.Deserialize<T>(json);
public static bool TryDeserialize<T>(this string json, out T value) where T : class
{
try
{
value = JsonHelper.Deserialize<T>(json);
return true;
}
catch (Exception e)
{
value = null;
return false;
}
}
}
}