source: binary-improvements/7dtd-server-fixes/src/JSON/JSONString.cs@ 161

Last change on this file since 161 was 160, checked in by alloc, 10 years ago

fixes

File size: 1.1 KB
Line 
1using System;
2using System.Text;
3
4namespace AllocsFixes.JSON
5{
6 public class JSONString : JSONNode
7 {
8 private string value;
9
10 public JSONString (string value)
11 {
12 this.value = value;
13 }
14
15 public override string ToString ()
16 {
17 if (value == null || value.Length == 0) {
18 return "";
19 }
20
21 int len = value.Length;
22
23 StringBuilder sb = new StringBuilder (len + 4);
24 String t;
25
26 foreach (char c in value) {
27 switch (c) {
28 case '\\':
29 case '"':
30 case '/':
31 sb.Append ('\\');
32 sb.Append (c);
33 break;
34 case '\b':
35 sb.Append ("\\b");
36 break;
37 case '\t':
38 sb.Append ("\\t");
39 break;
40 case '\n':
41 sb.Append ("\\n");
42 break;
43 case '\f':
44 sb.Append ("\\f");
45 break;
46 case '\r':
47 sb.Append ("\\r");
48 break;
49 default:
50 if (c < ' ') {
51 t = "000" + String.Format ("X", c);
52 sb.Append ("\\u" + t.Substring (t.Length - 4));
53 } else {
54 sb.Append (c);
55 }
56 break;
57 }
58 }
59
60 return string.Format ("\"{0}\"", sb.ToString ());
61 }
62
63 }
64}
65
Note: See TracBrowser for help on using the repository browser.