csharp-code-MinJson

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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
using System.Collections;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;



public static class MinJson
{
/// <summary>
/// Parses the string json into a value
/// </summary>
/// <param name="json">A JSON string.</param>
/// <returns>An List<object>, a Dictionary<string, object>, a double, an integer,a string, null, true, or false</returns>
//反序列化
public static object Deserialize(string json)
{
// save the string for debug information
if (json == null)
{
return null;
}
return Parser.Parse(json);
}

//阻止其他类从该类继承
sealed class Parser : IDisposable
{
const string WORD_BREAK = "{}[],:\"";
public static bool IsWordBreak(char c)
{
// 如果 c 是空白,则为 true;否则,为 false;报告指定 Unicode 字符在此字符串中的第一个匹配项的索引。
return Char.IsWhiteSpace(c) || WORD_BREAK.IndexOf(c) != -1;
}
enum TOKEN
{
NONE,
CURLY_OPEN,
CURLY_CLOSE,
SQUARED_OPEN,
SQUARED_CLOSE,
COLON,
COMMA,
STRING,
NUMBER,
TRUE,
FALSE,
NULL
};
// 实现从字符串进行读取的 System.IO.TextReader。
StringReader json;
Parser(string jsonString)
{
json = new StringReader(jsonString);
}
public static object Parse(string jsonString)
{
using (var instance = new Parser(jsonString))
{
return instance.ParseValue();
}
}
//释放
public void Dispose()
{
json.Dispose();
json = null;
}
Dictionary<string, object> ParseObject()
{
Dictionary<string, object> table = new Dictionary<string, object>();
// ditch opening brace
json.Read();
// {
while (true)
{
switch (NextToken)
{
case TOKEN.NONE:
return null;
case TOKEN.COMMA:
continue;
case TOKEN.CURLY_CLOSE:
return table;
default:
// name
string name = ParseString();
if (name == null)
{
return null;
}
// :
if (NextToken != TOKEN.COLON)
{
return null;
}
// ditch the colon
json.Read();
// value
table[name] = ParseValue();
break;
}
}
}
List<object> ParseArray()
{
List<object> array = new List<object>();
// ditch opening bracket
json.Read();
// [
bool parsing = true;
while (parsing)
{
TOKEN nextToken = NextToken;
switch (nextToken)
{
case TOKEN.NONE:
return null;
case TOKEN.COMMA:
continue;
case TOKEN.SQUARED_CLOSE:
parsing = false;
break;
default:
object value = ParseByToken(nextToken);
array.Add(value);
break;
}
}
return array;
}
object ParseValue()
{
TOKEN nextToken = NextToken;
return ParseByToken(nextToken);
}
object ParseByToken(TOKEN token)
{
switch (token)
{
case TOKEN.STRING:
return ParseString();
case TOKEN.NUMBER:
return ParseNumber();
case TOKEN.CURLY_OPEN:
return ParseObject();
case TOKEN.SQUARED_OPEN:
return ParseArray();
case TOKEN.TRUE:
return true;
case TOKEN.FALSE:
return false;
case TOKEN.NULL:
return null;
default:
return null;
}
}
string ParseString()
{
StringBuilder s = new StringBuilder();
char c;
// ditch opening quote
json.Read();
bool parsing = true;
while (parsing)
{
if (json.Peek() == -1)
{
parsing = false;
break;
}
c = NextChar;
switch (c)
{
case '"':
parsing = false;
break;
case '\\':
if (json.Peek() == -1)
{
parsing = false;
break;
}
c = NextChar;
switch (c)
{
case '"':
case '\\':
case '/':
s.Append(c);
break;
case 'b':
s.Append('\b');
break;
case 'f':
s.Append('\f');
break;
case 'n':
s.Append('\n');
break;
case 'r':
s.Append('\r');
break;
case 't':
s.Append('\t');
break;
case 'u':
var hex = new char[4];
for (int i = 0; i < 4; i++)
{
hex[i] = NextChar;
}
s.Append((char)Convert.ToInt32(new string(hex), 16));
break;
}
break;
default:
s.Append(c);
break;
}
}
return s.ToString();
}
object ParseNumber()
{
string number = NextWord;
// 摘要:
// 报告指定 Unicode 字符在此字符串中的第一个匹配项的索引。
//
// 参数:
// value:
// 要查找的 Unicode 字符。
//
// 返回结果:
// 如果找到该字符,则为 value 的从零开始的索引位置;如果未找到,则为 -1。
if (number.IndexOf('.') == -1)
{
long parsedInt;
// 将数字的字符串表示形式转换为它的等效 64 位有符号整数。一个指示转换是否成功的返回值。
Int64.TryParse(number, out parsedInt);
return parsedInt;
}
double parsedDouble;
Double.TryParse(number, out parsedDouble);
return parsedDouble;
}
//
void EatWhitespace()
{
//指示指定字符串中位于指定位置处的字符是否属于空白类别。
while (Char.IsWhiteSpace(PeekChar))
{
json.Read();
//摘要:
// 返回下一个可用的字符,但不使用它。
//
// 返回结果:
// 表示下一个要读取的字符的整数,或者,如果没有更多的可用字符或该流不支持查找,则为 -1。
if (json.Peek() == -1)
{
break;
}
}
}
char PeekChar
{
get
{
// 读取输入字符串中的下一个字符并将该字符的位置提升一个字符。
//
// 返回结果:
// 基础字符串中的下一个字符,或者如果没有更多的可用字符,则为 -1。
return Convert.ToChar(json.Peek());
}
}
char NextChar
{
get
{
return Convert.ToChar(json.Read());
}
}
string NextWord
{
get
{
// 表示可变字符字符串。无法继承此类。
StringBuilder word = new StringBuilder();
while (!IsWordBreak(PeekChar))
{
// 摘要:
// 在此实例的结尾追加指定 Unicode 字符的字符串表示形式。
//
// 参数:
// value:
// 要追加的 Unicode 字符。
//
// 返回结果:
// 完成追加操作后对此实例的引用。
word.Append(NextChar);
//下一个字符为空
if (json.Peek() == -1)
{
break;
}
}
//
return word.ToString();
}
}
TOKEN NextToken
{
get
{
EatWhitespace();
if (json.Peek() == -1)
{
return TOKEN.NONE;
}
switch (PeekChar)
{
case '{':
return TOKEN.CURLY_OPEN;
case '}':
json.Read();
return TOKEN.CURLY_CLOSE;
case '[':
return TOKEN.SQUARED_OPEN;
case ']':
json.Read();
return TOKEN.SQUARED_CLOSE;
case ',':
json.Read();
return TOKEN.COMMA;
case '"':
return TOKEN.STRING;
case ':':
return TOKEN.COLON;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '-':
return TOKEN.NUMBER;
}
switch (NextWord)
{
case "false":
return TOKEN.FALSE;
case "true":
return TOKEN.TRUE;
case "null":
return TOKEN.NULL;
}
return TOKEN.NONE;
}
}
}
/// <summary>
/// Converts a IDictionary / IList object or a simple type (string, int, etc.) into a JSON string
/// </summary>
/// <param name="json">A Dictionary<string, object> / List<object></param>
/// <returns>A JSON encoded string, or null if object 'json' is not serializable</returns>
public static string Serialize(object obj)
{
return Serializer.Serialize(obj);
}

sealed class Serializer
{
StringBuilder builder;
Serializer()
{
//创建生成器
builder = new StringBuilder();
}
//序列化
public static string Serialize(object obj)
{
var instance = new Serializer();
instance.SerializeValue(obj);
return instance.builder.ToString();
}
//类型
void SerializeValue(object value)
{
IList asList;
IDictionary asDict;
string asStr;
if (value == null)
{
builder.Append("null");
}
else if ((asStr = value as string) != null)
{
SerializeString(asStr);
}
else if (value is bool)
{
builder.Append((bool)value ? "true" : "false");
}
else if ((asList = value as IList) != null)
{
SerializeArray(asList);
}
else if ((asDict = value as IDictionary) != null)
{
SerializeObject(asDict);
}
else if (value is char)
{
SerializeString(new string((char)value, 1));
}
else
{
SerializeOther(value);
}
}
//序列化对象
void SerializeObject(IDictionary obj)
{
bool first = true;
builder.Append('{');
foreach (object e in obj.Keys)
{
if (!first)
{
builder.Append(',');
}
SerializeString(e.ToString());
builder.Append(':');
SerializeValue(obj[e]);
first = false;
}
builder.Append('}');
}
// 序列化数组
void SerializeArray(IList anArray)
{
builder.Append('[');
bool first = true;
foreach (object obj in anArray)
{
if (!first)
{
builder.Append(',');
}
SerializeValue(obj);
first = false;
}
builder.Append(']');
}
//string
void SerializeString(string str)
{
builder.Append('\"');
char[] charArray = str.ToCharArray();
foreach (var c in charArray)
{
switch (c)
{
case '"':
builder.Append("\\\"");
break;
case '\\':
builder.Append("\\\\");
break;
case '\b':
builder.Append("\\b");
break;
case '\f':
builder.Append("\\f");
break;
case '\n':
builder.Append("\\n");
break;
case '\r':
builder.Append("\\r");
break;
case '\t':
builder.Append("\\t");
break;
default:
int codepoint = Convert.ToInt32(c);
if ((codepoint >= 32) && (codepoint <= 126))
{
builder.Append(c);
}
else
{
builder.Append("\\u");
builder.Append(codepoint.ToString("x4"));
}
break;
}
}
builder.Append('\"');
}
//其他
void SerializeOther(object value)
{
// NOTE: decimals lose precision during serialization.
// They always have, I'm just letting you know.
// Previously floats and doubles lost precision too.
//注意:小数在序列化过程中丢失精度。
//他们总是有,我只是让你知道。
//以前失去精度和双精度浮点数。
if (value is float)
{
builder.Append(((float)value).ToString("R"));
}
else if (value is int
|| value is uint
|| value is long
|| value is sbyte
|| value is byte
|| value is short
|| value is ushort
|| value is ulong)
{
builder.Append(value);
}
else if (value is double
|| value is decimal)
{
builder.Append(Convert.ToDouble(value).ToString("R"));
}
else
{
SerializeString(value.ToString());
}
}
}
}

