0.2.0-dev Download ZIP
257 lines ·6.9 KB Raw
| 1 | using System.Globalization; |
| 2 | using System.Text; |
| 3 | using System.Text.Json.Nodes; |
| 4 | |
| 5 | namespace EvilEngine.Adapters.BeamNG; |
| 6 | |
| 7 | public sealed class LooseJson(byte[] data) |
| 8 | { |
| 9 | private int _at = data.Length >= 3 && data[0] == 0xEF && data[1] == 0xBB && data[2] == 0xBF ? 3 : 0; |
| 10 | |
| 11 | public static JsonNode? Parse(byte[] data) |
| 12 | { |
| 13 | return new LooseJson(data).Read(); |
| 14 | } |
| 15 | |
| 16 | public bool EnterObject() |
| 17 | { |
| 18 | Space(); |
| 19 | if (_at >= data.Length || data[_at] != '{') |
| 20 | return false; |
| 21 | _at++; |
| 22 | return true; |
| 23 | } |
| 24 | |
| 25 | public bool EnterArray() |
| 26 | { |
| 27 | Space(); |
| 28 | if (_at >= data.Length || data[_at] != '[') |
| 29 | return false; |
| 30 | _at++; |
| 31 | return true; |
| 32 | } |
| 33 | |
| 34 | public bool NextMember(out string key) |
| 35 | { |
| 36 | Space(); |
| 37 | key = ""; |
| 38 | if (_at >= data.Length) |
| 39 | throw new FormatException("It ends inside an object."); |
| 40 | if (data[_at] == '}') |
| 41 | { |
| 42 | _at++; |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | key = Token(); |
| 47 | Space(); |
| 48 | if (_at < data.Length && data[_at] == ':') |
| 49 | _at++; |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | public bool NextItem() |
| 54 | { |
| 55 | Space(); |
| 56 | if (_at >= data.Length) |
| 57 | throw new FormatException("It ends inside an array."); |
| 58 | if (data[_at] != ']') |
| 59 | return true; |
| 60 | _at++; |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | public JsonNode? Read() |
| 65 | { |
| 66 | Space(); |
| 67 | if (_at >= data.Length) |
| 68 | throw new FormatException("It ends where a value should be."); |
| 69 | switch (data[_at]) |
| 70 | { |
| 71 | case (byte)'{': |
| 72 | { |
| 73 | _at++; |
| 74 | var node = new JsonObject(); |
| 75 | while (NextMember(out var key)) |
| 76 | node[key] = Read(); |
| 77 | return node; |
| 78 | } |
| 79 | case (byte)'[': |
| 80 | { |
| 81 | _at++; |
| 82 | var node = new JsonArray(); |
| 83 | while (NextItem()) |
| 84 | node.Add(Read()); |
| 85 | return node; |
| 86 | } |
| 87 | case (byte)'"': |
| 88 | return JsonValue.Create(Text()); |
| 89 | default: |
| 90 | return Literal(Bare()); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | public void Skip() |
| 95 | { |
| 96 | Space(); |
| 97 | if (_at >= data.Length) |
| 98 | throw new FormatException("It ends where a value should be."); |
| 99 | switch (data[_at]) |
| 100 | { |
| 101 | case (byte)'{': |
| 102 | _at++; |
| 103 | while (NextMember(out _)) |
| 104 | Skip(); |
| 105 | return; |
| 106 | case (byte)'[': |
| 107 | _at++; |
| 108 | while (NextItem()) |
| 109 | Skip(); |
| 110 | return; |
| 111 | case (byte)'"': |
| 112 | SkipText(); |
| 113 | return; |
| 114 | default: |
| 115 | Bare(); |
| 116 | return; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | private string Token() |
| 121 | { |
| 122 | return _at < data.Length && data[_at] == '"' ? Text() : Bare(); |
| 123 | } |
| 124 | |
| 125 | private void Space() |
| 126 | { |
| 127 | while (_at < data.Length) |
| 128 | { |
| 129 | var c = data[_at]; |
| 130 | if (c is (byte)' ' or (byte)'\t' or (byte)'\r' or (byte)'\n' or (byte)',') |
| 131 | { |
| 132 | _at++; |
| 133 | } |
| 134 | else if (c == '/' && _at + 1 < data.Length && data[_at + 1] == '/') |
| 135 | { |
| 136 | var end = data.AsSpan(_at).IndexOf((byte)'\n'); |
| 137 | _at = end < 0 ? data.Length : _at + end + 1; |
| 138 | } |
| 139 | else if (c == '/' && _at + 1 < data.Length && data[_at + 1] == '*') |
| 140 | { |
| 141 | var end = data.AsSpan(_at + 2).IndexOf("*/"u8); |
| 142 | _at = end < 0 ? data.Length : _at + 2 + end + 2; |
| 143 | } |
| 144 | else |
| 145 | { |
| 146 | break; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | private string Text() |
| 152 | { |
| 153 | var start = ++_at; |
| 154 | var plain = true; |
| 155 | while (_at < data.Length && data[_at] != '"') |
| 156 | { |
| 157 | if (data[_at] == '\\') |
| 158 | { |
| 159 | plain = false; |
| 160 | _at++; |
| 161 | } |
| 162 | |
| 163 | _at++; |
| 164 | } |
| 165 | |
| 166 | if (_at >= data.Length) |
| 167 | throw new FormatException("It ends inside a string."); |
| 168 | var raw = data.AsSpan(start, _at - start); |
| 169 | _at++; |
| 170 | return plain ? Encoding.UTF8.GetString(raw) : Unescape(Encoding.UTF8.GetString(raw)); |
| 171 | } |
| 172 | |
| 173 | private void SkipText() |
| 174 | { |
| 175 | _at++; |
| 176 | while (_at < data.Length && data[_at] != '"') |
| 177 | _at += data[_at] == '\\' ? 2 : 1; |
| 178 | if (_at >= data.Length) |
| 179 | throw new FormatException("It ends inside a string."); |
| 180 | _at++; |
| 181 | } |
| 182 | |
| 183 | private string Bare() |
| 184 | { |
| 185 | var start = _at; |
| 186 | while (_at < data.Length && data[_at] is not ((byte)' ' or (byte)'\t' or (byte)'\r' or (byte)'\n' or (byte)',' |
| 187 | or (byte)':' or (byte)'[' or (byte)']' or (byte)'{' or (byte)'}' or (byte)'"')) |
| 188 | _at++; |
| 189 | if (_at == start) |
| 190 | throw new FormatException($"It has a stray '{(char)data[_at]}' at byte {_at}."); |
| 191 | return Encoding.UTF8.GetString(data.AsSpan(start, _at - start)); |
| 192 | } |
| 193 | |
| 194 | private static JsonNode? Literal(string token) |
| 195 | { |
| 196 | switch (token) |
| 197 | { |
| 198 | case "true": |
| 199 | return JsonValue.Create(true); |
| 200 | case "false": |
| 201 | return JsonValue.Create(false); |
| 202 | case "null": |
| 203 | return null; |
| 204 | } |
| 205 | |
| 206 | if (long.TryParse(token, NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out var whole)) |
| 207 | return JsonValue.Create(whole); |
| 208 | return double.TryParse(token, NumberStyles.Float, CultureInfo.InvariantCulture, out var number) |
| 209 | ? JsonValue.Create(number) |
| 210 | : JsonValue.Create(token); |
| 211 | } |
| 212 | |
| 213 | private static string Unescape(string text) |
| 214 | { |
| 215 | var result = new StringBuilder(text.Length); |
| 216 | for (var i = 0; i < text.Length; i++) |
| 217 | { |
| 218 | if (text[i] != '\\' || i + 1 >= text.Length) |
| 219 | { |
| 220 | result.Append(text[i]); |
| 221 | continue; |
| 222 | } |
| 223 | |
| 224 | var next = text[++i]; |
| 225 | switch (next) |
| 226 | { |
| 227 | case 'n': |
| 228 | result.Append('\n'); |
| 229 | break; |
| 230 | case 't': |
| 231 | result.Append('\t'); |
| 232 | break; |
| 233 | case 'r': |
| 234 | result.Append('\r'); |
| 235 | break; |
| 236 | case 'b': |
| 237 | result.Append('\b'); |
| 238 | break; |
| 239 | case 'f': |
| 240 | result.Append('\f'); |
| 241 | break; |
| 242 | case 'u' when i + 4 < text.Length && |
| 243 | ushort.TryParse(text.AsSpan(i + 1, 4), NumberStyles.HexNumber, |
| 244 | CultureInfo.InvariantCulture, |
| 245 | out var code): |
| 246 | result.Append((char)code); |
| 247 | i += 4; |
| 248 | break; |
| 249 | default: |
| 250 | result.Append(next); |
| 251 | break; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | return result.ToString(); |
| 256 | } |
| 257 | } |