bklLiudl
2024-07-23 277bbae216debe7e6c04e8cc6ee6e1ba9763e14b
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
 
namespace Json
{
    public class JsonObject
    {
        private Dictionary<string, JsonProperty> _property;
 
        public JsonObject()
        {
            this._property = (Dictionary<string, JsonProperty>)null;
        }
 
        public JsonObject(SetProperties callback)
        {
            if (callback == null)
                return;
            callback(this);
        }
 
        public JsonObject(string jsonString)
        {
            this.Parse(ref jsonString);
        }
 
        public JsonProperty AddProperty(string PropertyName)
        {
            if (this._property == null)
                this._property = new Dictionary<string, JsonProperty>((IEqualityComparer<string>)StringComparer.OrdinalIgnoreCase);
            if (!this._property.ContainsKey(PropertyName))
                this._property.Add(PropertyName, new JsonProperty());
            return this._property[PropertyName];
        }
 
        public JsonProperty AddProperty(string PropertyName, object Value)
        {
            if (this._property == null)
                this._property = new Dictionary<string, JsonProperty>((IEqualityComparer<string>)StringComparer.OrdinalIgnoreCase);
            if (!this._property.ContainsKey(PropertyName))
                this._property.Add(PropertyName, new JsonProperty(Value));
            return this._property[PropertyName];
        }
 
        public JsonProperty AddProperty(string PropertyName, params object[] Values)
        {
            if (this._property == null)
                this._property = new Dictionary<string, JsonProperty>((IEqualityComparer<string>)StringComparer.OrdinalIgnoreCase);
            if (!this._property.ContainsKey(PropertyName))
            {
                JsonProperty jsonProperty = new JsonProperty();
                foreach (object obj in Values)
                    jsonProperty.Add(obj);
                this._property.Add(PropertyName, jsonProperty);
            }
            return this._property[PropertyName];
        }
 
        public JsonProperty Replace(string PropertyName, object Value)
        {
            if (this._property == null)
                this._property = new Dictionary<string, JsonProperty>((IEqualityComparer<string>)StringComparer.OrdinalIgnoreCase);
            if (this._property.ContainsKey(PropertyName))
                this._property[PropertyName] = new JsonProperty(Value);
            return this._property[PropertyName];
        }
 
        public T GetEntity<T>() where T : new()
        {
            T obj = new T();
            foreach (string propertyName in this.GetPropertyNames())
            {
                PropertyInfo property = obj.GetType().GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public);
                if (property != null)
                {
                    string s = this[propertyName].Value;
                    Type propertyType = property.PropertyType;
                    if (string.IsNullOrEmpty(s))
                    {
                        if (propertyType.Equals((object)TypeCode.String))
                            property.SetValue((object)obj, (object)"", (object[])null);
                    }
                    else if (propertyType.Equals((object)TypeCode.Boolean))
                        property.SetValue((object)obj, (object)(s == "1"), (object[])null);
                    else if (propertyType.Equals((object)TypeCode.DateTime))
                    {
                        DateTime result;
                        if (!DateTime.TryParse(s, out result))
                            result = DateTime.MinValue;
                        property.SetValue((object)obj, (object)result, (object[])null);
                    }
                    else
                        property.SetValue((object)obj, Convert.ChangeType((object)s, propertyType), (object[])null);
                }
            }
            return obj;
        }
 