public class JsonDeserializer
{

/// <summary>
/// A Class used to direct which class to make when it's not obvious, like if you have a class
/// with a member that's a base class but the actual class could be one of many derived classes.
/// </summary>
public abstract class CustomCreator
{

/// <summary>
/// Creates an new derived class when a base is expected
/// </summary>
/// <param name="src">A dictionary of the json fields that belong to the object to be created.</param>
/// <param name="parentSrc">A dictionary of the json fields that belong to the object that is the parent of the object to be created.</param>
/// <example>
/// Example: Assume you have the following classes
/// <code>
/// class Fruit { public int type; }
/// class Apple : Fruit { public float height; public float radius; };
/// class Raspberry : Fruit { public int numBulbs; }
/// </code>
/// You'd register a dervied CustomCreator for type `Fruit`. When the Deserialize needs to create
/// a `Fruit` it will call your Create function. Using `src` you could look at `type` and
/// decide whether to make an Apple or a Raspberry.
/// <code>
/// int type = src["type"];
/// if (type == 0) { return new Apple; }
/// if (type == 1) { return new Raspberry; }
/// ..
/// </code>
/// If the parent has info on the type you can do this
/// <code>
/// class Fruit { }
/// class Apple : Fruit { public float height; public float radius; };
/// class Raspberry : Fruit { public int numBulbs; }
/// class Basket { public int typeInBasket; Fruit fruit; }
/// </code>
/// In this case again, when trying to create a `Fruit` your CustomCreator.Create function
/// will be called. You can use `'parentSrc`' to look at the fields from 'Basket' as in
/// <code>
/// int typeInBasket = parentSrc['typeInBasket'];
/// if (type == 0) { return new Apple; }
/// if (type == 1) { return new Raspberry; }
/// ..
/// </code>
/// </example>
/// <returns>The created object</returns>
public abstract object Create(Dictionary<string, object> src, Dictionary<string, object> parentSrc);

/// <summary>
/// The base type this CustomCreator makes.
/// </summary>
/// <returns>The type this CustomCreator makes.</returns>
public abstract System.Type TypeToCreate();
}

/// <summary>
/// Deserializer for Json to your classes.
/// </summary>
public JsonDeserializer()
{
m_creators = new Dictionary<System.Type, CustomCreator>();
}

/// <summary>
/// Deserializes a json string into classes.
/// </summary>
/// <param name="json">String containing JSON</param>
/// <param name="includeTypeInfoForDerviedTypes">Default false</param>
/// <returns>An instance of class T.</returns>
/// <example>
/// <code>
/// public class Foo {
/// public int num;
/// public string name;
/// public float weight;
/// };
///
/// public class Bar {
/// public int hp;
/// public Foo someFoo;
/// };
/// ...
/// Deserializer deserializer = new Deserializer();
///
/// string json = "{\"hp\":123,\"someFoo\":{\"num\":456,\"name\":\"gman\",\"weight\":156.4}}";
///
/// Bar bar = deserializer.Deserialize<Bar>(json);
///
/// print("bar.hp: " + bar.hp);
/// print("bar.someFoo.num: " + bar.someFoo.num);
/// print("bar.someFoo.name: " + bar.someFoo.name);
/// print("bar.someFoo.weight: " + bar.someFoo.weight);
///
/// </code>
/// </example>
public static T Deserialize<T>(string json)
{
JsonDeserializer dSer = new JsonDeserializer();
object o = MinJson.Deserialize(json);

return dSer.Deserialize<T>(o);
}

public T Deserialize<T>(object o)
{
return (T)ConvertToType(o, typeof(T), null);
}

/// <summary>
/// Registers a CustomCreator.
/// </summary>
/// <param name="creator">The creator to register</param>
public void RegisterCreator(CustomCreator creator)
{
System.Type t = creator.TypeToCreate();
m_creators[t] = creator;
}

private object DeserializeO(Type destType, Dictionary<string, object> src, Dictionary<string, object> parentSrc)
{
object dest = null;

// This seems like a hack but for now maybe it's the right thing?
// Basically if the thing you want is a Dictionary<stirng, object>
// Then just give it do you since that's the source. No need
// to try to copy it.
if (destType == typeof(Dictionary<string, object>))
{
return src;
}

// First see if there is a CustomCreator for this type.
CustomCreator creator;
if (m_creators.TryGetValue(destType, out creator))
{
dest = creator.Create(src, parentSrc);
}

if (dest == null)
{
// Check if there is a type serialized for this
object typeNameObject;
if (src.TryGetValue("$dotNetType", out typeNameObject))
{
destType = System.Type.GetType((string)typeNameObject);
}
dest = Activator.CreateInstance(destType);
}

DeserializeIt(dest, src);
return dest;
}

private void DeserializeIt(object dest, Dictionary<string, object> src)
{
System.Type type = dest.GetType();
System.Reflection.FieldInfo[] fields = type.GetFields();

DeserializeClassFields(dest, fields, src);
}

private void DeserializeClassFields(object dest, System.Reflection.FieldInfo[] fields, Dictionary<string, object> src)
{
foreach (System.Reflection.FieldInfo info in fields)
{
object value;

if (src.TryGetValue(info.Name, out value))
{
DeserializeField(dest, info, value, src);
}
}
}

private void DeserializeField(object dest, System.Reflection.FieldInfo info, object value, Dictionary<string, object> src)
{
Type fieldType = info.FieldType;
object o = ConvertToType(value, fieldType, src);
info.SetValue(dest, o);
}

private object ConvertToType(object value, System.Type type, Dictionary<string, object> src)
{
if (type.IsArray)
{
return ConvertToArray(value, type, src);
}
else if (type == typeof(string))
{
return Convert.ToString(value);
}
else if (type == typeof(int))
{
return Convert.ToInt32(value);
}
else if (type == typeof(float))
{
return Convert.ToSingle(value);
}
else if (type == typeof(double))
{
return Convert.ToDouble(value);
}
else if (type == typeof(bool))
{
return Convert.ToBoolean(value);
}
else if (type == typeof(ulong))
{
return Convert.ToUInt64(value);
}
else if (type == typeof(long))
{
return Convert.ToInt64(value);
}
else if (type == typeof(ushort))
{
return Convert.ToUInt16(value);
}
else if (type == typeof(short))
{
return Convert.ToInt16(value);
}
else if (type.IsClass)
{
return DeserializeO(type, (Dictionary<string, object>)value, src);
}
else
{
// Should we throw here?
}
return value;
}

private object ConvertToArray(object value, System.Type type, Dictionary<string, object> src)
{
List<object> elements = (List<object>)value;
int numElements = elements.Count;
Type elementType = type.GetElementType();
Array array = Array.CreateInstance(elementType, numElements);
int index = 0;
foreach (object elementValue in elements)
{
object o = ConvertToType(elementValue, elementType, src);
array.SetValue(o, index);
++index;
}
return array;
}

private Dictionary<System.Type, CustomCreator> m_creators;
};

