source: binary-improvements2/7dtd-server-fixes/src/JSON/JSONString.cs@ 393

Last change on this file since 393 was 391, checked in by alloc, 2 years ago

Major refactoring/cleanup

File size: 2.6 KB
RevLine 
[160]1using System.Text;
[154]2
[325]3namespace AllocsFixes.JSON {
[391]4 public class JsonString : JsonValue {
[325]5 private readonly string value;
[154]6
[391]7 public JsonString (string _value) {
[351]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) {
[391]16 if (string.IsNullOrEmpty (value)) {
[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
[391]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 ());
[391]106 return new JsonString (sb.ToString ());
[187]107 default:
[351]108 sb.Append (_json [_offset]);
109 _offset++;
[187]110 break;
111 }
112 }
[325]113
[391]114 throw new MalformedJsonException ("End of JSON reached before parsing string finished");
[187]115 }
[389]116
117 public override string AsString => value;
118 public override int AsInt => int.Parse (value);
119 public override double AsDouble => double.Parse (value);
[154]120 }
[325]121}
Note: See TracBrowser for help on using the repository browser.