        public static T GetEntityFromJson<T>(JsonObject js, ref T a)
        {
            if ((object)a != null && js != null)
            {
                foreach (PropertyInfo property in a.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
                {
                    if (js.HasProperty(property.Name) || js.HasProperty(property.Name.ToLower()))
                    {
                        string s;
                        if (js[property.Name] == null)
                        {
                            if (js[property.Name.ToLower()] != null)
                                s = js[property.Name.ToLower()].Value;
                            else
                                goto label_18;
                        }
                        else
                            s = js[property.Name].Value;
                        TypeCode typeCode = Type.GetTypeCode(property.PropertyType);
                        try
                        {
                            if ((s == null || s.Length == 0) && typeCode == TypeCode.String)
                                property.SetValue((object)a, (object)"", (object[])null);
                            switch (typeCode)
                            {
                                case TypeCode.Boolean:
                                    property.SetValue((object)a, (object)(s == "1"), (object[])null);
                                    break;
                                case TypeCode.DateTime:
                                    DateTime result;
                                    if (!DateTime.TryParse(s, out result))
                                        result = DateTime.MinValue;
                                    property.SetValue((object)a, (object)result, (object[])null);
                                    break;
                                default:
                                    property.SetValue((object)a, Convert.ChangeType((object)s, property.PropertyType), (object[])null);
                                    break;
                            }
                        }
                        catch (Exception ex)
                        {
                            throw new ArgumentException("[" + property.Name + "]Json绑定失败:" + ex.Message);
                        }
                    label_18:;
                    }
                }
            }
            return a;
        }
 
        public string[] GetPropertyNames()
        {
            if (this._property == null)
                return (string[])null;
            string[] array = (string[])null;
            if (this._property.Count > 0)
            {
                array = new string[this._property.Count];
                this._property.Keys.CopyTo(array, 0);
            }
            return array;
        }
 
        public bool HasProperty(string PropertyName)
        {
            return this._property != null && this._property.ContainsKey(PropertyName);
        }
 
        public bool IsNull()
        {
            return this._property == null;
          }
 
        private void Parse(ref string jsonString)
        {
            int length = jsonString.Length;
            if (string.IsNullOrEmpty(jsonString) || jsonString.Substring(0, 1) != "{" || jsonString.Substring(jsonString.Length - 1, 1) != "}")
                throw new ArgumentException("传入文本不符合Json格式!" + jsonString);
            Stack<char> charStack1 = new Stack<char>();
            Stack<char> charStack2 = new Stack<char>();
            StringBuilder stringBuilder = new StringBuilder();
            bool flag1 = false;
            bool flag2 = false;
            JsonProperty jsonProperty = (JsonProperty)null;
            for (int index = 1; index <= length - 2; ++index)
            {
                char ch = jsonString[index];
                if (ch != '}') ;
                if (ch == ' ' && charStack1.Count == 0)
                    stringBuilder.Append(ch);
                else if ((ch == '\'' || ch == '"') && (!flag1 && charStack1.Count == 0) && !flag2)
                {
                    stringBuilder.Length = 0;
                    charStack1.Push(ch);
                }
                else if ((ch == '\'' || ch == '"') && (!flag1 && charStack1.Count > 0) && (int)charStack1.Peek() == (int)ch && !flag2)
                {
                    int num1 = (int)charStack1.Pop();
                }
                else if ((ch == '[' || ch == '{') && charStack1.Count == 0)
                {
                    charStack2.Push(ch == '[' ? ']' : '}');
                    stringBuilder.Append(ch);
                }
                else if ((ch == ']' || ch == '}') && charStack1.Count == 0 && (int)charStack2.Peek() == (int)ch)
                {
                    int num2 = (int)charStack2.Pop();
                    stringBuilder.Append(ch);
                }
                else if (ch == ':' && charStack1.Count == 0 && charStack2.Count == 0 && !flag2)
                {
                    jsonProperty = new JsonProperty();
                    this[stringBuilder.ToString()] = jsonProperty;
                    flag2 = true;
                    stringBuilder.Length = 0;
                }
                else if (ch == ',' && charStack1.Count == 0 && charStack2.Count == 0)
                {
                    if (jsonProperty != null)
                    {
                        string jsonString1 = stringBuilder.ToString();
                        jsonProperty.Parse(ref jsonString1);
                    }
                    flag2 = false;
                    stringBuilder.Length = 0;
                }
                else
                    stringBuilder.Append(ch);
            }
            if (stringBuilder.Length <= 0 || jsonProperty == null || jsonProperty.Type != JsonPropertyType.Null)
                return;
            string jsonString2 = stringBuilder.ToString();
            jsonProperty.Parse(ref jsonString2);
        }
 
        public virtual T Properties<T>(string PropertyName) where T : class
        {
            JsonProperty jsonProperty = this[PropertyName];
            if (jsonProperty != null)
                return jsonProperty.GetValue<T>();
            return default(T);
        }
 
        public JsonProperty RemoveProperty(string PropertyName)
        {
            if (this._property == null || !this._property.ContainsKey(PropertyName))
                return (JsonProperty)null;
            JsonProperty jsonProperty = this._property[PropertyName];
            this._property.Remove(PropertyName);
            return jsonProperty;
        }
 
        public override string ToString()
        {
            return this.ToString("");
        }
 
        public virtual string ToString(string format)
        {
            if (this.IsNull())
                return "{}";
            StringBuilder stringBuilder = new StringBuilder();
            foreach (string key in this._property.Keys)
            {
                stringBuilder.Append(",");
                stringBuilder.Append(string.Format("\"{0}\"", (object)key)).Append(": ");
                stringBuilder.Append(this._property[key].ToString(format));
            }
            if (this._property.Count > 0)
                stringBuilder.Remove(0, 1);
            stringBuilder.Insert(0, "{");
            stringBuilder.Append("}");
            return stringBuilder.ToString();
        }
 
        public JsonProperty this[string PropertyName]
        {
            get
            {
                if (this._property != null && this._property.ContainsKey(PropertyName))
                    return this._property[PropertyName];
                return this.AddProperty(PropertyName);
            }
            set
            {
                if (this._property == null)
                    this._property = new Dictionary<string, JsonProperty>((IEqualityComparer<string>)StringComparer.OrdinalIgnoreCase);
                if (this._property.ContainsKey(PropertyName))
                    this._property[PropertyName] = value;
                else
                    this._property.Add(PropertyName, value);
            }
        }
    }
}