public class JsonSerializer
{
public static string Serialize(object obj, bool includeTypeInfoForDerivedTypes = false)
{
JsonSerializer s = new JsonSerializer(includeTypeInfoForDerivedTypes);
s.SerializeValue(obj);
return s.GetJson();
}

private JsonSerializer(bool includeTypeInfoForDerivedTypes)
{
m_builder = new StringBuilder();
m_includeTypeInfoForDerivedTypes = includeTypeInfoForDerivedTypes;
}

private string GetJson()
{
return m_builder.ToString();
}

private StringBuilder m_builder;
private bool m_includeTypeInfoForDerivedTypes;

private void SerializeValue(object obj)
{
if (obj == null)
{
m_builder.Append("undefined");
return;
}
System.Type type = obj.GetType();

if (type.IsArray)
{
SerializeArray(obj);
}
else if (type == typeof(string))
{
SerializeString(obj as string);
}
else if (type == typeof(int))
{
m_builder.Append(obj);
}
else if (type == typeof(float))
{
m_builder.Append(((float)obj).ToString("R", System.Globalization.CultureInfo.InvariantCulture));
}
else if (type == typeof(double))
{
m_builder.Append(((double)obj).ToString("R", System.Globalization.CultureInfo.InvariantCulture));
}
else if (type == typeof(bool))
{
m_builder.Append((bool)obj ? "true" : "false");
}
else if (type == typeof(ulong))
{
m_builder.Append((ulong)obj);
}
else if (type == typeof(long))
{
m_builder.Append((long)obj);
}
else if (type == typeof(ushort))
{
m_builder.Append((ushort)obj);
}
else if (type == typeof(short))
{
m_builder.Append((short)obj);
}
else if (type.IsClass)
{
SerializeObject(obj);
}
else
{
throw new System.InvalidOperationException("unsupport type: " + type.Name);
}
}

private void SerializeArray(object obj)
{
m_builder.Append("[");
Array array = obj as Array;
bool first = true;
foreach (object element in array)
{
if (!first)
{
m_builder.Append(",");
}
SerializeValue(element);
first = false;
}
m_builder.Append("]");
}

private void SerializeObject(object obj)
{
m_builder.Append("{");
bool first = true;
if (m_includeTypeInfoForDerivedTypes)
{
// Only inlcude type info for derived types.
System.Type type = obj.GetType();
System.Type baseType = type.BaseType;
if (baseType != null && baseType != typeof(System.Object))
{
SerializeString("$dotNetType"); // assuming this won't clash with user's properties.
m_builder.Append(":");
SerializeString(type.FullName);
}
}
System.Reflection.FieldInfo[] fields = obj.GetType().GetFields();
foreach (System.Reflection.FieldInfo info in fields)
{
object fieldValue = info.GetValue(obj);
if (fieldValue != null)
{
if (!first)
{
m_builder.Append(",");
}
SerializeString(info.Name);
m_builder.Append(":");
SerializeValue(fieldValue);
first = false;
}
}
m_builder.Append("}");
}

private void SerializeString(string str)
{
m_builder.Append('\"');

char[] charArray = str.ToCharArray();
foreach (var c in charArray)
{
switch (c)
{
case '"':
m_builder.Append("\\\"");
break;
case '\\':
m_builder.Append("\\\\");
break;
case '\b':
m_builder.Append("\\b");
break;
case '\f':
m_builder.Append("\\f");
break;
case '\n':
m_builder.Append("\\n");
break;
case '\r':
m_builder.Append("\\r");
break;
case '\t':
m_builder.Append("\\t");
break;
default:
int codepoint = Convert.ToInt32(c);
if ((codepoint >= 32) && (codepoint <= 126))
{
m_builder.Append(c);
}
else
{
m_builder.Append("\\u");
m_builder.Append(codepoint.ToString("x4"));
}
break;
}
}

m_builder.Append('\"');
}
}