Skip to content
Navigation Menu
{{ message }}
forked from rpgmaker/NetJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModels.cs
More file actions
348 lines (316 loc) · 8.96 KB
/
Copy pathModels.cs
File metadata and controls
348 lines (316 loc) · 8.96 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
using System;
using System.IO;
namespace NetJSON
{
/// <summary>
/// Attribute for renaming field/property name to use for serialization and deserialization
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Enum)]
public sealed class NetJSONPropertyAttribute : Attribute
{
/// <summary>
/// Name of property/field
/// </summary>
public string Name { get; private set; }
/// <summary>
/// Default constructor
/// </summary>
/// <param name="name"></param>
public NetJSONPropertyAttribute(string name) {
Name = name;
}
}
public unsafe sealed class NetJSONStringReader
{
private char* ptr;
private int index;
internal int counter;
public NetJSONStringReader() { }
internal NetJSONStringReader(char* ptr, int index)
{
this.ptr = ptr;
this.index = index;
this.counter++;
this.index++;
}
public char Next()
{
counter++;
return ptr[index++];
}
}
public sealed class NetJSONSettings
{
internal bool _hasDateStringFormat = false;
/// <summary>
/// Determine date format: Default: Default
/// </summary>
public NetJSONDateFormat DateFormat { get; set; }
internal string _dateStringFormat;
/// <summary>
/// String Format to use for formatting date when provided
/// </summary>
public string DateStringFormat {
get
{
return _dateStringFormat;
}
set
{
_dateStringFormat = value;
_hasDateStringFormat = !string.IsNullOrEmpty(value);
}
}
/// <summary>
/// Determine time zone format: Default : Unspecified
/// </summary>
public NetJSONTimeZoneFormat TimeZoneFormat { get; set; }
/// <summary>
/// Determine formatting for output json: Default: Default
/// </summary>
public NetJSONFormat Format { get; set; }
/// <summary>
/// Determine if Enum should be serialized as string or int value. Default: True
/// </summary>
public bool UseEnumString { get; set; }
/// <summary>
/// Determine if default value should be skipped: Default: True
/// </summary>
public bool SkipDefaultValue { get; set; }
public StringComparison _caseComparison = StringComparison.Ordinal;
private bool _caseSensitive;
/// <summary>
/// Determine case sensitive for property/field name: Default: True
/// </summary>
public bool CaseSensitive {
get {
return _caseSensitive;
}
set {
_caseSensitive = value;
_caseComparison = _caseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;
}
}
private NetJSONQuote _quoteType;
/// <summary>
/// Quote Type: Default: Double Quote
/// </summary>
public NetJSONQuote QuoteType {
get {
return _quoteType;
}
set {
_quoteType = value;
_quoteChar = _quoteType == NetJSONQuote.Single ? '\'' : '"';
_quoteCharString = _quoteType == NetJSONQuote.Single ? "'" : "\"";
}
}
public char _quoteChar;
public string _quoteCharString;
public bool HasOverrideQuoteChar { get; internal set; }
public bool UseStringOptimization { get; set; }
/// <summary>
/// Enable including type information for serialization and deserialization
/// </summary>
public bool IncludeTypeInformation { get; set; }
private StringComparison CaseComparison {
get {
return CaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;
}
}
/// <summary>
/// Enable camelCasing for property/field names
/// </summary>
public bool CamelCase { get; set; }
/// <summary>
/// Default constructor
/// </summary>
public NetJSONSettings() {
IncludeTypeInformation = NetJSON.IncludeTypeInformation;
DateFormat = NetJSON.DateFormat;
TimeZoneFormat = NetJSON.TimeZoneFormat;
UseEnumString = NetJSON.UseEnumString;
SkipDefaultValue = NetJSON.SkipDefaultValue;
CaseSensitive = NetJSON.CaseSensitive;
QuoteType = NetJSON.QuoteType;
UseStringOptimization = NetJSON.UseStringOptimization;
Format = NetJSONFormat.Default;
CamelCase = false;
}
/// <summary>
/// Clone settings
/// </summary>
/// <returns></returns>
public NetJSONSettings Clone() {
return new NetJSONSettings {
IncludeTypeInformation = IncludeTypeInformation,
DateFormat = DateFormat,
TimeZoneFormat = TimeZoneFormat,
UseEnumString = UseEnumString,
SkipDefaultValue = SkipDefaultValue,
CaseSensitive = CaseSensitive,
QuoteType = QuoteType,
UseStringOptimization = UseStringOptimization,
Format = Format
};
}
[ThreadStatic]
private static NetJSONSettings _currentSettings;
/// <summary>
/// Returns current NetJSONSettings that correspond to old use of settings
/// </summary>
public static NetJSONSettings CurrentSettings {
get {
return _currentSettings ?? (_currentSettings = new NetJSONSettings());
}
}
}
/// <summary>
/// Attribute for configuration of Class that requires type information for serialization and deserialization
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Struct, AllowMultiple = true)]
public sealed class NetJSONKnownTypeAttribute : Attribute
{
/// <summary>
/// Type
/// </summary>
public Type Type { private set; get; }
/// <summary>
/// Default constructor
/// </summary>
/// <param name="type"></param>
public NetJSONKnownTypeAttribute(Type type) {
Type = type;
}
}
/// <summary>
/// Exception thrown for invalid json string
/// </summary>
public sealed class NetJSONInvalidJSONException : Exception
{
public NetJSONInvalidJSONException()
: base("Input is not a valid JSON.") {
}
}
/// <summary>
/// Exception thrown for invalid json string
/// </summary>
public sealed class NetJSONTypeMismatchException : Exception
{
public NetJSONTypeMismatchException()
: base("Unexpected type was encountered in JSON")
{
}
}
/// <summary>
/// Exception thrown for invalid json property attribute
/// </summary>
public sealed class NetJSONInvalidJSONPropertyException : Exception
{
/// <summary>
/// Default constructor
/// </summary>
public NetJSONInvalidJSONPropertyException()
: base("Class cannot contain any NetJSONProperty with null or blank space character") {
}
}
/// <summary>
/// Exception thrown for invalid assembly generation when adding all assembly into a specified assembly file
/// </summary>
public sealed class NetJSONInvalidAssemblyGeneration : Exception
{
/// <summary>
/// Default constructor
/// </summary>
/// <param name="asmName"></param>
public NetJSONInvalidAssemblyGeneration(string asmName) : base(String.Format("Could not generate assembly with name [{0}] due to empty list of types to include", asmName)) { }
}
internal abstract class NetJSONSerializer<T>
{
public abstract string Serialize(T value);
public abstract T Deserialize(string value);
public abstract void Serialize(T value, TextWriter writer);
public abstract T Deserialize(TextReader reader);
//With Settings
public abstract string Serialize(T value, NetJSONSettings settings);
public abstract T Deserialize(string value, NetJSONSettings settings);
public abstract void Serialize(T value, TextWriter writer, NetJSONSettings settings);
public abstract T Deserialize(TextReader reader, NetJSONSettings settings);
}
/// <summary>
/// Option for determining date formatting
/// </summary>
public enum NetJSONDateFormat
{
/// <summary>
/// Default /Date(...)/
/// </summary>
Default = 0,
/// <summary>
/// ISO Format
/// </summary>
ISO = 2,
/// <summary>
/// Unix Epoch Milliseconds
/// </summary>
EpochTime = 4,
/// <summary>
/// JSON.NET Format for backward compatibility
/// </summary>
JsonNetISO = 6,
/// <summary>
/// .NET System.Web.Script.Serialization.JavaScriptSerializer backward compatibility
/// </summary>
JavascriptSerializer = 8
}
/// <summary>
/// Option for determining timezone formatting
/// </summary>
public enum NetJSONTimeZoneFormat
{
/// <summary>
/// Default unspecified
/// </summary>
Unspecified = 0,
/// <summary>
/// Utc
/// </summary>
Utc = 2,
/// <summary>
/// Local time
/// </summary>
Local = 4
}
/// <summary>
/// Option for determine what type of quote to use for serialization and deserialization
/// </summary>
public enum NetJSONQuote
{
/// <summary>
/// Default: double quote
/// </summary>
Default = 0,
/// <summary>
/// Use double quote
/// </summary>
Double = Default,
/// <summary>
/// Use single quote
/// </summary>
Single = 2
}
/// <summary>
/// Options for controlling serialize json format
/// </summary>
public enum NetJSONFormat
{
/// <summary>
/// Default
/// </summary>
Default = 0,
/// <summary>
/// Prettify string
/// </summary>
Prettify = 2
}
}
You can’t perform that action at this time.
