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