Line | |
---|
1 | using System;
|
---|
2 | using System.Text;
|
---|
3 |
|
---|
4 | namespace AllocsFixes.JSON
|
---|
5 | {
|
---|
6 | public class JSONString : JSONValue
|
---|
7 | {
|
---|
8 | private string value;
|
---|
9 |
|
---|
10 | public JSONString (string value)
|
---|
11 | {
|
---|
12 | this.value = value;
|
---|
13 | }
|
---|
14 |
|
---|
15 | public string GetString ()
|
---|
16 | {
|
---|
17 | return value;
|
---|
18 | }
|
---|
19 |
|
---|
20 | public override string ToString (bool prettyPrint = false, int currentLevel = 0)
|
---|
21 | {
|
---|
22 | if (value == null || value.Length == 0) {
|
---|
23 | return "\"\"";
|
---|
24 | }
|
---|
25 |
|
---|
26 | int len = value.Length;
|
---|
27 |
|
---|
28 | StringBuilder sb = new StringBuilder (len + 4);
|
---|
29 | String t;
|
---|
30 |
|
---|
31 | foreach (char c in value) {
|
---|
32 | switch (c) {
|
---|
33 | case '\\':
|
---|
34 | case '"':
|
---|
35 | case '/':
|
---|
36 | sb.Append ('\\');
|
---|
37 | sb.Append (c);
|
---|
38 | break;
|
---|
39 | case '\b':
|
---|
40 | sb.Append ("\\b");
|
---|
41 | break;
|
---|
42 | case '\t':
|
---|
43 | sb.Append ("\\t");
|
---|
44 | break;
|
---|
45 | case '\n':
|
---|
46 | sb.Append ("\\n");
|
---|
47 | break;
|
---|
48 | case '\f':
|
---|
49 | sb.Append ("\\f");
|
---|
50 | break;
|
---|
51 | case '\r':
|
---|
52 | sb.Append ("\\r");
|
---|
53 | break;
|
---|
54 | default:
|
---|
55 | if (c < ' ') {
|
---|
56 | t = "000" + String.Format ("X", c);
|
---|
57 | sb.Append ("\\u" + t.Substring (t.Length - 4));
|
---|
58 | } else {
|
---|
59 | sb.Append (c);
|
---|
60 | }
|
---|
61 | break;
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | return string.Format ("\"{0}\"", sb.ToString ());
|
---|
66 | }
|
---|
67 |
|
---|
68 | public static JSONString Parse (string json, ref int offset)
|
---|
69 | {
|
---|
70 | //Log.Out ("ParseString enter (" + offset + ")");
|
---|
71 | StringBuilder sb = new StringBuilder ();
|
---|
72 | offset++;
|
---|
73 | while (offset < json.Length) {
|
---|
74 | switch (json [offset]) {
|
---|
75 | case '\\':
|
---|
76 | offset++;
|
---|
77 | switch (json [offset]) {
|
---|
78 | case '\\':
|
---|
79 | case '"':
|
---|
80 | case '/':
|
---|
81 | sb.Append (json [offset]);
|
---|
82 | break;
|
---|
83 | case 'b':
|
---|
84 | sb.Append ('\b');
|
---|
85 | break;
|
---|
86 | case 't':
|
---|
87 | sb.Append ('\t');
|
---|
88 | break;
|
---|
89 | case 'n':
|
---|
90 | sb.Append ('\n');
|
---|
91 | break;
|
---|
92 | case 'f':
|
---|
93 | sb.Append ('\f');
|
---|
94 | break;
|
---|
95 | case 'r':
|
---|
96 | sb.Append ('\r');
|
---|
97 | break;
|
---|
98 | default:
|
---|
99 | sb.Append (json [offset]);
|
---|
100 | break;
|
---|
101 | }
|
---|
102 | offset++;
|
---|
103 | break;
|
---|
104 | case '"':
|
---|
105 | offset++;
|
---|
106 | //Log.Out ("JSON:Parsed String: " + sb.ToString ());
|
---|
107 | return new JSONString (sb.ToString ());
|
---|
108 | default:
|
---|
109 | sb.Append (json [offset]);
|
---|
110 | offset++;
|
---|
111 | break;
|
---|
112 | }
|
---|
113 | }
|
---|
114 | throw new MalformedJSONException ("End of JSON reached before parsing string finished");
|
---|
115 | }
|
---|
116 |
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
Note:
See
TracBrowser
for help on using the repository browser.