| 1 | /* * * * * |
| 2 | * A simple JSON Parser / builder |
| 3 | * ------------------------------ |
| 4 | * |
| 5 | * It mainly has been written as a simple JSON parser. It can build a JSON string |
| 6 | * from the node-tree, or generate a node tree from any valid JSON string. |
| 7 | * |
| 8 | * If you want to use compression when saving to file / stream / B64 you have to include |
| 9 | * SharpZipLib ( http://www.icsharpcode.net/opensource/sharpziplib/ ) in your project and |
| 10 | * define "USE_SharpZipLib" at the top of the file |
| 11 | * |
| 12 | * Written by Bunny83 |
| 13 | * 2012-06-09 |
| 14 | * |
| 15 | * [2012-06-09 First Version] |
| 16 | * - provides strongly typed node classes and lists / dictionaries |
| 17 | * - provides easy access to class members / array items / data values |
| 18 | * - the parser now properly identifies types. So generating JSON with this framework should work. |
| 19 | * - only double quotes (") are used for quoting strings. |
| 20 | * - provides "casting" properties to easily convert to / from those types: |
| 21 | * int / float / double / bool |
| 22 | * - provides a common interface for each node so no explicit casting is required. |
| 23 | * - the parser tries to avoid errors, but if malformed JSON is parsed the result is more or less undefined |
| 24 | * - It can serialize/deserialize a node tree into/from an experimental compact binary format. It might |
| 25 | * be handy if you want to store things in a file and don't want it to be easily modifiable |
| 26 | * |
| 27 | * [2012-12-17 Update] |
| 28 | * - Added internal JSONLazyCreator class which simplifies the construction of a JSON tree |
| 29 | * Now you can simple reference any item that doesn't exist yet and it will return a JSONLazyCreator |
| 30 | * The class determines the required type by it's further use, creates the type and removes itself. |
| 31 | * - Added binary serialization / deserialization. |
| 32 | * - Added support for BZip2 zipped binary format. Requires the SharpZipLib ( http://www.icsharpcode.net/opensource/sharpziplib/ ) |
| 33 | * The usage of the SharpZipLib library can be disabled by removing or commenting out the USE_SharpZipLib define at the top |
| 34 | * - The serializer uses different types when it comes to store the values. Since my data values |
| 35 | * are all of type string, the serializer will "try" which format fits best. The order is: int, float, double, bool, string. |
| 36 | * It's not the most efficient way but for a moderate amount of data it should work on all platforms. |
| 37 | * |
| 38 | * [2017-03-08 Update] |
| 39 | * - Optimised parsing by using a StringBuilder for token. This prevents performance issues when large |
| 40 | * string data fields are contained in the json data. |
| 41 | * - Finally refactored the badly named JSONClass into JSONObject. |
| 42 | * - Replaced the old JSONData class by distict typed classes ( JSONString, JSONNumber, JSONBool, JSONNull ) this |
| 43 | * allows to propertly convert the node tree back to json without type information loss. The actual value |
| 44 | * parsing now happens at parsing time and not when you actually access one of the casting properties. |
| 45 | * |
| 46 | * [2017-04-11 Update] |
| 47 | * - Fixed parsing bug where empty string values have been ignored. |
| 48 | * - Optimised "ToString" by using a StringBuilder internally. This should heavily improve performance for large files |
| 49 | * - Changed the overload of "ToString(string aIndent)" to "ToString(int aIndent)" |
| 50 | * |
| 51 | * [2017-11-29 Update] |
| 52 | * - Removed the IEnumerator implementations on JSONArray & JSONObject and replaced it with a common |
| 53 | * struct Enumerator in JSONNode that should avoid garbage generation. The enumerator always works |
| 54 | * on KeyValuePair<string, JSONNode>, even for JSONArray. |
| 55 | * - Added two wrapper Enumerators that allows for easy key or value enumeration. A JSONNode now has |
| 56 | * a "Keys" and a "Values" enumerable property. Those are also struct enumerators / enumerables |
| 57 | * - A KeyValuePair<string, JSONNode> can now be implicitly converted into a JSONNode. This allows |
| 58 | * a foreach loop over a JSONNode to directly access the values only. Since KeyValuePair as well as |
| 59 | * all the Enumerators are structs, no garbage is allocated. |
| 60 | * - To add Linq support another "LinqEnumerator" is available through the "Linq" property. This |
| 61 | * enumerator does implement the generic IEnumerable interface so most Linq extensions can be used |
| 62 | * on this enumerable object. This one does allocate memory as it's a wrapper class. |
| 63 | * - The Escape method now escapes all control characters (# < 32) in strings as uncode characters |
| 64 | * (\uXXXX) and if the static bool JSONNode.forceASCII is set to true it will also escape all |
| 65 | * characters # > 127. This might be useful if you require an ASCII output. Though keep in mind |
| 66 | * when your strings contain many non-ascii characters the strings become much longer (x6) and are |
| 67 | * no longer human readable. |
| 68 | * - The node types JSONObject and JSONArray now have an "Inline" boolean switch which will default to |
| 69 | * false. It can be used to serialize this element inline even you serialize with an indented format |
| 70 | * This is useful for arrays containing numbers so it doesn't place every number on a new line |
| 71 | * - Extracted the binary serialization code into a seperate extension file. All classes are now declared |
| 72 | * as "partial" so an extension file can even add a new virtual or abstract method / interface to |
| 73 | * JSONNode and override it in the concrete type classes. It's of course a hacky approach which is |
| 74 | * generally not recommended, but i wanted to keep everything tightly packed. |
| 75 | * - Added a static CreateOrGet method to the JSONNull class. Since this class is immutable it could |
| 76 | * be reused without major problems. If you have a lot null fields in your data it will help reduce |
| 77 | * the memory / garbage overhead. I also added a static setting (reuseSameInstance) to JSONNull |
| 78 | * (default is true) which will change the behaviour of "CreateOrGet". If you set this to false |
| 79 | * CreateOrGet will not reuse the cached instance but instead create a new JSONNull instance each time. |
| 80 | * I made the JSONNull constructor private so if you need to create an instance manually use |
| 81 | * JSONNull.CreateOrGet() |
| 82 | * |
| 83 | * [2018-01-09 Update] |
| 84 | * - Changed all double.TryParse and double.ToString uses to use the invariant culture to avoid problems |
| 85 | * on systems with a culture that uses a comma as decimal point. |
| 86 | * |
| 87 | * [2018-01-26 Update] |
| 88 | * - Added AsLong. Note that a JSONNumber is stored as double and can't represent all long values. However |
| 89 | * storing it as string would work. |
| 90 | * - Added static setting "JSONNode.longAsString" which controls the default type that is used by the |
| 91 | * LazyCreator when using AsLong |
| 92 | * |
| 93 | * [2018-04-25 Update] |
| 94 | * - Added support for parsing single values (JSONBool, JSONString, JSONNumber, JSONNull) as top level value. |
| 95 | * |
| 96 | * The MIT License (MIT) |
| 97 | * |
| 98 | * Copyright (c) 2012-2017 Markus Göbel (Bunny83) |
| 99 | * |
| 100 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 101 | * of this software and associated documentation files (the "Software"), to deal |
| 102 | * in the Software without restriction, including without limitation the rights |
| 103 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 104 | * copies of the Software, and to permit persons to whom the Software is |
| 105 | * furnished to do so, subject to the following conditions: |
| 106 | * |
| 107 | * The above copyright notice and this permission notice shall be included in all |
| 108 | * copies or substantial portions of the Software. |
| 109 | * |
| 110 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 111 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 112 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 113 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 114 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 115 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 116 | * SOFTWARE. |
| 117 | * |
| 118 | * * * * */ |
| 119 | using System; |
| 120 | using System.Collections; |
| 121 | using System.Collections.Generic; |
| 122 | using System.Globalization; |
| 123 | using System.Linq; |
| 124 | using System.Text; |
| 125 | |
| 126 | // renaming the namespace to avoid conflict |
| 127 | namespace OVRSimpleJSON // SimpleJSON |
| 128 | { |
| 129 | public enum JSONNodeType |
| 130 | { |
| 131 | Array = 1, |
| 132 | Object = 2, |
| 133 | String = 3, |
| 134 | Number = 4, |
| 135 | NullValue = 5, |
| 136 | Boolean = 6, |
| 137 | None = 7, |
| 138 | Custom = 0xFF, |
| 139 | } |
| 140 | public enum JSONTextMode |
| 141 | { |
| 142 | Compact, |
| 143 | Indent |
| 144 | } |
| 145 | |
| 146 | public abstract partial class JSONNode |
| 147 | { |
| 148 | #region Enumerators |
| 149 | public struct Enumerator |
| 150 | { |
| 151 | private enum Type { None, Array, Object } |
| 152 | private Type type; |
| 153 | private Dictionary<string, JSONNode>.Enumerator m_Object; |
| 154 | private List<JSONNode>.Enumerator m_Array; |
| 155 | public bool IsValid { get { return type != Type.None; } } |
| 156 | public Enumerator(List<JSONNode>.Enumerator aArrayEnum) |
| 157 | { |
| 158 | type = Type.Array; |
| 159 | m_Object = default(Dictionary<string, JSONNode>.Enumerator); |
| 160 | m_Array = aArrayEnum; |
| 161 | } |
| 162 | public Enumerator(Dictionary<string, JSONNode>.Enumerator aDictEnum) |
| 163 | { |
| 164 | type = Type.Object; |
| 165 | m_Object = aDictEnum; |
| 166 | m_Array = default(List<JSONNode>.Enumerator); |
| 167 | } |
| 168 | public KeyValuePair<string, JSONNode> Current |
| 169 | { |
| 170 | get { |
| 171 | if (type == Type.Array) |
| 172 | return new KeyValuePair<string, JSONNode>(string.Empty, m_Array.Current); |
| 173 | else if (type == Type.Object) |
| 174 | return m_Object.Current; |
| 175 | return new KeyValuePair<string, JSONNode>(string.Empty, null); |
| 176 | } |
| 177 | } |
| 178 | public bool MoveNext() |
| 179 | { |
| 180 | if (type == Type.Array) |
| 181 | return m_Array.MoveNext(); |
| 182 | else if (type == Type.Object) |
| 183 | return m_Object.MoveNext(); |
| 184 | return false; |
| 185 | } |
| 186 | } |
| 187 | public struct ValueEnumerator |
| 188 | { |
| 189 | private Enumerator m_Enumerator; |
| 190 | public ValueEnumerator(List<JSONNode>.Enumerator aArrayEnum) : this(new Enumerator(aArrayEnum)) { } |
| 191 | public ValueEnumerator(Dictionary<string, JSONNode>.Enumerator aDictEnum) : this(new Enumerator(aDictEnum)) { } |
| 192 | public ValueEnumerator(Enumerator aEnumerator) { m_Enumerator = aEnumerator; } |
| 193 | public JSONNode Current { get { return m_Enumerator.Current.Value; } } |
| 194 | public bool MoveNext() { return m_Enumerator.MoveNext(); } |
| 195 | public ValueEnumerator GetEnumerator() { return this; } |
| 196 | } |
| 197 | public struct KeyEnumerator |
| 198 | { |
| 199 | private Enumerator m_Enumerator; |
| 200 | public KeyEnumerator(List<JSONNode>.Enumerator aArrayEnum) : this(new Enumerator(aArrayEnum)) { } |
| 201 | public KeyEnumerator(Dictionary<string, JSONNode>.Enumerator aDictEnum) : this(new Enumerator(aDictEnum)) { } |
| 202 | public KeyEnumerator(Enumerator aEnumerator) { m_Enumerator = aEnumerator; } |
| 203 | public string Current { get { return m_Enumerator.Current.Key; } } |
| 204 | public bool MoveNext() { return m_Enumerator.MoveNext(); } |
| 205 | public KeyEnumerator GetEnumerator() { return this; } |
| 206 | } |
| 207 | |
| 208 | public class LinqEnumerator : IEnumerator<KeyValuePair<string, JSONNode>>, IEnumerable<KeyValuePair<string, JSONNode>> |
| 209 | { |
| 210 | private JSONNode m_Node; |
| 211 | private Enumerator m_Enumerator; |
| 212 | internal LinqEnumerator(JSONNode aNode) |
| 213 | { |
| 214 | m_Node = aNode; |
| 215 | if (m_Node != null) |
| 216 | m_Enumerator = m_Node.GetEnumerator(); |
| 217 | } |
| 218 | public KeyValuePair<string, JSONNode> Current { get { return m_Enumerator.Current; } } |
| 219 | object IEnumerator.Current { get { return m_Enumerator.Current; } } |
| 220 | public bool MoveNext() { return m_Enumerator.MoveNext(); } |
| 221 | |
| 222 | public void Dispose() |
| 223 | { |
| 224 | m_Node = null; |
| 225 | m_Enumerator = new Enumerator(); |
| 226 | } |
| 227 | |
| 228 | public IEnumerator<KeyValuePair<string, JSONNode>> GetEnumerator() |
| 229 | { |
| 230 | return new LinqEnumerator(m_Node); |
| 231 | } |
| 232 | |
| 233 | public void Reset() |
| 234 | { |
| 235 | if (m_Node != null) |
| 236 | m_Enumerator = m_Node.GetEnumerator(); |
| 237 | } |
| 238 | |
| 239 | IEnumerator IEnumerable.GetEnumerator() |
| 240 | { |
| 241 | return new LinqEnumerator(m_Node); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | #endregion Enumerators |
| 246 | |
| 247 | #region common interface |
| 248 | |
| 249 | public static bool forceASCII = false; // Use Unicode by default |
| 250 | public static bool longAsString = false; // lazy creator creates a JSONString instead of JSONNumber |
| 251 | |
| 252 | public abstract JSONNodeType Tag { get; } |
| 253 | |
| 254 | public virtual JSONNode this[int aIndex] { get { return null; } set { } } |
| 255 | |
| 256 | public virtual JSONNode this[string aKey] { get { return null; } set { } } |
| 257 | |
| 258 | public virtual string Value { get { return ""; } set { } } |
| 259 | |
| 260 | public virtual int Count { get { return 0; } } |
| 261 | |
| 262 | public virtual bool IsNumber { get { return false; } } |
| 263 | public virtual bool IsString { get { return false; } } |
| 264 | public virtual bool IsBoolean { get { return false; } } |
| 265 | public virtual bool IsNull { get { return false; } } |
| 266 | public virtual bool IsArray { get { return false; } } |
| 267 | public virtual bool IsObject { get { return false; } } |
| 268 | |
| 269 | public virtual bool Inline { get { return false; } set { } } |
| 270 | |
| 271 | public virtual void Add(string aKey, JSONNode aItem) |
| 272 | { |
| 273 | } |
| 274 | public virtual void Add(JSONNode aItem) |
| 275 | { |
| 276 | Add("", aItem); |
| 277 | } |
| 278 | |
| 279 | public virtual JSONNode Remove(string aKey) |
| 280 | { |
| 281 | return null; |
| 282 | } |
| 283 | |
| 284 | public virtual JSONNode Remove(int aIndex) |
| 285 | { |
| 286 | return null; |
| 287 | } |
| 288 | |
| 289 | public virtual JSONNode Remove(JSONNode aNode) |
| 290 | { |
| 291 | return aNode; |
| 292 | } |
| 293 | |
| 294 | public virtual IEnumerable<JSONNode> Children |
| 295 | { |
| 296 | get |
| 297 | { |
| 298 | yield break; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | public IEnumerable<JSONNode> DeepChildren |
| 303 | { |
| 304 | get |
| 305 | { |
| 306 | foreach (var C in Children) |
| 307 | foreach (var D in C.DeepChildren) |
| 308 | yield return D; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | public override string ToString() |
| 313 | { |
| 314 | StringBuilder sb = new StringBuilder(); |
| 315 | WriteToStringBuilder(sb, 0, 0, JSONTextMode.Compact); |
| 316 | return sb.ToString(); |
| 317 | } |
| 318 | |
| 319 | public virtual string ToString(int aIndent) |
| 320 | { |
| 321 | StringBuilder sb = new StringBuilder(); |
| 322 | WriteToStringBuilder(sb, 0, aIndent, JSONTextMode.Indent); |
| 323 | return sb.ToString(); |
| 324 | } |
| 325 | internal abstract void WriteToStringBuilder(StringBuilder aSB, int aIndent, int aIndentInc, JSONTextMode aMode); |
| 326 | |
| 327 | public abstract Enumerator GetEnumerator(); |
| 328 | public IEnumerable<KeyValuePair<string, JSONNode>> Linq { get { return new LinqEnumerator(this); } } |
| 329 | public KeyEnumerator Keys { get { return new KeyEnumerator(GetEnumerator()); } } |
| 330 | public ValueEnumerator Values { get { return new ValueEnumerator(GetEnumerator()); } } |
| 331 | |
| 332 | #endregion common interface |
| 333 | |
| 334 | #region typecasting properties |
| 335 | |
| 336 | |
| 337 | public virtual double AsDouble |
| 338 | { |
| 339 | get |
| 340 | { |
| 341 | double v = 0.0; |
| 342 | if (double.TryParse(Value,NumberStyles.Float, CultureInfo.InvariantCulture, out v)) |
| 343 | return v; |
| 344 | return 0.0; |
| 345 | } |
| 346 | set |
| 347 | { |
| 348 | Value = value.ToString(CultureInfo.InvariantCulture); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | public virtual int AsInt |
| 353 | { |
| 354 | get { return (int)AsDouble; } |
| 355 | set { AsDouble = value; } |
| 356 | } |
| 357 | |
| 358 | public virtual float AsFloat |
| 359 | { |
| 360 | get { return (float)AsDouble; } |
| 361 | set { AsDouble = value; } |
| 362 | } |
| 363 | |
| 364 | public virtual bool AsBool |
| 365 | { |
| 366 | get |
| 367 | { |
| 368 | bool v = false; |
| 369 | if (bool.TryParse(Value, out v)) |
| 370 | return v; |
| 371 | return !string.IsNullOrEmpty(Value); |
| 372 | } |
| 373 | set |
| 374 | { |
| 375 | Value = (value) ? "true" : "false"; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | public virtual long AsLong |
| 380 | { |
| 381 | get |
| 382 | { |
| 383 | long val = 0; |
| 384 | if (long.TryParse(Value, out val)) |
| 385 | return val; |
| 386 | return 0L; |
| 387 | } |
| 388 | set |
| 389 | { |
| 390 | Value = value.ToString(); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | public virtual JSONArray AsArray |
| 395 | { |
| 396 | get |
| 397 | { |
| 398 | return this as JSONArray; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | public virtual JSONObject AsObject |
| 403 | { |
| 404 | get |
| 405 | { |
| 406 | return this as JSONObject; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | |
| 411 | #endregion typecasting properties |
| 412 | |
| 413 | #region operators |
| 414 | |
| 415 | public static implicit operator JSONNode(string s) |
| 416 | { |
| 417 | return new JSONString(s); |
| 418 | } |
| 419 | public static implicit operator string(JSONNode d) |
| 420 | { |
| 421 | return (d == null) ? null : d.Value; |
| 422 | } |
| 423 | |
| 424 | public static implicit operator JSONNode(double n) |
| 425 | { |
| 426 | return new JSONNumber(n); |
| 427 | } |
| 428 | public static implicit operator double(JSONNode d) |
| 429 | { |
| 430 | return (d == null) ? 0 : d.AsDouble; |
| 431 | } |
| 432 | |
| 433 | public static implicit operator JSONNode(float n) |
| 434 | { |
| 435 | return new JSONNumber(n); |
| 436 | } |
| 437 | public static implicit operator float(JSONNode d) |
| 438 | { |
| 439 | return (d == null) ? 0 : d.AsFloat; |
| 440 | } |
| 441 | |
| 442 | public static implicit operator JSONNode(int n) |
| 443 | { |
| 444 | return new JSONNumber(n); |
| 445 | } |
| 446 | public static implicit operator int(JSONNode d) |
| 447 | { |
| 448 | return (d == null) ? 0 : d.AsInt; |
| 449 | } |
| 450 | |
| 451 | public static implicit operator JSONNode(long n) |
| 452 | { |
| 453 | if (longAsString) |
| 454 | return new JSONString(n.ToString()); |
| 455 | return new JSONNumber(n); |
| 456 | } |
| 457 | public static implicit operator long(JSONNode d) |
| 458 | { |
| 459 | return (d == null) ? 0L : d.AsLong; |
| 460 | } |
| 461 | |
| 462 | public static implicit operator JSONNode(bool b) |
| 463 | { |
| 464 | return new JSONBool(b); |
| 465 | } |
| 466 | public static implicit operator bool(JSONNode d) |
| 467 | { |
| 468 | return (d == null) ? false : d.AsBool; |
| 469 | } |
| 470 | |
| 471 | public static implicit operator JSONNode(KeyValuePair<string, JSONNode> aKeyValue) |
| 472 | { |
| 473 | return aKeyValue.Value; |
| 474 | } |
| 475 | |
| 476 | public static bool operator ==(JSONNode a, object b) |
| 477 | { |
| 478 | if (ReferenceEquals(a, b)) |
| 479 | return true; |
| 480 | bool aIsNull = a is JSONNull || ReferenceEquals(a, null) || a is JSONLazyCreator; |
| 481 | bool bIsNull = b is JSONNull || ReferenceEquals(b, null) || b is JSONLazyCreator; |
| 482 | if (aIsNull && bIsNull) |
| 483 | return true; |
| 484 | return !aIsNull && a.Equals(b); |
| 485 | } |
| 486 | |
| 487 | public static bool operator !=(JSONNode a, object b) |
| 488 | { |
| 489 | return !(a == b); |
| 490 | } |
| 491 | |
| 492 | public override bool Equals(object obj) |
| 493 | { |
| 494 | return ReferenceEquals(this, obj); |
| 495 | } |
| 496 | |
| 497 | public override int GetHashCode() |
| 498 | { |
| 499 | return base.GetHashCode(); |
| 500 | } |
| 501 | |
| 502 | #endregion operators |
| 503 | |
| 504 | [ThreadStatic] |
| 505 | private static StringBuilder m_EscapeBuilder; |
| 506 | internal static StringBuilder EscapeBuilder |
| 507 | { |
| 508 | get { |
| 509 | if (m_EscapeBuilder == null) |
| 510 | m_EscapeBuilder = new StringBuilder(); |
| 511 | return m_EscapeBuilder; |
| 512 | } |
| 513 | } |
| 514 | internal static string Escape(string aText) |
| 515 | { |
| 516 | var sb = EscapeBuilder; |
| 517 | sb.Length = 0; |
| 518 | if (sb.Capacity < aText.Length + aText.Length / 10) |
| 519 | sb.Capacity = aText.Length + aText.Length / 10; |
| 520 | foreach (char c in aText) |
| 521 | { |
| 522 | switch (c) |
| 523 | { |
| 524 | case '\\': |
| 525 | sb.Append("\\\\"); |
| 526 | break; |
| 527 | case '\"': |
| 528 | sb.Append("\\\""); |
| 529 | break; |
| 530 | case '\n': |
| 531 | sb.Append("\\n"); |
| 532 | break; |
| 533 | case '\r': |
| 534 | sb.Append("\\r"); |
| 535 | break; |
| 536 | case '\t': |
| 537 | sb.Append("\\t"); |
| 538 | break; |
| 539 | case '\b': |
| 540 | sb.Append("\\b"); |
| 541 | break; |
| 542 | case '\f': |
| 543 | sb.Append("\\f"); |
| 544 | break; |
| 545 | default: |
| 546 | if (c < ' ' || (forceASCII && c > 127)) |
| 547 | { |
| 548 | ushort val = c; |
| 549 | sb.Append("\\u").Append(val.ToString("X4")); |
| 550 | } |
| 551 | else |
| 552 | sb.Append(c); |
| 553 | break; |
| 554 | } |
| 555 | } |
| 556 | string result = sb.ToString(); |
| 557 | sb.Length = 0; |
| 558 | return result; |
| 559 | } |
| 560 | |
| 561 | private static JSONNode ParseElement(string token, bool quoted) |
| 562 | { |
| 563 | if (quoted) |
| 564 | return token; |
| 565 | string tmp = token.ToLower(); |
| 566 | if (tmp == "false" || tmp == "true") |
| 567 | return tmp == "true"; |
| 568 | if (tmp == "null") |
| 569 | return JSONNull.CreateOrGet(); |
| 570 | double val; |
| 571 | if (double.TryParse(token, NumberStyles.Float, CultureInfo.InvariantCulture, out val)) |
| 572 | return val; |
| 573 | else |
| 574 | return token; |
| 575 | } |
| 576 | |
| 577 | public static JSONNode Parse(string aJSON) |
| 578 | { |
| 579 | Stack<JSONNode> stack = new Stack<JSONNode>(); |
| 580 | JSONNode ctx = null; |
| 581 | int i = 0; |
| 582 | StringBuilder Token = new StringBuilder(); |
| 583 | string TokenName = ""; |
| 584 | bool QuoteMode = false; |
| 585 | bool TokenIsQuoted = false; |
| 586 | while (i < aJSON.Length) |
| 587 | { |
| 588 | switch (aJSON[i]) |
| 589 | { |
| 590 | case '{': |
| 591 | if (QuoteMode) |
| 592 | { |
| 593 | Token.Append(aJSON[i]); |
| 594 | break; |
| 595 | } |
| 596 | stack.Push(new JSONObject()); |
| 597 | if (ctx != null) |
| 598 | { |
| 599 | ctx.Add(TokenName, stack.Peek()); |
| 600 | } |
| 601 | TokenName = ""; |
| 602 | Token.Length = 0; |
| 603 | ctx = stack.Peek(); |
| 604 | break; |
| 605 | |
| 606 | case '[': |
| 607 | if (QuoteMode) |
| 608 | { |
| 609 | Token.Append(aJSON[i]); |
| 610 | break; |
| 611 | } |
| 612 | |
| 613 | stack.Push(new JSONArray()); |
| 614 | if (ctx != null) |
| 615 | { |
| 616 | ctx.Add(TokenName, stack.Peek()); |
| 617 | } |
| 618 | TokenName = ""; |
| 619 | Token.Length = 0; |
| 620 | ctx = stack.Peek(); |
| 621 | break; |
| 622 | |
| 623 | case '}': |
| 624 | case ']': |
| 625 | if (QuoteMode) |
| 626 | { |
| 627 | |
| 628 | Token.Append(aJSON[i]); |
| 629 | break; |
| 630 | } |
| 631 | if (stack.Count == 0) |
| 632 | throw new Exception("JSON Parse: Too many closing brackets"); |
| 633 | |
| 634 | stack.Pop(); |
| 635 | if (Token.Length > 0 || TokenIsQuoted) |
| 636 | ctx.Add(TokenName, ParseElement(Token.ToString(), TokenIsQuoted)); |
| 637 | TokenIsQuoted = false; |
| 638 | TokenName = ""; |
| 639 | Token.Length = 0; |
| 640 | if (stack.Count > 0) |
| 641 | ctx = stack.Peek(); |
| 642 | break; |
| 643 | |
| 644 | case ':': |
| 645 | if (QuoteMode) |
| 646 | { |
| 647 | Token.Append(aJSON[i]); |
| 648 | break; |
| 649 | } |
| 650 | TokenName = Token.ToString(); |
| 651 | Token.Length = 0; |
| 652 | TokenIsQuoted = false; |
| 653 | break; |
| 654 | |
| 655 | case '"': |
| 656 | QuoteMode ^= true; |
| 657 | TokenIsQuoted |= QuoteMode; |
| 658 | break; |
| 659 | |
| 660 | case ',': |
| 661 | if (QuoteMode) |
| 662 | { |
| 663 | Token.Append(aJSON[i]); |
| 664 | break; |
| 665 | } |
| 666 | if (Token.Length > 0 || TokenIsQuoted) |
| 667 | ctx.Add(TokenName, ParseElement(Token.ToString(), TokenIsQuoted)); |
| 668 | TokenIsQuoted = false; |
| 669 | TokenName = ""; |
| 670 | Token.Length = 0; |
| 671 | TokenIsQuoted = false; |
| 672 | break; |
| 673 | |
| 674 | case '\r': |
| 675 | case '\n': |
| 676 | break; |
| 677 | |
| 678 | case ' ': |
| 679 | case '\t': |
| 680 | if (QuoteMode) |
| 681 | Token.Append(aJSON[i]); |
| 682 | break; |
| 683 | |
| 684 | case '\\': |
| 685 | ++i; |
| 686 | if (QuoteMode) |
| 687 | { |
| 688 | char C = aJSON[i]; |
| 689 | switch (C) |
| 690 | { |
| 691 | case 't': |
| 692 | Token.Append('\t'); |
| 693 | break; |
| 694 | case 'r': |
| 695 | Token.Append('\r'); |
| 696 | break; |
| 697 | case 'n': |
| 698 | Token.Append('\n'); |
| 699 | break; |
| 700 | case 'b': |
| 701 | Token.Append('\b'); |
| 702 | break; |
| 703 | case 'f': |
| 704 | Token.Append('\f'); |
| 705 | break; |
| 706 | case 'u': |
| 707 | { |
| 708 | string s = aJSON.Substring(i + 1, 4); |
| 709 | Token.Append((char)int.Parse( |
| 710 | s, |
| 711 | System.Globalization.NumberStyles.AllowHexSpecifier)); |
| 712 | i += 4; |
| 713 | break; |
| 714 | } |
| 715 | default: |
| 716 | Token.Append(C); |
| 717 | break; |
| 718 | } |
| 719 | } |
| 720 | break; |
| 721 | |
| 722 | default: |
| 723 | Token.Append(aJSON[i]); |
| 724 | break; |
| 725 | } |
| 726 | ++i; |
| 727 | } |
| 728 | if (QuoteMode) |
| 729 | { |
| 730 | throw new Exception("JSON Parse: Quotation marks seems to be messed up."); |
| 731 | } |
| 732 | if (ctx == null) |
| 733 | return ParseElement(Token.ToString(), TokenIsQuoted); |
| 734 | return ctx; |
| 735 | } |
| 736 | |
| 737 | } |
| 738 | // End of JSONNode |
| 739 | |
| 740 | public partial class JSONArray : JSONNode |
| 741 | { |
| 742 | private List<JSONNode> m_List = new List<JSONNode>(); |
| 743 | private bool inline = false; |
| 744 | public override bool Inline |
| 745 | { |
| 746 | get { return inline; } |
| 747 | set { inline = value; } |
| 748 | } |
| 749 | |
| 750 | public override JSONNodeType Tag { get { return JSONNodeType.Array; } } |
| 751 | public override bool IsArray { get { return true; } } |
| 752 | public override Enumerator GetEnumerator() { return new Enumerator(m_List.GetEnumerator()); } |
| 753 | |
| 754 | public override JSONNode this[int aIndex] |
| 755 | { |
| 756 | get |
| 757 | { |
| 758 | if (aIndex < 0 || aIndex >= m_List.Count) |
| 759 | return new JSONLazyCreator(this); |
| 760 | return m_List[aIndex]; |
| 761 | } |
| 762 | set |
| 763 | { |
| 764 | if (value == null) |
| 765 | value = JSONNull.CreateOrGet(); |
| 766 | if (aIndex < 0 || aIndex >= m_List.Count) |
| 767 | m_List.Add(value); |
| 768 | else |
| 769 | m_List[aIndex] = value; |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | public override JSONNode this[string aKey] |
| 774 | { |
| 775 | get { return new JSONLazyCreator(this); } |
| 776 | set |
| 777 | { |
| 778 | if (value == null) |
| 779 | value = JSONNull.CreateOrGet(); |
| 780 | m_List.Add(value); |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | public override int Count |
| 785 | { |
| 786 | get { return m_List.Count; } |
| 787 | } |
| 788 | |
| 789 | public override void Add(string aKey, JSONNode aItem) |
| 790 | { |
| 791 | if (aItem == null) |
| 792 | aItem = JSONNull.CreateOrGet(); |
| 793 | m_List.Add(aItem); |
| 794 | } |
| 795 | |
| 796 | public override JSONNode Remove(int aIndex) |
| 797 | { |
| 798 | if (aIndex < 0 || aIndex >= m_List.Count) |
| 799 | return null; |
| 800 | JSONNode tmp = m_List[aIndex]; |
| 801 | m_List.RemoveAt(aIndex); |
| 802 | return tmp; |
| 803 | } |
| 804 | |
| 805 | public override JSONNode Remove(JSONNode aNode) |
| 806 | { |
| 807 | m_List.Remove(aNode); |
| 808 | return aNode; |
| 809 | } |
| 810 | |
| 811 | public override IEnumerable<JSONNode> Children |
| 812 | { |
| 813 | get |
| 814 | { |
| 815 | foreach (JSONNode N in m_List) |
| 816 | yield return N; |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | |
| 821 | internal override void WriteToStringBuilder(StringBuilder aSB, int aIndent, int aIndentInc, JSONTextMode aMode) |
| 822 | { |
| 823 | aSB.Append('['); |
| 824 | int count = m_List.Count; |
| 825 | if (inline) |
| 826 | aMode = JSONTextMode.Compact; |
| 827 | for (int i = 0; i < count; i++) |
| 828 | { |
| 829 | if (i > 0) |
| 830 | aSB.Append(','); |
| 831 | if (aMode == JSONTextMode.Indent) |
| 832 | aSB.AppendLine(); |
| 833 | |
| 834 | if (aMode == JSONTextMode.Indent) |
| 835 | aSB.Append(' ', aIndent + aIndentInc); |
| 836 | m_List[i].WriteToStringBuilder(aSB, aIndent + aIndentInc, aIndentInc, aMode); |
| 837 | } |
| 838 | if (aMode == JSONTextMode.Indent) |
| 839 | aSB.AppendLine().Append(' ', aIndent); |
| 840 | aSB.Append(']'); |
| 841 | } |
| 842 | } |
| 843 | // End of JSONArray |
| 844 | |
| 845 | public partial class JSONObject : JSONNode |
| 846 | { |
| 847 | private Dictionary<string, JSONNode> m_Dict = new Dictionary<string, JSONNode>(); |
| 848 | |
| 849 | private bool inline = false; |
| 850 | public override bool Inline |
| 851 | { |
| 852 | get { return inline; } |
| 853 | set { inline = value; } |
| 854 | } |
| 855 | |
| 856 | public override JSONNodeType Tag { get { return JSONNodeType.Object; } } |
| 857 | public override bool IsObject { get { return true; } } |
| 858 | |
| 859 | public override Enumerator GetEnumerator() { return new Enumerator(m_Dict.GetEnumerator()); } |
| 860 | |
| 861 | |
| 862 | public override JSONNode this[string aKey] |
| 863 | { |
| 864 | get |
| 865 | { |
| 866 | if (m_Dict.ContainsKey(aKey)) |
| 867 | return m_Dict[aKey]; |
| 868 | else |
| 869 | return new JSONLazyCreator(this, aKey); |
| 870 | } |
| 871 | set |
| 872 | { |
| 873 | if (value == null) |
| 874 | value = JSONNull.CreateOrGet(); |
| 875 | if (m_Dict.ContainsKey(aKey)) |
| 876 | m_Dict[aKey] = value; |
| 877 | else |
| 878 | m_Dict.Add(aKey, value); |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | public override JSONNode this[int aIndex] |
| 883 | { |
| 884 | get |
| 885 | { |
| 886 | if (aIndex < 0 || aIndex >= m_Dict.Count) |
| 887 | return null; |
| 888 | return m_Dict.ElementAt(aIndex).Value; |
| 889 | } |
| 890 | set |
| 891 | { |
| 892 | if (value == null) |
| 893 | value = JSONNull.CreateOrGet(); |
| 894 | if (aIndex < 0 || aIndex >= m_Dict.Count) |
| 895 | return; |
| 896 | string key = m_Dict.ElementAt(aIndex).Key; |
| 897 | m_Dict[key] = value; |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | public override int Count |
| 902 | { |
| 903 | get { return m_Dict.Count; } |
| 904 | } |
| 905 | |
| 906 | public override void Add(string aKey, JSONNode aItem) |
| 907 | { |
| 908 | if (aItem == null) |
| 909 | aItem = JSONNull.CreateOrGet(); |
| 910 | |
| 911 | if (!string.IsNullOrEmpty(aKey)) |
| 912 | { |
| 913 | if (m_Dict.ContainsKey(aKey)) |
| 914 | m_Dict[aKey] = aItem; |
| 915 | else |
| 916 | m_Dict.Add(aKey, aItem); |
| 917 | } |
| 918 | else |
| 919 | m_Dict.Add(Guid.NewGuid().ToString(), aItem); |
| 920 | } |
| 921 | |
| 922 | public override JSONNode Remove(string aKey) |
| 923 | { |
| 924 | if (!m_Dict.ContainsKey(aKey)) |
| 925 | return null; |
| 926 | JSONNode tmp = m_Dict[aKey]; |
| 927 | m_Dict.Remove(aKey); |
| 928 | return tmp; |
| 929 | } |
| 930 | |
| 931 | public override JSONNode Remove(int aIndex) |
| 932 | { |
| 933 | if (aIndex < 0 || aIndex >= m_Dict.Count) |
| 934 | return null; |
| 935 | var item = m_Dict.ElementAt(aIndex); |
| 936 | m_Dict.Remove(item.Key); |
| 937 | return item.Value; |
| 938 | } |
| 939 | |
| 940 | public override JSONNode Remove(JSONNode aNode) |
| 941 | { |
| 942 | try |
| 943 | { |
| 944 | var item = m_Dict.Where(k => k.Value == aNode).First(); |
| 945 | m_Dict.Remove(item.Key); |
| 946 | return aNode; |
| 947 | } |
| 948 | catch |
| 949 | { |
| 950 | return null; |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | public override IEnumerable<JSONNode> Children |
| 955 | { |
| 956 | get |
| 957 | { |
| 958 | foreach (KeyValuePair<string, JSONNode> N in m_Dict) |
| 959 | yield return N.Value; |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | internal override void WriteToStringBuilder(StringBuilder aSB, int aIndent, int aIndentInc, JSONTextMode aMode) |
| 964 | { |
| 965 | aSB.Append('{'); |
| 966 | bool first = true; |
| 967 | if (inline) |
| 968 | aMode = JSONTextMode.Compact; |
| 969 | foreach (var k in m_Dict) |
| 970 | { |
| 971 | if (!first) |
| 972 | aSB.Append(','); |
| 973 | first = false; |
| 974 | if (aMode == JSONTextMode.Indent) |
| 975 | aSB.AppendLine(); |
| 976 | if (aMode == JSONTextMode.Indent) |
| 977 | aSB.Append(' ', aIndent + aIndentInc); |
| 978 | aSB.Append('\"').Append(Escape(k.Key)).Append('\"'); |
| 979 | if (aMode == JSONTextMode.Compact) |
| 980 | aSB.Append(':'); |
| 981 | else |
| 982 | aSB.Append(" : "); |
| 983 | k.Value.WriteToStringBuilder(aSB, aIndent + aIndentInc, aIndentInc, aMode); |
| 984 | } |
| 985 | if (aMode == JSONTextMode.Indent) |
| 986 | aSB.AppendLine().Append(' ', aIndent); |
| 987 | aSB.Append('}'); |
| 988 | } |
| 989 | |
| 990 | } |
| 991 | // End of JSONObject |
| 992 | |
| 993 | public partial class JSONString : JSONNode |
| 994 | { |
| 995 | private string m_Data; |
| 996 | |
| 997 | public override JSONNodeType Tag { get { return JSONNodeType.String; } } |
| 998 | public override bool IsString { get { return true; } } |
| 999 | |
| 1000 | public override Enumerator GetEnumerator() { return new Enumerator(); } |
| 1001 | |
| 1002 | |
| 1003 | public override string Value |
| 1004 | { |
| 1005 | get { return m_Data; } |
| 1006 | set |
| 1007 | { |
| 1008 | m_Data = value; |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | public JSONString(string aData) |
| 1013 | { |
| 1014 | m_Data = aData; |
| 1015 | } |
| 1016 | |
| 1017 | internal override void WriteToStringBuilder(StringBuilder aSB, int aIndent, int aIndentInc, JSONTextMode aMode) |
| 1018 | { |
| 1019 | aSB.Append('\"').Append(Escape(m_Data)).Append('\"'); |
| 1020 | } |
| 1021 | public override bool Equals(object obj) |
| 1022 | { |
| 1023 | if (base.Equals(obj)) |
| 1024 | return true; |
| 1025 | string s = obj as string; |
| 1026 | if (s != null) |
| 1027 | return m_Data == s; |
| 1028 | JSONString s2 = obj as JSONString; |
| 1029 | if (s2 != null) |
| 1030 | return m_Data == s2.m_Data; |
| 1031 | return false; |
| 1032 | } |
| 1033 | public override int GetHashCode() |
| 1034 | { |
| 1035 | return m_Data.GetHashCode(); |
| 1036 | } |
| 1037 | } |
| 1038 | // End of JSONString |
| 1039 | |
| 1040 | public partial class JSONNumber : JSONNode |
| 1041 | { |
| 1042 | private double m_Data; |
| 1043 | |
| 1044 | public override JSONNodeType Tag { get { return JSONNodeType.Number; } } |
| 1045 | public override bool IsNumber { get { return true; } } |
| 1046 | public override Enumerator GetEnumerator() { return new Enumerator(); } |
| 1047 | |
| 1048 | public override string Value |
| 1049 | { |
| 1050 | get { return m_Data.ToString(CultureInfo.InvariantCulture); } |
| 1051 | set |
| 1052 | { |
| 1053 | double v; |
| 1054 | if (double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out v)) |
| 1055 | m_Data = v; |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | public override double AsDouble |
| 1060 | { |
| 1061 | get { return m_Data; } |
| 1062 | set { m_Data = value; } |
| 1063 | } |
| 1064 | public override long AsLong |
| 1065 | { |
| 1066 | get { return (long)m_Data; } |
| 1067 | set { m_Data = value; } |
| 1068 | } |
| 1069 | |
| 1070 | public JSONNumber(double aData) |
| 1071 | { |
| 1072 | m_Data = aData; |
| 1073 | } |
| 1074 | |
| 1075 | public JSONNumber(string aData) |
| 1076 | { |
| 1077 | Value = aData; |
| 1078 | } |
| 1079 | |
| 1080 | internal override void WriteToStringBuilder(StringBuilder aSB, int aIndent, int aIndentInc, JSONTextMode aMode) |
| 1081 | { |
| 1082 | aSB.Append(Value); |
| 1083 | } |
| 1084 | private static bool IsNumeric(object value) |
| 1085 | { |
| 1086 | return value is int || value is uint |
| 1087 | || value is float || value is double |
| 1088 | || value is decimal |
| 1089 | || value is long || value is ulong |
| 1090 | || value is short || value is ushort |
| 1091 | || value is sbyte || value is byte; |
| 1092 | } |
| 1093 | public override bool Equals(object obj) |
| 1094 | { |
| 1095 | if (obj == null) |
| 1096 | return false; |
| 1097 | if (base.Equals(obj)) |
| 1098 | return true; |
| 1099 | JSONNumber s2 = obj as JSONNumber; |
| 1100 | if (s2 != null) |
| 1101 | return m_Data == s2.m_Data; |
| 1102 | if (IsNumeric(obj)) |
| 1103 | return Convert.ToDouble(obj) == m_Data; |
| 1104 | return false; |
| 1105 | } |
| 1106 | public override int GetHashCode() |
| 1107 | { |
| 1108 | return m_Data.GetHashCode(); |
| 1109 | } |
| 1110 | } |
| 1111 | // End of JSONNumber |
| 1112 | |
| 1113 | public partial class JSONBool : JSONNode |
| 1114 | { |
| 1115 | private bool m_Data; |
| 1116 | |
| 1117 | public override JSONNodeType Tag { get { return JSONNodeType.Boolean; } } |
| 1118 | public override bool IsBoolean { get { return true; } } |
| 1119 | public override Enumerator GetEnumerator() { return new Enumerator(); } |
| 1120 | |
| 1121 | public override string Value |
| 1122 | { |
| 1123 | get { return m_Data.ToString(); } |
| 1124 | set |
| 1125 | { |
| 1126 | bool v; |
| 1127 | if (bool.TryParse(value, out v)) |
| 1128 | m_Data = v; |
| 1129 | } |
| 1130 | } |
| 1131 | public override bool AsBool |
| 1132 | { |
| 1133 | get { return m_Data; } |
| 1134 | set { m_Data = value; } |
| 1135 | } |
| 1136 | |
| 1137 | public JSONBool(bool aData) |
| 1138 | { |
| 1139 | m_Data = aData; |
| 1140 | } |
| 1141 | |
| 1142 | public JSONBool(string aData) |
| 1143 | { |
| 1144 | Value = aData; |
| 1145 | } |
| 1146 | |
| 1147 | internal override void WriteToStringBuilder(StringBuilder aSB, int aIndent, int aIndentInc, JSONTextMode aMode) |
| 1148 | { |
| 1149 | aSB.Append((m_Data) ? "true" : "false"); |
| 1150 | } |
| 1151 | public override bool Equals(object obj) |
| 1152 | { |
| 1153 | if (obj == null) |
| 1154 | return false; |
| 1155 | if (obj is bool) |
| 1156 | return m_Data == (bool)obj; |
| 1157 | return false; |
| 1158 | } |
| 1159 | public override int GetHashCode() |
| 1160 | { |
| 1161 | return m_Data.GetHashCode(); |
| 1162 | } |
| 1163 | } |
| 1164 | // End of JSONBool |
| 1165 | |
| 1166 | public partial class JSONNull : JSONNode |
| 1167 | { |
| 1168 | static JSONNull m_StaticInstance = new JSONNull(); |
| 1169 | public static bool reuseSameInstance = true; |
| 1170 | public static JSONNull CreateOrGet() |
| 1171 | { |
| 1172 | if (reuseSameInstance) |
| 1173 | return m_StaticInstance; |
| 1174 | return new JSONNull(); |
| 1175 | } |
| 1176 | private JSONNull() { } |
| 1177 | |
| 1178 | public override JSONNodeType Tag { get { return JSONNodeType.NullValue; } } |
| 1179 | public override bool IsNull { get { return true; } } |
| 1180 | public override Enumerator GetEnumerator() { return new Enumerator(); } |
| 1181 | |
| 1182 | public override string Value |
| 1183 | { |
| 1184 | get { return "null"; } |
| 1185 | set { } |
| 1186 | } |
| 1187 | public override bool AsBool |
| 1188 | { |
| 1189 | get { return false; } |
| 1190 | set { } |
| 1191 | } |
| 1192 | |
| 1193 | public override bool Equals(object obj) |
| 1194 | { |
| 1195 | if (object.ReferenceEquals(this, obj)) |
| 1196 | return true; |
| 1197 | return (obj is JSONNull); |
| 1198 | } |
| 1199 | public override int GetHashCode() |
| 1200 | { |
| 1201 | return 0; |
| 1202 | } |
| 1203 | |
| 1204 | internal override void WriteToStringBuilder(StringBuilder aSB, int aIndent, int aIndentInc, JSONTextMode aMode) |
| 1205 | { |
| 1206 | aSB.Append("null"); |
| 1207 | } |
| 1208 | } |
| 1209 | // End of JSONNull |
| 1210 | |
| 1211 | internal partial class JSONLazyCreator : JSONNode |
| 1212 | { |
| 1213 | private JSONNode m_Node = null; |
| 1214 | private string m_Key = null; |
| 1215 | public override JSONNodeType Tag { get { return JSONNodeType.None; } } |
| 1216 | public override Enumerator GetEnumerator() { return new Enumerator(); } |
| 1217 | |
| 1218 | public JSONLazyCreator(JSONNode aNode) |
| 1219 | { |
| 1220 | m_Node = aNode; |
| 1221 | m_Key = null; |
| 1222 | } |
| 1223 | |
| 1224 | public JSONLazyCreator(JSONNode aNode, string aKey) |
| 1225 | { |
| 1226 | m_Node = aNode; |
| 1227 | m_Key = aKey; |
| 1228 | } |
| 1229 | |
| 1230 | private T Set<T>(T aVal) where T : JSONNode |
| 1231 | { |
| 1232 | if (m_Key == null) |
| 1233 | m_Node.Add(aVal); |
| 1234 | else |
| 1235 | m_Node.Add(m_Key, aVal); |
| 1236 | m_Node = null; // Be GC friendly. |
| 1237 | return aVal; |
| 1238 | } |
| 1239 | |
| 1240 | public override JSONNode this[int aIndex] |
| 1241 | { |
| 1242 | get { return new JSONLazyCreator(this); } |
| 1243 | set { Set(new JSONArray()).Add(value); } |
| 1244 | } |
| 1245 | |
| 1246 | public override JSONNode this[string aKey] |
| 1247 | { |
| 1248 | get { return new JSONLazyCreator(this, aKey); } |
| 1249 | set { Set(new JSONObject()).Add(aKey, value); } |
| 1250 | } |
| 1251 | |
| 1252 | public override void Add(JSONNode aItem) |
| 1253 | { |
| 1254 | Set(new JSONArray()).Add(aItem); |
| 1255 | } |
| 1256 | |
| 1257 | public override void Add(string aKey, JSONNode aItem) |
| 1258 | { |
| 1259 | Set(new JSONObject()).Add(aKey, aItem); |
| 1260 | } |
| 1261 | |
| 1262 | public static bool operator ==(JSONLazyCreator a, object b) |
| 1263 | { |
| 1264 | if (b == null) |
| 1265 | return true; |
| 1266 | return System.Object.ReferenceEquals(a, b); |
| 1267 | } |
| 1268 | |
| 1269 | public static bool operator !=(JSONLazyCreator a, object b) |
| 1270 | { |
| 1271 | return !(a == b); |
| 1272 | } |
| 1273 | |
| 1274 | public override bool Equals(object obj) |
| 1275 | { |
| 1276 | if (obj == null) |
| 1277 | return true; |
| 1278 | return System.Object.ReferenceEquals(this, obj); |
| 1279 | } |
| 1280 | |
| 1281 | public override int GetHashCode() |
| 1282 | { |
| 1283 | return 0; |
| 1284 | } |
| 1285 | |
| 1286 | public override int AsInt |
| 1287 | { |
| 1288 | get { Set(new JSONNumber(0)); return 0; } |
| 1289 | set { Set(new JSONNumber(value)); } |
| 1290 | } |
| 1291 | |
| 1292 | public override float AsFloat |
| 1293 | { |
| 1294 | get { Set(new JSONNumber(0.0f)); return 0.0f; } |
| 1295 | set { Set(new JSONNumber(value)); } |
| 1296 | } |
| 1297 | |
| 1298 | public override double AsDouble |
| 1299 | { |
| 1300 | get { Set(new JSONNumber(0.0)); return 0.0; } |
| 1301 | set { Set(new JSONNumber(value)); } |
| 1302 | } |
| 1303 | |
| 1304 | public override long AsLong |
| 1305 | { |
| 1306 | get |
| 1307 | { |
| 1308 | if (longAsString) |
| 1309 | Set(new JSONString("0")); |
| 1310 | else |
| 1311 | Set(new JSONNumber(0.0)); |
| 1312 | return 0L; |
| 1313 | } |
| 1314 | set |
| 1315 | { |
| 1316 | if (longAsString) |
| 1317 | Set(new JSONString(value.ToString())); |
| 1318 | else |
| 1319 | Set(new JSONNumber(value)); |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | public override bool AsBool |
| 1324 | { |
| 1325 | get { Set(new JSONBool(false)); return false; } |
| 1326 | set { Set(new JSONBool(value)); } |
| 1327 | } |
| 1328 | |
| 1329 | public override JSONArray AsArray |
| 1330 | { |
| 1331 | get { return Set(new JSONArray()); } |
| 1332 | } |
| 1333 | |
| 1334 | public override JSONObject AsObject |
| 1335 | { |
| 1336 | get { return Set(new JSONObject()); } |
| 1337 | } |
| 1338 | internal override void WriteToStringBuilder(StringBuilder aSB, int aIndent, int aIndentInc, JSONTextMode aMode) |
| 1339 | { |
| 1340 | aSB.Append("null"); |
| 1341 | } |
| 1342 | } |
| 1343 | // End of JSONLazyCreator |
| 1344 | |
| 1345 | public static class JSON |
| 1346 | { |
| 1347 | public static JSONNode Parse(string aJSON) |
| 1348 | { |
| 1349 | return JSONNode.Parse(aJSON); |
| 1350 | } |
| 1351 | } |
| 1352 | } |