How to use string method of SimpleJSON.LinqEnumerator class

Best Vstest code snippet using SimpleJSON.LinqEnumerator.string

SimpleJSON.cs

Source:SimpleJSON.cs Github

copy

Full Screen

1/* * * * *2 * A simple JSON Parser / builder3 * ------------------------------4 * 5 * It mainly has been written as a simple JSON parser. It can build a JSON string6 * 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 include9 * SharpZipLib ( http://www.icsharpcode.net/opensource/sharpziplib/ ) in your project and10 * define "USE_SharpZipLib" at the top of the file11 * 12 * Written by Bunny83 13 * 2012-06-0914 * 15 * [2012-06-09 First Version]16 * - provides strongly typed node classes and lists / dictionaries17 * - provides easy access to class members / array items / data values18 * - 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 / bool22 * - 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 undefined24 * - It can serialize/deserialize a node tree into/from an experimental compact binary format. It might25 * be handy if you want to store things in a file and don't want it to be easily modifiable26 * 27 * [2012-12-17 Update]28 * - Added internal JSONLazyCreator class which simplifies the construction of a JSON tree29 * Now you can simple reference any item that doesn't exist yet and it will return a JSONLazyCreator30 * 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 top34 * - The serializer uses different types when it comes to store the values. Since my data values35 * 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 large40 * 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 ) this43 * allows to propertly convert the node tree back to json without type information loss. The actual value44 * 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 files49 * - 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 common53 * struct Enumerator in JSONNode that should avoid garbage generation. The enumerator always works54 * on KeyValuePair<string, JSONNode>, even for JSONArray.55 * - Added two wrapper Enumerators that allows for easy key or value enumeration. A JSONNode now has56 * a "Keys" and a "Values" enumerable property. Those are also struct enumerators / enumerables57 * - A KeyValuePair<string, JSONNode> can now be implicitly converted into a JSONNode. This allows58 * a foreach loop over a JSONNode to directly access the values only. Since KeyValuePair as well as59 * all the Enumerators are structs, no garbage is allocated.60 * - To add Linq support another "LinqEnumerator" is available through the "Linq" property. This61 * enumerator does implement the generic IEnumerable interface so most Linq extensions can be used62 * 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 characters64 * (\uXXXX) and if the static bool JSONNode.forceASCII is set to true it will also escape all65 * characters # > 127. This might be useful if you require an ASCII output. Though keep in mind66 * when your strings contain many non-ascii characters the strings become much longer (x6) and are67 * no longer human readable.68 * - The node types JSONObject and JSONArray now have an "Inline" boolean switch which will default to69 * false. It can be used to serialize this element inline even you serialize with an indented format70 * This is useful for arrays containing numbers so it doesn't place every number on a new line71 * - Extracted the binary serialization code into a seperate extension file. All classes are now declared72 * as "partial" so an extension file can even add a new virtual or abstract method / interface to73 * JSONNode and override it in the concrete type classes. It's of course a hacky approach which is74 * 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 could76 * be reused without major problems. If you have a lot null fields in your data it will help reduce77 * the memory / garbage overhead. I also added a static setting (reuseSameInstance) to JSONNull78 * (default is true) which will change the behaviour of "CreateOrGet". If you set this to false79 * 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 use81 * JSONNull.CreateOrGet()82 * 83 * [2018-01-09 Update]84 * - Changed all double.TryParse and double.ToString uses to use the invariant culture to avoid problems85 * 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. However89 * storing it as string would work.90 * - Added static setting "JSONNode.longAsString" which controls the default type that is used by the91 * LazyCreator when using AsLong92 * 93 * 94 * The MIT License (MIT)95 * 96 * Copyright (c) 2012-2017 Markus Göbel (Bunny83)97 * 98 * Permission is hereby granted, free of charge, to any person obtaining a copy99 * of this software and associated documentation files (the "Software"), to deal100 * in the Software without restriction, including without limitation the rights101 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell102 * copies of the Software, and to permit persons to whom the Software is103 * furnished to do so, subject to the following conditions:104 * 105 * The above copyright notice and this permission notice shall be included in all106 * copies or substantial portions of the Software.107 * 108 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR109 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,110 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE111 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER112 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,113 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE114 * SOFTWARE.115 * 116 * * * * */117using System;118using System.Collections;119using System.Collections.Generic;120using System.Globalization;121using System.Linq;122using System.Text;123namespace ThirdParty.SimpleJSON124{125 public enum JSONNodeType126 {127 Array = 1,128 Object = 2,129 String = 3,130 Number = 4,131 NullValue = 5,132 Boolean = 6,133 None = 7,134 Custom = 0xFF,135 }136 public enum JSONTextMode137 {138 Compact,139 Indent140 }141 public abstract partial class JSONNode142 {143 #region Enumerators144 public struct Enumerator145 {146 enum Type { None, Array, Object }147 Type type;148 Dictionary<string, global::ThirdParty.SimpleJSON.JSONNode>.Enumerator m_Object;149 List<global::ThirdParty.SimpleJSON.JSONNode>.Enumerator m_Array;150 public bool IsValid { get { return type != Type.None; } }151 public Enumerator(List<global::ThirdParty.SimpleJSON.JSONNode>.Enumerator aArrayEnum)152 {153 type = Type.Array;154 m_Object = default(Dictionary<string, global::ThirdParty.SimpleJSON.JSONNode>.Enumerator);155 m_Array = aArrayEnum;156 }157 public Enumerator(Dictionary<string, global::ThirdParty.SimpleJSON.JSONNode>.Enumerator aDictEnum)158 {159 type = Type.Object;160 m_Object = aDictEnum;161 m_Array = default(List<global::ThirdParty.SimpleJSON.JSONNode>.Enumerator);162 }163 public KeyValuePair<string, global::ThirdParty.SimpleJSON.JSONNode> Current164 {165 get {166 if (type == Type.Array)167 return new KeyValuePair<string, global::ThirdParty.SimpleJSON.JSONNode>(string.Empty, m_Array.Current);168 else if (type == Type.Object)169 return m_Object.Current;170 return new KeyValuePair<string, global::ThirdParty.SimpleJSON.JSONNode>(string.Empty, null);171 }172 }173 public bool MoveNext()174 {175 if (type == Type.Array)176 return m_Array.MoveNext();177 else if (type == Type.Object)178 return m_Object.MoveNext();179 return false;180 }181 }182 public struct ValueEnumerator183 {184 Enumerator m_Enumerator;185 public ValueEnumerator(List<global::ThirdParty.SimpleJSON.JSONNode>.Enumerator aArrayEnum) : this(new Enumerator(aArrayEnum)) { }186 public ValueEnumerator(Dictionary<string, global::ThirdParty.SimpleJSON.JSONNode>.Enumerator aDictEnum) : this(new Enumerator(aDictEnum)) { }187 public ValueEnumerator(Enumerator aEnumerator) { m_Enumerator = aEnumerator; }188 public global::ThirdParty.SimpleJSON.JSONNode Current { get { return m_Enumerator.Current.Value; } }189 public bool MoveNext() { return m_Enumerator.MoveNext(); }190 public ValueEnumerator GetEnumerator() { return this; }191 }192 public struct KeyEnumerator193 {194 Enumerator m_Enumerator;195 public KeyEnumerator(List<global::ThirdParty.SimpleJSON.JSONNode>.Enumerator aArrayEnum) : this(new Enumerator(aArrayEnum)) { }196 public KeyEnumerator(Dictionary<string, global::ThirdParty.SimpleJSON.JSONNode>.Enumerator aDictEnum) : this(new Enumerator(aDictEnum)) { }197 public KeyEnumerator(Enumerator aEnumerator) { m_Enumerator = aEnumerator; }198 public global::ThirdParty.SimpleJSON.JSONNode Current { get { return m_Enumerator.Current.Key; } }199 public bool MoveNext() { return m_Enumerator.MoveNext(); }200 public KeyEnumerator GetEnumerator() { return this; }201 }202 public class LinqEnumerator : IEnumerator<KeyValuePair<string, global::ThirdParty.SimpleJSON.JSONNode>>, IEnumerable<KeyValuePair<string, global::ThirdParty.SimpleJSON.JSONNode>>203 {204 global::ThirdParty.SimpleJSON.JSONNode m_Node;205 Enumerator m_Enumerator;206 internal LinqEnumerator(global::ThirdParty.SimpleJSON.JSONNode aNode)207 {208 m_Node = aNode;209 if (m_Node != null)210 m_Enumerator = m_Node.GetEnumerator();211 }212 public KeyValuePair<string, global::ThirdParty.SimpleJSON.JSONNode> Current { get { return m_Enumerator.Current; } }213 object IEnumerator.Current { get { return m_Enumerator.Current; } }214 public bool MoveNext() { return m_Enumerator.MoveNext(); }215 public void Dispose()216 {217 m_Node = null;218 m_Enumerator = new Enumerator();219 }220 public IEnumerator<KeyValuePair<string, global::ThirdParty.SimpleJSON.JSONNode>> GetEnumerator()221 {222 return new LinqEnumerator(m_Node);223 }224 public void Reset()225 {226 if (m_Node != null)227 m_Enumerator = m_Node.GetEnumerator();228 }229 IEnumerator IEnumerable.GetEnumerator()230 {231 return new LinqEnumerator(m_Node);232 }233 }234 #endregion Enumerators235 #region common interface236 public static bool forceASCII = false; // Use Unicode by default237 public static bool longAsString = false; // lazy creator creates a JSONString instead of JSONNumber238 public abstract JSONNodeType Tag { get; }239 public virtual global::ThirdParty.SimpleJSON.JSONNode this[int aIndex] { get { return null; } set { } }240 public virtual global::ThirdParty.SimpleJSON.JSONNode this[string aKey] { get { return null; } set { } }241 public virtual string Value { get { return ""; } set { } }242 public virtual int Count { get { return 0; } }243 public virtual bool IsNumber { get { return false; } }244 public virtual bool IsString { get { return false; } }245 public virtual bool IsBoolean { get { return false; } }246 public virtual bool IsNull { get { return false; } }247 public virtual bool IsArray { get { return false; } }248 public virtual bool IsObject { get { return false; } }249 public virtual bool Inline { get { return false; } set { } }250 public virtual void Add(string aKey, global::ThirdParty.SimpleJSON.JSONNode aItem)251 {252 }253 public virtual void Add(global::ThirdParty.SimpleJSON.JSONNode aItem)254 {255 Add("", aItem);256 }257 public virtual global::ThirdParty.SimpleJSON.JSONNode Remove(string aKey)258 {259 return null;260 }261 public virtual global::ThirdParty.SimpleJSON.JSONNode Remove(int aIndex)262 {263 return null;264 }265 public virtual global::ThirdParty.SimpleJSON.JSONNode Remove(global::ThirdParty.SimpleJSON.JSONNode aNode)266 {267 return aNode;268 }269 public virtual IEnumerable<global::ThirdParty.SimpleJSON.JSONNode> Children270 {271 get272 {273 yield break;274 }275 }276 public IEnumerable<global::ThirdParty.SimpleJSON.JSONNode> DeepChildren277 {278 get279 {280 foreach (global::ThirdParty.SimpleJSON.JSONNode C in Children)281 foreach (global::ThirdParty.SimpleJSON.JSONNode D in C.DeepChildren)282 yield return D;283 }284 }285 public override string ToString()286 {287 StringBuilder sb = new StringBuilder();288 WriteToStringBuilder(sb, 0, 0, JSONTextMode.Compact);289 return sb.ToString();290 }291 public virtual string ToString(int aIndent)292 {293 StringBuilder sb = new StringBuilder();294 WriteToStringBuilder(sb, 0, aIndent, JSONTextMode.Indent);295 return sb.ToString();296 }297 internal abstract void WriteToStringBuilder(StringBuilder aSB, int aIndent, int aIndentInc, JSONTextMode aMode);298 public abstract Enumerator GetEnumerator();299 public IEnumerable<KeyValuePair<string, global::ThirdParty.SimpleJSON.JSONNode>> Linq { get { return new LinqEnumerator(this); } }300 public KeyEnumerator Keys { get { return new KeyEnumerator(GetEnumerator()); } }301 public ValueEnumerator Values { get { return new ValueEnumerator(GetEnumerator()); } }302 #endregion common interface303 #region typecasting properties304 public virtual double AsDouble305 {306 get307 {308 double v = 0.0;309 if (double.TryParse(Value,NumberStyles.Float, CultureInfo.InvariantCulture, out v))310 return v;311 return 0.0;312 }313 set314 {315 Value = value.ToString(CultureInfo.InvariantCulture);316 }317 }318 public virtual int AsInt319 {320 get { return (int)AsDouble; }321 set { AsDouble = value; }322 }323 public virtual float AsFloat324 {325 get { return (float)AsDouble; }326 set { AsDouble = value; }327 }328 public virtual bool AsBool329 {330 get331 {332 bool v = false;333 if (bool.TryParse(Value, out v))334 return v;335 return !string.IsNullOrEmpty(Value);336 }337 set338 {339 Value = (value) ? "true" : "false";340 }341 }342 public virtual long AsLong343 {344 get345 {346 long val = 0;347 if (long.TryParse(Value, out val))348 return val;349 return 0L;350 }351 set352 {353 Value = value.ToString();354 }355 }356 public virtual global::ThirdParty.SimpleJSON.JSONArray AsArray357 {358 get359 {360 return this as global::ThirdParty.SimpleJSON.JSONArray;361 }362 }363 public virtual global::ThirdParty.SimpleJSON.JSONObject AsObject364 {365 get366 {367 return this as global::ThirdParty.SimpleJSON.JSONObject;368 }369 }370 #endregion typecasting properties371 #region operators372 public static implicit operator global::ThirdParty.SimpleJSON.JSONNode(string s)373 {374 return new global::ThirdParty.SimpleJSON.JSONString(s);375 }376 public static implicit operator string(global::ThirdParty.SimpleJSON.JSONNode d)377 {378 return (d == null) ? null : d.Value;379 }380 public static implicit operator global::ThirdParty.SimpleJSON.JSONNode(double n)381 {382 return new global::ThirdParty.SimpleJSON.JSONNumber(n);383 }384 public static implicit operator double(global::ThirdParty.SimpleJSON.JSONNode d)385 {386 return (d == null) ? 0 : d.AsDouble;387 }388 public static implicit operator global::ThirdParty.SimpleJSON.JSONNode(float n)389 {390 return new global::ThirdParty.SimpleJSON.JSONNumber(n);391 }392 public static implicit operator float(global::ThirdParty.SimpleJSON.JSONNode d)393 {394 return (d == null) ? 0 : d.AsFloat;395 }396 public static implicit operator global::ThirdParty.SimpleJSON.JSONNode(int n)397 {398 return new global::ThirdParty.SimpleJSON.JSONNumber(n);399 }400 public static implicit operator int(global::ThirdParty.SimpleJSON.JSONNode d)401 {402 return (d == null) ? 0 : d.AsInt;403 }404 public static implicit operator global::ThirdParty.SimpleJSON.JSONNode(long n)405 {406 return new global::ThirdParty.SimpleJSON.JSONNumber(n);407 }408 public static implicit operator long(global::ThirdParty.SimpleJSON.JSONNode d)409 {410 return (d == null) ? 0L : d.AsLong;411 }412 public static implicit operator global::ThirdParty.SimpleJSON.JSONNode(bool b)413 {414 return new global::ThirdParty.SimpleJSON.JSONBool(b);415 }416 public static implicit operator bool(global::ThirdParty.SimpleJSON.JSONNode d)417 {418 return (d == null) ? false : d.AsBool;419 }420 public static implicit operator global::ThirdParty.SimpleJSON.JSONNode(KeyValuePair<string, global::ThirdParty.SimpleJSON.JSONNode> aKeyValue)421 {422 return aKeyValue.Value;423 }424 public static bool operator ==(global::ThirdParty.SimpleJSON.JSONNode a, object b)425 {426 if (ReferenceEquals(a, b))427 return true;428 bool aIsNull = a is global::ThirdParty.SimpleJSON.JSONNull || ReferenceEquals(a, null) || a is global::ThirdParty.SimpleJSON.JSONLazyCreator;429 bool bIsNull = b is global::ThirdParty.SimpleJSON.JSONNull || ReferenceEquals(b, null) || b is global::ThirdParty.SimpleJSON.JSONLazyCreator;430 if (aIsNull && bIsNull)431 return true;432 return !aIsNull && a.Equals(b);433 }434 public static bool operator !=(global::ThirdParty.SimpleJSON.JSONNode a, object b)435 {436 return !(a == b);437 }438 public override bool Equals(object obj)439 {440 return ReferenceEquals(this, obj);441 }442 public override int GetHashCode()443 {444 return base.GetHashCode();445 }446 #endregion operators447 [ThreadStatic]448 static StringBuilder m_EscapeBuilder;449 internal static StringBuilder EscapeBuilder450 {451 get {452 if (m_EscapeBuilder == null)453 m_EscapeBuilder = new StringBuilder();454 return m_EscapeBuilder;455 }456 }457 internal static string Escape(string aText)458 {459 StringBuilder sb = EscapeBuilder;460 sb.Length = 0;461 if (sb.Capacity < aText.Length + aText.Length / 10)462 sb.Capacity = aText.Length + aText.Length / 10;463 foreach (char c in aText)464 {465 switch (c)466 {467 case '\\':468 sb.Append("\\\\");469 break;470 case '\"':471 sb.Append("\\\"");472 break;473 case '\n':474 sb.Append("\\n");475 break;476 case '\r':477 sb.Append("\\r");478 break;479 case '\t':480 sb.Append("\\t");481 break;482 case '\b':483 sb.Append("\\b");484 break;485 case '\f':486 sb.Append("\\f");487 break;488 default:489 if (c < ' ' || (forceASCII && c > 127))490 {491 ushort val = c;492 sb.Append("\\u").Append(val.ToString("X4"));493 }494 else495 sb.Append(c);496 break;497 }498 }499 string result = sb.ToString();500 sb.Length = 0;501 return result;502 }503 static void ParseElement(global::ThirdParty.SimpleJSON.JSONNode ctx, string token, string tokenName, bool quoted)504 {505 if (quoted)506 {507 ctx.Add(tokenName, token);508 return;509 }510 string tmp = token.ToLower();511 if (tmp == "false" || tmp == "true")512 ctx.Add(tokenName, tmp == "true");513 else if (tmp == "null")514 ctx.Add(tokenName, null);515 else516 {517 double val;518 if (double.TryParse(token, NumberStyles.Float, CultureInfo.InvariantCulture, out val))519 ctx.Add(tokenName, val);520 else521 ctx.Add(tokenName, token);522 }523 }524 public static global::ThirdParty.SimpleJSON.JSONNode Parse(string aJSON)525 {526 Stack<global::ThirdParty.SimpleJSON.JSONNode> stack = new Stack<global::ThirdParty.SimpleJSON.JSONNode>();527 global::ThirdParty.SimpleJSON.JSONNode ctx = null;528 int i = 0;529 StringBuilder Token = new StringBuilder();530 string TokenName = "";531 bool QuoteMode = false;532 bool TokenIsQuoted = false;533 while (i < aJSON.Length) {534 switch (aJSON[i])535 {536 case '{':537 if (QuoteMode)538 {539 Token.Append(aJSON[i]);540 break;541 }542 stack.Push(new global::ThirdParty.SimpleJSON.JSONObject());543 if (ctx != null)544 {545 ctx.Add(TokenName, stack.Peek());546 }547 TokenName = "";548 Token.Length = 0;549 ctx = stack.Peek();550 break;551 case '[':552 if (QuoteMode)553 {554 Token.Append(aJSON[i]);555 break;556 }557 stack.Push(new global::ThirdParty.SimpleJSON.JSONArray());558 if (ctx != null)559 {560 ctx.Add(TokenName, stack.Peek());561 }562 TokenName = "";563 Token.Length = 0;564 ctx = stack.Peek();565 break;566 case '}':567 case ']':568 if (QuoteMode)569 {570 Token.Append(aJSON[i]);571 break;572 }573 if (stack.Count == 0)574 throw new Exception("JSON Parse: Too many closing brackets");575 stack.Pop();576 if (Token.Length > 0 || TokenIsQuoted)577 {578 ParseElement(ctx, Token.ToString(), TokenName, TokenIsQuoted);579 TokenIsQuoted = false;580 }581 TokenName = "";582 Token.Length = 0;583 if (stack.Count > 0)584 ctx = stack.Peek();585 break;586 case ':':587 if (QuoteMode)588 {589 Token.Append(aJSON[i]);590 break;591 }592 TokenName = Token.ToString();593 Token.Length = 0;594 TokenIsQuoted = false;595 break;596 case '"':597 QuoteMode ^= true;598 TokenIsQuoted |= QuoteMode;599 break;600 case ',':601 if (QuoteMode)602 {603 Token.Append(aJSON[i]);604 break;605 }606 if (Token.Length > 0 || TokenIsQuoted)607 {608 ParseElement(ctx, Token.ToString(), TokenName, TokenIsQuoted);609 TokenIsQuoted = false;610 }611 TokenName = "";612 Token.Length = 0;613 TokenIsQuoted = false;614 break;615 case '\r':616 case '\n':617 break;618 case ' ':619 case '\t':620 if (QuoteMode)621 Token.Append(aJSON[i]);622 break;623 case '\\':624 ++i;625 if (QuoteMode)626 {627 char C = aJSON[i];628 switch (C)629 {630 case 't':631 Token.Append('\t');632 break;633 case 'r':634 Token.Append('\r');635 break;636 case 'n':637 Token.Append('\n');638 break;639 case 'b':640 Token.Append('\b');641 break;642 case 'f':643 Token.Append('\f');644 break;645 case 'u':646 {647 string s = aJSON.Substring(i + 1, 4);648 Token.Append((char)int.Parse(649 s,650 System.Globalization.NumberStyles.AllowHexSpecifier));651 i += 4;652 break;653 }654 default:655 Token.Append(C);656 break;657 }658 }659 break;660 default:661 Token.Append(aJSON[i]);662 break;663 }664 ++i;665 }666 if (QuoteMode)667 {668 throw new Exception("JSON Parse: Quotation marks seems to be messed up.");669 }670 return ctx;671 }672 }673 // End of JSONNode674 public partial class JSONArray : global::ThirdParty.SimpleJSON.JSONNode675 {676 List<global::ThirdParty.SimpleJSON.JSONNode> m_List = new List<global::ThirdParty.SimpleJSON.JSONNode>();677 bool inline = false;678 public override bool Inline679 {680 get { return inline; }681 set { inline = value; }682 }683 public override JSONNodeType Tag { get { return JSONNodeType.Array; } }684 public override bool IsArray { get { return true; } }685 public override Enumerator GetEnumerator() { return new Enumerator(m_List.GetEnumerator()); }686 public override global::ThirdParty.SimpleJSON.JSONNode this[int aIndex]687 {688 get689 {690 if (aIndex < 0 || aIndex >= m_List.Count)691 return new global::ThirdParty.SimpleJSON.JSONLazyCreator(this);692 return m_List[aIndex];693 }694 set695 {696 if (value == null)697 value = global::ThirdParty.SimpleJSON.JSONNull.CreateOrGet();698 if (aIndex < 0 || aIndex >= m_List.Count)699 m_List.Add(value);700 else701 m_List[aIndex] = value;702 }703 }704 public override global::ThirdParty.SimpleJSON.JSONNode this[string aKey]705 {706 get { return new global::ThirdParty.SimpleJSON.JSONLazyCreator(this); }707 set708 {709 if (value == null)710 value = global::ThirdParty.SimpleJSON.JSONNull.CreateOrGet();711 m_List.Add(value);712 }713 }714 public override int Count715 {716 get { return m_List.Count; }717 }718 public override void Add(string aKey, global::ThirdParty.SimpleJSON.JSONNode aItem)719 {720 if (aItem == null)721 aItem = global::ThirdParty.SimpleJSON.JSONNull.CreateOrGet();722 m_List.Add(aItem);723 }724 public override global::ThirdParty.SimpleJSON.JSONNode Remove(int aIndex)725 {726 if (aIndex < 0 || aIndex >= m_List.Count)727 return null;728 global::ThirdParty.SimpleJSON.JSONNode tmp = m_List[aIndex];729 m_List.RemoveAt(aIndex);730 return tmp;731 }732 public override global::ThirdParty.SimpleJSON.JSONNode Remove(global::ThirdParty.SimpleJSON.JSONNode aNode)733 {734 m_List.Remove(aNode);735 return aNode;736 }737 public override IEnumerable<global::ThirdParty.SimpleJSON.JSONNode> Children738 {739 get740 {741 foreach (global::ThirdParty.SimpleJSON.JSONNode N in m_List)742 yield return N;743 }744 }745 internal override void WriteToStringBuilder(StringBuilder aSB, int aIndent, int aIndentInc, JSONTextMode aMode)746 {747 aSB.Append('[');748 int count = m_List.Count;749 if (inline)750 aMode = JSONTextMode.Compact;751 for (int i = 0; i < count; i++)752 {753 if (i > 0)754 aSB.Append(',');755 if (aMode == JSONTextMode.Indent)756 aSB.AppendLine();757 if (aMode == JSONTextMode.Indent)758 aSB.Append(' ', aIndent + aIndentInc);759 m_List[i].WriteToStringBuilder(aSB, aIndent + aIndentInc, aIndentInc, aMode);760 }761 if (aMode == JSONTextMode.Indent)762 aSB.AppendLine().Append(' ', aIndent);763 aSB.Append(']');764 }765 }766 // End of JSONArray767 public partial class JSONObject : global::ThirdParty.SimpleJSON.JSONNode768 {769 Dictionary<string, global::ThirdParty.SimpleJSON.JSONNode> m_Dict = new Dictionary<string, global::ThirdParty.SimpleJSON.JSONNode>();770 bool inline = false;771 public override bool Inline772 {773 get { return inline; }774 set { inline = value; }775 }776 public override JSONNodeType Tag { get { return JSONNodeType.Object; } }777 public override bool IsObject { get { return true; } }778 public override Enumerator GetEnumerator() { return new Enumerator(m_Dict.GetEnumerator()); }779 public override global::ThirdParty.SimpleJSON.JSONNode this[string aKey]780 {781 get782 {783 if (m_Dict.ContainsKey(aKey))784 return m_Dict[aKey];785 else786 return new global::ThirdParty.SimpleJSON.JSONLazyCreator(this, aKey);787 }788 set789 {790 if (value == null)791 value = global::ThirdParty.SimpleJSON.JSONNull.CreateOrGet();792 if (m_Dict.ContainsKey(aKey))793 m_Dict[aKey] = value;794 else795 m_Dict.Add(aKey, value);796 }797 }798 public override global::ThirdParty.SimpleJSON.JSONNode this[int aIndex]799 {800 get801 {802 if (aIndex < 0 || aIndex >= m_Dict.Count)803 return null;804 return m_Dict.ElementAt(aIndex).Value;805 }806 set807 {808 if (value == null)809 value = global::ThirdParty.SimpleJSON.JSONNull.CreateOrGet();810 if (aIndex < 0 || aIndex >= m_Dict.Count)811 return;812 string key = m_Dict.ElementAt(aIndex).Key;813 m_Dict[key] = value;814 }815 }816 public override int Count817 {818 get { return m_Dict.Count; }819 }820 public override void Add(string aKey, global::ThirdParty.SimpleJSON.JSONNode aItem)821 {822 if (aItem == null)823 aItem = global::ThirdParty.SimpleJSON.JSONNull.CreateOrGet();824 if (!string.IsNullOrEmpty(aKey))825 {826 if (m_Dict.ContainsKey(aKey))827 m_Dict[aKey] = aItem;828 else829 m_Dict.Add(aKey, aItem);830 }831 else832 m_Dict.Add(Guid.NewGuid().ToString(), aItem);833 }834 public override global::ThirdParty.SimpleJSON.JSONNode Remove(string aKey)835 {836 if (!m_Dict.ContainsKey(aKey))837 return null;838 global::ThirdParty.SimpleJSON.JSONNode tmp = m_Dict[aKey];839 m_Dict.Remove(aKey);840 return tmp;841 }842 public override global::ThirdParty.SimpleJSON.JSONNode Remove(int aIndex)843 {844 if (aIndex < 0 || aIndex >= m_Dict.Count)845 return null;846 KeyValuePair<string, global::ThirdParty.SimpleJSON.JSONNode> item = m_Dict.ElementAt(aIndex);847 m_Dict.Remove(item.Key);848 return item.Value;849 }850 public override global::ThirdParty.SimpleJSON.JSONNode Remove(global::ThirdParty.SimpleJSON.JSONNode aNode)851 {852 try853 {854 KeyValuePair<string, global::ThirdParty.SimpleJSON.JSONNode> item = m_Dict.Where(k => k.Value == aNode).First();855 m_Dict.Remove(item.Key);856 return aNode;857 }858 catch859 {860 return null;861 }862 }863 public override IEnumerable<global::ThirdParty.SimpleJSON.JSONNode> Children864 {865 get866 {867 foreach (KeyValuePair<string, global::ThirdParty.SimpleJSON.JSONNode> N in m_Dict)868 yield return N.Value;869 }870 }871 internal override void WriteToStringBuilder(StringBuilder aSB, int aIndent, int aIndentInc, JSONTextMode aMode)872 {873 aSB.Append('{');874 bool first = true;875 if (inline)876 aMode = JSONTextMode.Compact;877 foreach (KeyValuePair<string, global::ThirdParty.SimpleJSON.JSONNode> k in m_Dict)878 {879 if (!first)880 aSB.Append(',');881 first = false;882 if (aMode == JSONTextMode.Indent)883 aSB.AppendLine();884 if (aMode == JSONTextMode.Indent)885 aSB.Append(' ', aIndent + aIndentInc);886 aSB.Append('\"').Append(Escape(k.Key)).Append('\"');887 if (aMode == JSONTextMode.Compact)888 aSB.Append(':');889 else890 aSB.Append(" : ");891 k.Value.WriteToStringBuilder(aSB, aIndent + aIndentInc, aIndentInc, aMode);892 }893 if (aMode == JSONTextMode.Indent)894 aSB.AppendLine().Append(' ', aIndent);895 aSB.Append('}');896 }897 }898 // End of JSONObject899 public partial class JSONString : global::ThirdParty.SimpleJSON.JSONNode900 {901 string m_Data;902 public override JSONNodeType Tag { get { return JSONNodeType.String; } }903 public override bool IsString { get { return true; } }904 public override Enumerator GetEnumerator() { return new Enumerator(); }905 public override string Value906 {907 get { return m_Data; }908 set909 {910 m_Data = value;911 }912 }913 public JSONString(string aData)914 {915 m_Data = aData;916 }917 internal override void WriteToStringBuilder(StringBuilder aSB, int aIndent, int aIndentInc, JSONTextMode aMode)918 {919 aSB.Append('\"').Append(Escape(m_Data)).Append('\"');920 }921 public override bool Equals(object obj)922 {923 if (base.Equals(obj))924 return true;925 string s = obj as string;926 if (s != null)927 return m_Data == s;928 global::ThirdParty.SimpleJSON.JSONString s2 = obj as global::ThirdParty.SimpleJSON.JSONString;929 if (s2 != null)930 return m_Data == s2.m_Data;931 return false;932 }933 public override int GetHashCode()934 {935 return m_Data.GetHashCode();936 }937 }938 // End of JSONString939 public partial class JSONNumber : global::ThirdParty.SimpleJSON.JSONNode940 {941 double m_Data;942 public override JSONNodeType Tag { get { return JSONNodeType.Number; } }943 public override bool IsNumber { get { return true; } }944 public override Enumerator GetEnumerator() { return new Enumerator(); }945 public override string Value946 {947 get { return m_Data.ToString(CultureInfo.InvariantCulture); }948 set949 {950 double v;951 if (double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out v))952 m_Data = v;953 }954 }955 public override double AsDouble956 {957 get { return m_Data; }958 set { m_Data = value; }959 }960 public override long AsLong961 {962 get { return (long)m_Data; }963 set { m_Data = value; }964 }965 public JSONNumber(double aData)966 {967 m_Data = aData;968 }969 public JSONNumber(string aData)970 {971 Value = aData;972 }973 internal override void WriteToStringBuilder(StringBuilder aSB, int aIndent, int aIndentInc, JSONTextMode aMode)974 {975 aSB.Append(Value);976 }977 static bool IsNumeric(object value)978 {979 return value is int || value is uint980 || value is float || value is double981 || value is decimal982 || value is long || value is ulong983 || value is short || value is ushort984 || value is sbyte || value is byte;985 }986 public override bool Equals(object obj)987 {988 if (obj == null)989 return false;990 if (base.Equals(obj))991 return true;992 global::ThirdParty.SimpleJSON.JSONNumber s2 = obj as global::ThirdParty.SimpleJSON.JSONNumber;993 if (s2 != null)994 return m_Data == s2.m_Data;995 if (IsNumeric(obj))996 return Convert.ToDouble(obj) == m_Data;997 return false;998 }999 public override int GetHashCode()1000 {1001 return m_Data.GetHashCode();1002 }1003 }1004 // End of JSONNumber1005 public partial class JSONBool : global::ThirdParty.SimpleJSON.JSONNode1006 {1007 bool m_Data;1008 public override JSONNodeType Tag { get { return JSONNodeType.Boolean; } }1009 public override bool IsBoolean { get { return true; } }1010 public override Enumerator GetEnumerator() { return new Enumerator(); }1011 public override string Value1012 {1013 get { return m_Data.ToString(); }1014 set1015 {1016 bool v;1017 if (bool.TryParse(value, out v))1018 m_Data = v;1019 }1020 }1021 public override bool AsBool1022 {1023 get { return m_Data; }1024 set { m_Data = value; }1025 }1026 public JSONBool(bool aData)1027 {1028 m_Data = aData;1029 }1030 public JSONBool(string aData)1031 {1032 Value = aData;1033 }1034 internal override void WriteToStringBuilder(StringBuilder aSB, int aIndent, int aIndentInc, JSONTextMode aMode)1035 {1036 aSB.Append((m_Data) ? "true" : "false");1037 }1038 public override bool Equals(object obj)1039 {1040 if (obj == null)1041 return false;1042 if (obj is bool)1043 return m_Data == (bool)obj;1044 return false;1045 }1046 public override int GetHashCode()1047 {1048 return m_Data.GetHashCode();1049 }1050 }1051 // End of JSONBool1052 public partial class JSONNull : global::ThirdParty.SimpleJSON.JSONNode1053 {1054 static global::ThirdParty.SimpleJSON.JSONNull m_StaticInstance = new global::ThirdParty.SimpleJSON.JSONNull();1055 public static bool reuseSameInstance = true;1056 public static global::ThirdParty.SimpleJSON.JSONNull CreateOrGet()1057 {1058 if (reuseSameInstance)1059 return m_StaticInstance;1060 return new global::ThirdParty.SimpleJSON.JSONNull();1061 }1062 JSONNull() { }1063 public override JSONNodeType Tag { get { return JSONNodeType.NullValue; } }1064 public override bool IsNull { get { return true; } }1065 public override Enumerator GetEnumerator() { return new Enumerator(); }1066 public override string Value1067 {1068 get { return "null"; }1069 set { }1070 }1071 public override bool AsBool1072 {1073 get { return false; }1074 set { }1075 }1076 public override bool Equals(object obj)1077 {1078 if (object.ReferenceEquals(this, obj))1079 return true;1080 return (obj is global::ThirdParty.SimpleJSON.JSONNull);1081 }1082 public override int GetHashCode()1083 {1084 return 0;1085 }1086 internal override void WriteToStringBuilder(StringBuilder aSB, int aIndent, int aIndentInc, JSONTextMode aMode)1087 {1088 aSB.Append("null");1089 }1090 }1091 // End of JSONNull1092 internal partial class JSONLazyCreator : global::ThirdParty.SimpleJSON.JSONNode1093 {1094 global::ThirdParty.SimpleJSON.JSONNode m_Node = null;1095 string m_Key = null;1096 public override JSONNodeType Tag { get { return JSONNodeType.None; } }1097 public override Enumerator GetEnumerator() { return new Enumerator(); }1098 public JSONLazyCreator(global::ThirdParty.SimpleJSON.JSONNode aNode)1099 {1100 m_Node = aNode;1101 m_Key = null;1102 }1103 public JSONLazyCreator(global::ThirdParty.SimpleJSON.JSONNode aNode, string aKey)1104 {1105 m_Node = aNode;1106 m_Key = aKey;1107 }1108 void Set(global::ThirdParty.SimpleJSON.JSONNode aVal)1109 {1110 if (m_Key == null)1111 {1112 m_Node.Add(aVal);1113 }1114 else1115 {1116 m_Node.Add(m_Key, aVal);1117 }1118 m_Node = null; // Be GC friendly.1119 }1120 public override global::ThirdParty.SimpleJSON.JSONNode this[int aIndex]1121 {1122 get1123 {1124 return new global::ThirdParty.SimpleJSON.JSONLazyCreator(this);1125 }1126 set1127 {1128 global::ThirdParty.SimpleJSON.JSONArray tmp = new global::ThirdParty.SimpleJSON.JSONArray();1129 tmp.Add(value);1130 Set(tmp);1131 }1132 }1133 public override global::ThirdParty.SimpleJSON.JSONNode this[string aKey]1134 {1135 get1136 {1137 return new global::ThirdParty.SimpleJSON.JSONLazyCreator(this, aKey);1138 }1139 set1140 {1141 global::ThirdParty.SimpleJSON.JSONObject tmp = new global::ThirdParty.SimpleJSON.JSONObject();1142 tmp.Add(aKey, value);1143 Set(tmp);1144 }1145 }1146 public override void Add(global::ThirdParty.SimpleJSON.JSONNode aItem)1147 {1148 global::ThirdParty.SimpleJSON.JSONArray tmp = new global::ThirdParty.SimpleJSON.JSONArray();1149 tmp.Add(aItem);1150 Set(tmp);1151 }1152 public override void Add(string aKey, global::ThirdParty.SimpleJSON.JSONNode aItem)1153 {1154 global::ThirdParty.SimpleJSON.JSONObject tmp = new global::ThirdParty.SimpleJSON.JSONObject();1155 tmp.Add(aKey, aItem);1156 Set(tmp);1157 }1158 public static bool operator ==(global::ThirdParty.SimpleJSON.JSONLazyCreator a, object b)1159 {1160 if (b == null)1161 return true;1162 return System.Object.ReferenceEquals(a, b);1163 }1164 public static bool operator !=(global::ThirdParty.SimpleJSON.JSONLazyCreator a, object b)1165 {1166 return !(a == b);1167 }1168 public override bool Equals(object obj)1169 {1170 if (obj == null)1171 return true;1172 return System.Object.ReferenceEquals(this, obj);1173 }1174 public override int GetHashCode()1175 {1176 return 0;1177 }1178 public override int AsInt1179 {1180 get1181 {1182 global::ThirdParty.SimpleJSON.JSONNumber tmp = new global::ThirdParty.SimpleJSON.JSONNumber(0);1183 Set(tmp);1184 return 0;1185 }1186 set1187 {1188 global::ThirdParty.SimpleJSON.JSONNumber tmp = new global::ThirdParty.SimpleJSON.JSONNumber(value);1189 Set(tmp);1190 }1191 }1192 public override float AsFloat1193 {1194 get1195 {1196 global::ThirdParty.SimpleJSON.JSONNumber tmp = new global::ThirdParty.SimpleJSON.JSONNumber(0.0f);1197 Set(tmp);1198 return 0.0f;1199 }1200 set1201 {1202 global::ThirdParty.SimpleJSON.JSONNumber tmp = new global::ThirdParty.SimpleJSON.JSONNumber(value);1203 Set(tmp);1204 }1205 }1206 public override double AsDouble1207 {1208 get1209 {1210 global::ThirdParty.SimpleJSON.JSONNumber tmp = new global::ThirdParty.SimpleJSON.JSONNumber(0.0);1211 Set(tmp);1212 return 0.0;1213 }1214 set1215 {1216 global::ThirdParty.SimpleJSON.JSONNumber tmp = new global::ThirdParty.SimpleJSON.JSONNumber(value);1217 Set(tmp);1218 }1219 }1220 public override long AsLong1221 {1222 get1223 {1224 if (longAsString)1225 Set(new global::ThirdParty.SimpleJSON.JSONString("0"));1226 else1227 Set(new global::ThirdParty.SimpleJSON.JSONNumber(0.0));1228 return 0L;1229 }1230 set1231 {1232 if (longAsString)1233 Set(new global::ThirdParty.SimpleJSON.JSONString(value.ToString()));1234 else1235 Set(new global::ThirdParty.SimpleJSON.JSONNumber(value));1236 }1237 }1238 public override bool AsBool1239 {1240 get1241 {1242 global::ThirdParty.SimpleJSON.JSONBool tmp = new global::ThirdParty.SimpleJSON.JSONBool(false);1243 Set(tmp);1244 return false;1245 }1246 set1247 {1248 global::ThirdParty.SimpleJSON.JSONBool tmp = new global::ThirdParty.SimpleJSON.JSONBool(value);1249 Set(tmp);1250 }1251 }1252 public override global::ThirdParty.SimpleJSON.JSONArray AsArray1253 {1254 get1255 {1256 global::ThirdParty.SimpleJSON.JSONArray tmp = new global::ThirdParty.SimpleJSON.JSONArray();1257 Set(tmp);1258 return tmp;1259 }1260 }1261 public override global::ThirdParty.SimpleJSON.JSONObject AsObject1262 {1263 get1264 {1265 global::ThirdParty.SimpleJSON.JSONObject tmp = new global::ThirdParty.SimpleJSON.JSONObject();1266 Set(tmp);1267 return tmp;1268 }1269 }1270 internal override void WriteToStringBuilder(StringBuilder aSB, int aIndent, int aIndentInc, JSONTextMode aMode)1271 {1272 aSB.Append("null");1273 }1274 }1275 // End of JSONLazyCreator1276 public static class JSON1277 {1278 public static global::ThirdParty.SimpleJSON.JSONNode Parse(string aJSON)1279 {1280 return global::ThirdParty.SimpleJSON.JSONNode.Parse(aJSON);1281 }1282 }1283}...

Full Screen

Full Screen

JSONNode.cs

Source:JSONNode.cs Github

copy

Full Screen

...29 set30 {31 }32 }33 public virtual JSONNode this[string aKey]34 {35 get => (JSONNode) null;36 set37 {38 }39 }40 public virtual string Value41 {42 get => "";43 set44 {45 }46 }47 public virtual int Count => 0;48 public virtual bool IsNumber => false;49 public virtual bool IsString => false;50 public virtual bool IsBoolean => false;51 public virtual bool IsNull => false;52 public virtual bool IsArray => false;53 public virtual bool IsObject => false;54 public virtual bool Inline55 {56 get => false;57 set58 {59 }60 }61 public virtual void Add(string aKey, JSONNode aItem)62 {63 }64 public virtual void Add(JSONNode aItem) => this.Add("", aItem);65 public virtual JSONNode Remove(string aKey) => (JSONNode) null;66 public virtual JSONNode Remove(int aIndex) => (JSONNode) null;67 public virtual JSONNode Remove(JSONNode aNode) => aNode;68 public virtual JSONNode Clone() => (JSONNode) null;69 public virtual IEnumerable<JSONNode> Children70 {71 get72 {73 yield break;74 }75 }76 public IEnumerable<JSONNode> DeepChildren77 {78 get79 {80 foreach (JSONNode child in this.Children)81 {82 foreach (JSONNode deepChild in child.DeepChildren)83 yield return deepChild;84 }85 }86 }87 public virtual bool HasKey(string aKey) => false;88 public virtual JSONNode GetValueOrDefault(string aKey, JSONNode aDefault) => aDefault;89 public override string ToString()90 {91 StringBuilder aSB = new StringBuilder();92 this.WriteToStringBuilder(aSB, 0, 0, JSONTextMode.Compact);93 return aSB.ToString();94 }95 public virtual string ToString(int aIndent)96 {97 StringBuilder aSB = new StringBuilder();98 this.WriteToStringBuilder(aSB, 0, aIndent, JSONTextMode.Indent);99 return aSB.ToString();100 }101 internal abstract void WriteToStringBuilder(102 StringBuilder aSB,103 int aIndent,104 int aIndentInc,105 JSONTextMode aMode);106 public abstract JSONNode.Enumerator GetEnumerator();107 public IEnumerable<KeyValuePair<string, JSONNode>> Linq => (IEnumerable<KeyValuePair<string, JSONNode>>) new JSONNode.LinqEnumerator(this);108 public JSONNode.KeyEnumerator Keys => new JSONNode.KeyEnumerator(this.GetEnumerator());109 public JSONNode.ValueEnumerator Values => new JSONNode.ValueEnumerator(this.GetEnumerator());110 public virtual double AsDouble111 {112 get113 {114 double result = 0.0;115 return double.TryParse(this.Value, NumberStyles.Float, (IFormatProvider) CultureInfo.InvariantCulture, out result) ? result : 0.0;116 }117 set => this.Value = value.ToString((IFormatProvider) CultureInfo.InvariantCulture);118 }119 public virtual int AsInt120 {121 get => (int) this.AsDouble;122 set => this.AsDouble = (double) value;123 }124 public virtual float AsFloat125 {126 get => (float) this.AsDouble;127 set => this.AsDouble = (double) value;128 }129 public virtual bool AsBool130 {131 get132 {133 bool result = false;134 return bool.TryParse(this.Value, out result) ? result : !string.IsNullOrEmpty(this.Value);135 }136 set => this.Value = value ? "true" : "false";137 }138 public virtual long AsLong139 {140 get141 {142 long result = 0;143 return long.TryParse(this.Value, out result) ? result : 0L;144 }145 set => this.Value = value.ToString();146 }147 public virtual JSONArray AsArray => this as JSONArray;148 public virtual JSONObject AsObject => this as JSONObject;149 public static implicit operator JSONNode(string s) => (JSONNode) new JSONString(s);150 public static implicit operator string(JSONNode d) => !(d == (object) null) ? d.Value : (string) null;151 public static implicit operator JSONNode(double n) => (JSONNode) new JSONNumber(n);152 public static implicit operator double(JSONNode d) => !(d == (object) null) ? d.AsDouble : 0.0;153 public static implicit operator JSONNode(float n) => (JSONNode) new JSONNumber((double) n);154 public static implicit operator float(JSONNode d) => !(d == (object) null) ? d.AsFloat : 0.0f;155 public static implicit operator JSONNode(int n) => (JSONNode) new JSONNumber((double) n);156 public static implicit operator int(JSONNode d) => !(d == (object) null) ? d.AsInt : 0;157 public static implicit operator JSONNode(long n) => JSONNode.longAsString ? (JSONNode) new JSONString(n.ToString()) : (JSONNode) new JSONNumber((double) n);158 public static implicit operator long(JSONNode d) => !(d == (object) null) ? d.AsLong : 0L;159 public static implicit operator JSONNode(bool b) => (JSONNode) new JSONBool(b);160 public static implicit operator bool(JSONNode d) => !(d == (object) null) && d.AsBool;161 public static implicit operator JSONNode(KeyValuePair<string, JSONNode> aKeyValue) => aKeyValue.Value;162 public static bool operator ==(JSONNode a, object b)163 {164 if ((object) a == b)165 return true;166 bool flag1 = a is JSONNull || (object) a == null || a is JSONLazyCreator;167 int num;168 switch (b)169 {170 case JSONNull _:171 case null:172 num = 1;173 break;174 default:175 num = b is JSONLazyCreator ? 1 : 0;176 break;177 }178 bool flag2 = num != 0;179 if (flag1 & flag2)180 return true;181 return !flag1 && a.Equals(b);182 }183 public static bool operator !=(JSONNode a, object b) => !(a == b);184 public override bool Equals(object obj) => (object) this == obj;185 public override int GetHashCode() => base.GetHashCode();186 internal static StringBuilder EscapeBuilder187 {188 get189 {190 if (JSONNode.m_EscapeBuilder == null)191 JSONNode.m_EscapeBuilder = new StringBuilder();192 return JSONNode.m_EscapeBuilder;193 }194 }195 internal static string Escape(string aText)196 {197 StringBuilder escapeBuilder = JSONNode.EscapeBuilder;198 escapeBuilder.Length = 0;199 if (escapeBuilder.Capacity < aText.Length + aText.Length / 10)200 escapeBuilder.Capacity = aText.Length + aText.Length / 10;201 foreach (char ch in aText)202 {203 switch (ch)204 {205 case '\b':206 escapeBuilder.Append("\\b");207 break;208 case '\t':209 escapeBuilder.Append("\\t");210 break;211 case '\n':212 escapeBuilder.Append("\\n");213 break;214 case '\f':215 escapeBuilder.Append("\\f");216 break;217 case '\r':218 escapeBuilder.Append("\\r");219 break;220 case '"':221 escapeBuilder.Append("\\\"");222 break;223 case '\\':224 escapeBuilder.Append("\\\\");225 break;226 default:227 if (ch < ' ' || JSONNode.forceASCII && ch > '\u007F')228 {229 ushort num = (ushort) ch;230 escapeBuilder.Append("\\u").Append(num.ToString("X4"));231 break;232 }233 escapeBuilder.Append(ch);234 break;235 }236 }237 string str = escapeBuilder.ToString();238 escapeBuilder.Length = 0;239 return str;240 }241 private static JSONNode ParseElement(string token, bool quoted)242 {243 if (quoted)244 return (JSONNode) token;245 string lower = token.ToLower();246 if (lower == "false" || lower == "true")247 return (JSONNode) (lower == "true");248 if (lower == "null")249 return (JSONNode) JSONNull.CreateOrGet();250 double result;251 return double.TryParse(token, NumberStyles.Float, (IFormatProvider) CultureInfo.InvariantCulture, out result) ? (JSONNode) result : (JSONNode) token;252 }253 public static JSONNode Parse(string aJSON)254 {255 Stack<JSONNode> jsonNodeStack = new Stack<JSONNode>();256 JSONNode jsonNode = (JSONNode) null;257 int index = 0;258 StringBuilder stringBuilder = new StringBuilder();259 string aKey = "";260 bool flag = false;261 bool quoted = false;262 for (; index < aJSON.Length; ++index)263 {264 switch (aJSON[index])265 {266 case '\t':267 case ' ':268 if (flag)269 {270 stringBuilder.Append(aJSON[index]);271 continue;272 }273 continue;274 case '\n':275 case '\r':276 case '\uFEFF':277 continue;278 case '"':279 flag = !flag;280 quoted |= flag;281 continue;282 case ',':283 if (flag)284 {285 stringBuilder.Append(aJSON[index]);286 continue;287 }288 if (stringBuilder.Length > 0 | quoted)289 jsonNode.Add(aKey, JSONNode.ParseElement(stringBuilder.ToString(), quoted));290 aKey = "";291 stringBuilder.Length = 0;292 quoted = false;293 continue;294 case '/':295 if (JSONNode.allowLineComments && !flag && index + 1 < aJSON.Length && aJSON[index + 1] == '/')296 {297 while (++index < aJSON.Length && aJSON[index] != '\n' && aJSON[index] != '\r')298 ;299 continue;300 }301 stringBuilder.Append(aJSON[index]);302 continue;303 case ':':304 if (flag)305 {306 stringBuilder.Append(aJSON[index]);307 continue;308 }309 aKey = stringBuilder.ToString();310 stringBuilder.Length = 0;311 quoted = false;312 continue;313 case '[':314 if (flag)315 {316 stringBuilder.Append(aJSON[index]);317 continue;318 }319 jsonNodeStack.Push((JSONNode) new JSONArray());320 if (jsonNode != (object) null)321 jsonNode.Add(aKey, jsonNodeStack.Peek());322 aKey = "";323 stringBuilder.Length = 0;324 jsonNode = jsonNodeStack.Peek();325 continue;326 case '\\':327 ++index;328 if (flag)329 {330 char ch = aJSON[index];331 switch (ch)332 {333 case 'b':334 stringBuilder.Append('\b');335 continue;336 case 'f':337 stringBuilder.Append('\f');338 continue;339 case 'n':340 stringBuilder.Append('\n');341 continue;342 case 'r':343 stringBuilder.Append('\r');344 continue;345 case 't':346 stringBuilder.Append('\t');347 continue;348 case 'u':349 string s = aJSON.Substring(index + 1, 4);350 stringBuilder.Append((char) int.Parse(s, NumberStyles.AllowHexSpecifier));351 index += 4;352 continue;353 default:354 stringBuilder.Append(ch);355 continue;356 }357 }358 else359 continue;360 case ']':361 case '}':362 if (flag)363 {364 stringBuilder.Append(aJSON[index]);365 continue;366 }367 if (jsonNodeStack.Count == 0)368 throw new Exception("JSON Parse: Too many closing brackets");369 jsonNodeStack.Pop();370 if (stringBuilder.Length > 0 | quoted)371 jsonNode.Add(aKey, JSONNode.ParseElement(stringBuilder.ToString(), quoted));372 quoted = false;373 aKey = "";374 stringBuilder.Length = 0;375 if (jsonNodeStack.Count > 0)376 {377 jsonNode = jsonNodeStack.Peek();378 continue;379 }380 continue;381 case '{':382 if (flag)383 {384 stringBuilder.Append(aJSON[index]);385 continue;386 }387 jsonNodeStack.Push((JSONNode) new JSONObject());388 if (jsonNode != (object) null)389 jsonNode.Add(aKey, jsonNodeStack.Peek());390 aKey = "";391 stringBuilder.Length = 0;392 jsonNode = jsonNodeStack.Peek();393 continue;394 default:395 stringBuilder.Append(aJSON[index]);396 continue;397 }398 }399 if (flag)400 throw new Exception("JSON Parse: Quotation marks seems to be messed up.");401 return jsonNode == (object) null ? JSONNode.ParseElement(stringBuilder.ToString(), quoted) : jsonNode;402 }403 public abstract void SerializeBinary(BinaryWriter aWriter);404 public void SaveToBinaryStream(Stream aData) => this.SerializeBinary(new BinaryWriter(aData));405 public void SaveToCompressedStream(Stream aData) => throw new Exception("Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON");406 public void SaveToCompressedFile(string aFileName) => throw new Exception("Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON");407 public string SaveToCompressedBase64() => throw new Exception("Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON");408 public void SaveToBinaryFile(string aFileName)409 {410 Directory.CreateDirectory(new FileInfo(aFileName).Directory.FullName);411 using (FileStream aData = File.OpenWrite(aFileName))412 this.SaveToBinaryStream((Stream) aData);413 }414 public string SaveToBinaryBase64()415 {416 using (MemoryStream aData = new MemoryStream())417 {418 this.SaveToBinaryStream((Stream) aData);419 aData.Position = 0L;420 return Convert.ToBase64String(aData.ToArray());421 }422 }423 public static JSONNode DeserializeBinary(BinaryReader aReader)424 {425 JSONNodeType jsonNodeType = (JSONNodeType) aReader.ReadByte();426 switch (jsonNodeType)427 {428 case JSONNodeType.Array:429 int num1 = aReader.ReadInt32();430 JSONArray jsonArray = new JSONArray();431 for (int index = 0; index < num1; ++index)432 jsonArray.Add(JSONNode.DeserializeBinary(aReader));433 return (JSONNode) jsonArray;434 case JSONNodeType.Object:435 int num2 = aReader.ReadInt32();436 JSONObject jsonObject = new JSONObject();437 for (int index = 0; index < num2; ++index)438 {439 string aKey = aReader.ReadString();440 JSONNode aItem = JSONNode.DeserializeBinary(aReader);441 jsonObject.Add(aKey, aItem);442 }443 return (JSONNode) jsonObject;444 case JSONNodeType.String:445 return (JSONNode) new JSONString(aReader.ReadString());446 case JSONNodeType.Number:447 return (JSONNode) new JSONNumber(aReader.ReadDouble());448 case JSONNodeType.NullValue:449 return (JSONNode) JSONNull.CreateOrGet();450 case JSONNodeType.Boolean:451 return (JSONNode) new JSONBool(aReader.ReadBoolean());452 default:453 throw new Exception("Error deserializing JSON. Unknown tag: " + (object) jsonNodeType);454 }455 }456 public static JSONNode LoadFromCompressedFile(string aFileName) => throw new Exception("Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON");457 public static JSONNode LoadFromCompressedStream(Stream aData) => throw new Exception("Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON");458 public static JSONNode LoadFromCompressedBase64(string aBase64) => throw new Exception("Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON");459 public static JSONNode LoadFromBinaryStream(Stream aData)460 {461 using (BinaryReader aReader = new BinaryReader(aData))462 return JSONNode.DeserializeBinary(aReader);463 }464 public static JSONNode LoadFromBinaryFile(string aFileName)465 {466 using (FileStream aData = File.OpenRead(aFileName))467 return JSONNode.LoadFromBinaryStream((Stream) aData);468 }469 public static JSONNode LoadFromBinaryBase64(string aBase64)470 {471 MemoryStream aData = new MemoryStream(Convert.FromBase64String(aBase64));472 aData.Position = 0L;473 return JSONNode.LoadFromBinaryStream((Stream) aData);474 }475 private static JSONNode GetContainer(JSONContainerType aType) => aType == JSONContainerType.Array ? (JSONNode) new JSONArray() : (JSONNode) new JSONObject();476 public static implicit operator JSONNode(Vector2 aVec)477 {478 JSONNode container = JSONNode.GetContainer(JSONNode.VectorContainerType);479 container.WriteVector2(aVec);480 return container;481 }482 public static implicit operator JSONNode(Vector3 aVec)483 {484 JSONNode container = JSONNode.GetContainer(JSONNode.VectorContainerType);485 container.WriteVector3(aVec);486 return container;487 }488 public static implicit operator JSONNode(Vector4 aVec)489 {490 JSONNode container = JSONNode.GetContainer(JSONNode.VectorContainerType);491 container.WriteVector4(aVec);492 return container;493 }494 public static implicit operator JSONNode(Quaternion aRot)495 {496 JSONNode container = JSONNode.GetContainer(JSONNode.QuaternionContainerType);497 container.WriteQuaternion(aRot);498 return container;499 }500 public static implicit operator JSONNode(Rect aRect)501 {502 JSONNode container = JSONNode.GetContainer(JSONNode.RectContainerType);503 container.WriteRect(aRect);504 return container;505 }506 public static implicit operator JSONNode(RectOffset aRect)507 {508 JSONNode container = JSONNode.GetContainer(JSONNode.RectContainerType);509 container.WriteRectOffset(aRect);510 return container;511 }512 public static implicit operator Vector2(JSONNode aNode) => aNode.ReadVector2();513 public static implicit operator Vector3(JSONNode aNode) => aNode.ReadVector3();514 public static implicit operator Vector4(JSONNode aNode) => aNode.ReadVector4();515 public static implicit operator Quaternion(JSONNode aNode) => aNode.ReadQuaternion();516 public static implicit operator Rect(JSONNode aNode) => aNode.ReadRect();517 public static implicit operator RectOffset(JSONNode aNode) => aNode.ReadRectOffset();518 public Vector2 ReadVector2(Vector2 aDefault)519 {520 if (this.IsObject)521 return new Vector2(this["x"].AsFloat, this["y"].AsFloat);522 return this.IsArray ? new Vector2(this[0].AsFloat, this[1].AsFloat) : aDefault;523 }524 public Vector2 ReadVector2(string aXName, string aYName) => this.IsObject ? new Vector2(this[aXName].AsFloat, this[aYName].AsFloat) : Vector2.zero;525 public Vector2 ReadVector2() => this.ReadVector2(Vector2.zero);526 public JSONNode WriteVector2(Vector2 aVec, string aXName = "x", string aYName = "y")527 {528 if (this.IsObject)529 {530 this.Inline = true;531 this[aXName].AsFloat = aVec.x;532 this[aYName].AsFloat = aVec.y;533 }534 else if (this.IsArray)535 {536 this.Inline = true;537 this[0].AsFloat = aVec.x;538 this[1].AsFloat = aVec.y;539 }540 return this;541 }542 public Vector3 ReadVector3(Vector3 aDefault)543 {544 if (this.IsObject)545 return new Vector3(this["x"].AsFloat, this["y"].AsFloat, this["z"].AsFloat);546 return this.IsArray ? new Vector3(this[0].AsFloat, this[1].AsFloat, this[2].AsFloat) : aDefault;547 }548 public Vector3 ReadVector3(string aXName, string aYName, string aZName) => this.IsObject ? new Vector3(this[aXName].AsFloat, this[aYName].AsFloat, this[aZName].AsFloat) : Vector3.zero;549 public Vector3 ReadVector3() => this.ReadVector3(Vector3.zero);550 public JSONNode WriteVector3(551 Vector3 aVec,552 string aXName = "x",553 string aYName = "y",554 string aZName = "z")555 {556 if (this.IsObject)557 {558 this.Inline = true;559 this[aXName].AsFloat = aVec.x;560 this[aYName].AsFloat = aVec.y;561 this[aZName].AsFloat = aVec.z;562 }563 else if (this.IsArray)564 {565 this.Inline = true;566 this[0].AsFloat = aVec.x;567 this[1].AsFloat = aVec.y;568 this[2].AsFloat = aVec.z;569 }570 return this;571 }572 public Vector4 ReadVector4(Vector4 aDefault)573 {574 if (this.IsObject)575 return new Vector4(this["x"].AsFloat, this["y"].AsFloat, this["z"].AsFloat, this["w"].AsFloat);576 return this.IsArray ? new Vector4(this[0].AsFloat, this[1].AsFloat, this[2].AsFloat, this[3].AsFloat) : aDefault;577 }578 public Vector4 ReadVector4() => this.ReadVector4(Vector4.zero);579 public JSONNode WriteVector4(Vector4 aVec)580 {581 if (this.IsObject)582 {583 this.Inline = true;584 this["x"].AsFloat = aVec.x;585 this["y"].AsFloat = aVec.y;586 this["z"].AsFloat = aVec.z;587 this["w"].AsFloat = aVec.w;588 }589 else if (this.IsArray)590 {591 this.Inline = true;592 this[0].AsFloat = aVec.x;593 this[1].AsFloat = aVec.y;594 this[2].AsFloat = aVec.z;595 this[3].AsFloat = aVec.w;596 }597 return this;598 }599 public Quaternion ReadQuaternion(Quaternion aDefault)600 {601 if (this.IsObject)602 return new Quaternion(this["x"].AsFloat, this["y"].AsFloat, this["z"].AsFloat, this["w"].AsFloat);603 return this.IsArray ? new Quaternion(this[0].AsFloat, this[1].AsFloat, this[2].AsFloat, this[3].AsFloat) : aDefault;604 }605 public Quaternion ReadQuaternion() => this.ReadQuaternion(Quaternion.identity);606 public JSONNode WriteQuaternion(Quaternion aRot)607 {608 if (this.IsObject)609 {610 this.Inline = true;611 this["x"].AsFloat = aRot.x;612 this["y"].AsFloat = aRot.y;613 this["z"].AsFloat = aRot.z;614 this["w"].AsFloat = aRot.w;615 }616 else if (this.IsArray)617 {618 this.Inline = true;619 this[0].AsFloat = aRot.x;620 this[1].AsFloat = aRot.y;621 this[2].AsFloat = aRot.z;622 this[3].AsFloat = aRot.w;623 }624 return this;625 }626 public Rect ReadRect(Rect aDefault)627 {628 if (this.IsObject)629 return new Rect(this["x"].AsFloat, this["y"].AsFloat, this["width"].AsFloat, this["height"].AsFloat);630 return this.IsArray ? new Rect(this[0].AsFloat, this[1].AsFloat, this[2].AsFloat, this[3].AsFloat) : aDefault;631 }632 public Rect ReadRect() => this.ReadRect(new Rect());633 public JSONNode WriteRect(Rect aRect)634 {635 if (this.IsObject)636 {637 this.Inline = true;638 this["x"].AsFloat = aRect.x;639 this["y"].AsFloat = aRect.y;640 this["width"].AsFloat = aRect.width;641 this["height"].AsFloat = aRect.height;642 }643 else if (this.IsArray)644 {645 this.Inline = true;646 this[0].AsFloat = aRect.x;647 this[1].AsFloat = aRect.y;648 this[2].AsFloat = aRect.width;649 this[3].AsFloat = aRect.height;650 }651 return this;652 }653 public RectOffset ReadRectOffset(RectOffset aDefault)654 {655 switch (this)656 {657 case JSONObject _:658 return new RectOffset(this["left"].AsInt, this["right"].AsInt, this["top"].AsInt, this["bottom"].AsInt);659 case JSONArray _:660 return new RectOffset(this[0].AsInt, this[1].AsInt, this[2].AsInt, this[3].AsInt);661 default:662 return aDefault;663 }664 }665 public RectOffset ReadRectOffset() => this.ReadRectOffset(new RectOffset());666 public JSONNode WriteRectOffset(RectOffset aRect)667 {668 if (this.IsObject)669 {670 this.Inline = true;671 this["left"].AsInt = aRect.left;672 this["right"].AsInt = aRect.right;673 this["top"].AsInt = aRect.top;674 this["bottom"].AsInt = aRect.bottom;675 }676 else if (this.IsArray)677 {678 this.Inline = true;679 this[0].AsInt = aRect.left;680 this[1].AsInt = aRect.right;681 this[2].AsInt = aRect.top;682 this[3].AsInt = aRect.bottom;683 }684 return this;685 }686 public Matrix4x4 ReadMatrix()687 {688 Matrix4x4 identity = Matrix4x4.identity;689 if (this.IsArray)690 {691 for (int index = 0; index < 16; ++index)692 identity[index] = this[index].AsFloat;693 }694 return identity;695 }696 public JSONNode WriteMatrix(Matrix4x4 aMatrix)697 {698 if (this.IsArray)699 {700 this.Inline = true;701 for (int index = 0; index < 16; ++index)702 this[index].AsFloat = aMatrix[index];703 }704 return this;705 }706 public struct Enumerator707 {708 private JSONNode.Enumerator.Type type;709 private Dictionary<string, JSONNode>.Enumerator m_Object;710 private List<JSONNode>.Enumerator m_Array;711 public bool IsValid => (uint) this.type > 0U;712 public Enumerator(List<JSONNode>.Enumerator aArrayEnum)713 {714 this.type = JSONNode.Enumerator.Type.Array;715 this.m_Object = new Dictionary<string, JSONNode>.Enumerator();716 this.m_Array = aArrayEnum;717 }718 public Enumerator(Dictionary<string, JSONNode>.Enumerator aDictEnum)719 {720 this.type = JSONNode.Enumerator.Type.Object;721 this.m_Object = aDictEnum;722 this.m_Array = new List<JSONNode>.Enumerator();723 }724 public KeyValuePair<string, JSONNode> Current725 {726 get727 {728 if (this.type == JSONNode.Enumerator.Type.Array)729 return new KeyValuePair<string, JSONNode>(string.Empty, this.m_Array.Current);730 return this.type == JSONNode.Enumerator.Type.Object ? this.m_Object.Current : new KeyValuePair<string, JSONNode>(string.Empty, (JSONNode) null);731 }732 }733 public bool MoveNext()734 {735 if (this.type == JSONNode.Enumerator.Type.Array)736 return this.m_Array.MoveNext();737 return this.type == JSONNode.Enumerator.Type.Object && this.m_Object.MoveNext();738 }739 private enum Type740 {741 None,742 Array,743 Object,744 }745 }746 public struct ValueEnumerator747 {748 private JSONNode.Enumerator m_Enumerator;749 public ValueEnumerator(List<JSONNode>.Enumerator aArrayEnum)750 : this(new JSONNode.Enumerator(aArrayEnum))751 {752 }753 public ValueEnumerator(Dictionary<string, JSONNode>.Enumerator aDictEnum)754 : this(new JSONNode.Enumerator(aDictEnum))755 {756 }757 public ValueEnumerator(JSONNode.Enumerator aEnumerator) => this.m_Enumerator = aEnumerator;758 public JSONNode Current => this.m_Enumerator.Current.Value;759 public bool MoveNext() => this.m_Enumerator.MoveNext();760 public JSONNode.ValueEnumerator GetEnumerator() => this;761 }762 public struct KeyEnumerator763 {764 private JSONNode.Enumerator m_Enumerator;765 public KeyEnumerator(List<JSONNode>.Enumerator aArrayEnum)766 : this(new JSONNode.Enumerator(aArrayEnum))767 {768 }769 public KeyEnumerator(Dictionary<string, JSONNode>.Enumerator aDictEnum)770 : this(new JSONNode.Enumerator(aDictEnum))771 {772 }773 public KeyEnumerator(JSONNode.Enumerator aEnumerator) => this.m_Enumerator = aEnumerator;774 public string Current => this.m_Enumerator.Current.Key;775 public bool MoveNext() => this.m_Enumerator.MoveNext();776 public JSONNode.KeyEnumerator GetEnumerator() => this;777 }778 public class LinqEnumerator : 779 IEnumerator<KeyValuePair<string, JSONNode>>,780 IEnumerator,781 IDisposable,782 IEnumerable<KeyValuePair<string, JSONNode>>,783 IEnumerable784 {785 private JSONNode m_Node;786 private JSONNode.Enumerator m_Enumerator;787 internal LinqEnumerator(JSONNode aNode)788 {789 this.m_Node = aNode;790 if (!(this.m_Node != (object) null))791 return;792 this.m_Enumerator = this.m_Node.GetEnumerator();793 }794 public KeyValuePair<string, JSONNode> Current => this.m_Enumerator.Current;795 object IEnumerator.Current => (object) this.m_Enumerator.Current;796 public bool MoveNext() => this.m_Enumerator.MoveNext();797 public void Dispose()798 {799 this.m_Node = (JSONNode) null;800 this.m_Enumerator = new JSONNode.Enumerator();801 }802 public IEnumerator<KeyValuePair<string, JSONNode>> GetEnumerator() => (IEnumerator<KeyValuePair<string, JSONNode>>) new JSONNode.LinqEnumerator(this.m_Node);803 public void Reset()804 {805 if (!(this.m_Node != (object) null))806 return;807 this.m_Enumerator = this.m_Node.GetEnumerator();808 }809 IEnumerator IEnumerable.GetEnumerator() => (IEnumerator) new JSONNode.LinqEnumerator(this.m_Node);810 }811 }812}...

Full Screen

Full Screen

string

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using SimpleJSON;7{8 {9 static void Main(string[] args)10 {11 string json = @"{""name"":""John Smith"",""age"":33,""address"":{""streetAddress"":""21 2nd Street"",""city"":""New York"",""state"":""NY"",""postalCode"":""10021-3100""},""phoneNumbers"":[{""type"":""home"",""number"":""212 555-1234""},{""type"":""fax"",""number"":""646 555-4567""}]}";12 var N = JSON.Parse(json);13 var S = N["phoneNumbers"].ToString();14 Console.WriteLine(S);15 Console.ReadLine();16 }17 }18}19[{"type":"home","number":"212 555-1234"},{"type":"fax","number":"646 555-4567"}]

Full Screen

Full Screen

string

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using SimpleJSON;7{8 {9 static void Main(string[] args)10 {11 var obj = new JObject();12 obj["name"] = "John";13 obj["age"] = 30;14 obj["isAlive"] = true;15 obj["address"] = null;16 obj["phoneNumbers"] = new JArray(1234567890, 9876543210);17 obj["children"] = new JArray("Sara", "Alex", "Jack");18 obj["spouse"] = null;19 var obj2 = new JObject();20 obj2["name"] = "Mary";21 obj2["age"] = 25;22 obj2["isAlive"] = true;23 obj2["address"] = null;24 obj2["phoneNumbers"] = new JArray(1234567890, 9876543210);25 obj2["children"] = new JArray("Sara", "Alex", "Jack");26 obj2["spouse"] = null;27 var obj3 = new JObject();28 obj3["name"] = "Peter";29 obj3["age"] = 35;30 obj3["isAlive"] = true;31 obj3["address"] = null;32 obj3["phoneNumbers"] = new JArray(1234567890, 9876543210);33 obj3["children"] = new JArray("Sara", "Alex", "Jack");34 obj3["spouse"] = null;35 var obj4 = new JObject();36 obj4["name"] = "Joseph";37 obj4["age"] = 40;38 obj4["isAlive"] = true;39 obj4["address"] = null;40 obj4["phoneNumbers"] = new JArray(1234567890, 9876543210);41 obj4["children"] = new JArray("Sara", "Alex", "Jack");42 obj4["spouse"] = null;43 var obj5 = new JObject();44 obj5["name"] = "David";45 obj5["age"] = 45;46 obj5["isAlive"] = true;47 obj5["address"] = null;48 obj5["phoneNumbers"] = new JArray(

Full Screen

Full Screen

string

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using SimpleJSON;7{8 {9 static void Main(string[] args)10 {11 string json = @"{12 {13 },14 {15 }16 }";17 var N = JSON.Parse(json);18 var name = N["name"];19 Console.WriteLine(name);20 Console.ReadKey();

Full Screen

Full Screen

string

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using SimpleJSON;7{8 {9 static void Main(string[] args)10 {11 string json = @"{""name"":""John Smith"",""age"":33,""address"":{""street"":""21 2nd Street"",""city"":""New York"",""state"":""NY"",""postalCode"":""10021-3100""},""phoneNumbers"":[{""type"":""home"",""number"":""212 555-1234""},{""type"":""fax"",""number"":""646 555-4567""}]}";12 var N = JSON.Parse(json);13 var A = N["phoneNumbers"];14 var B = A[0];15 var C = B["number"];16 Console.WriteLine(C);17 Console.Read();18 }19 }20}21using System;22using System.Collections.Generic;23using System.Linq;24using System.Text;25using System.Threading.Tasks;26using SimpleJSON;27{28 {29 static void Main(string[] args)30 {31 string json = @"{""name"":""John Smith"",""age"":33,""address"":{""street"":""21 2nd Street"",""city"":""New York"",""state"":""NY"",""postalCode"":""10021-3100""},""phoneNumbers"":[{""type"":""home"",""number"":""212 555-1234""},{""type"":""fax"",""number"":""646 555-4567""}]}";32 var N = JSON.Parse(json);33 var A = N["phoneNumbers"];34 var B = A[0];35 var C = B["number"];36 Console.WriteLine(C);37 Console.Read();38 }39 }40}

Full Screen

Full Screen

string

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using SimpleJSON;7{8 {9 static void Main(string[] args)10 {11 string json = @"{12 ""hometown"": {13 },14}";15 var N = JSON.Parse(json);16 var children = N["children"];17 var enumerator = children.GetEnumerator();18 while (enumerator.MoveNext())19 {20 Console.WriteLine(enumerator.Current);21 }22 Console.ReadLine();23 }24 }25}26using System;27using System.Collections.Generic;28using System.Linq;29using System.Text;30using System.Threading.Tasks;31using SimpleJSON;32{33 {34 static void Main(string[] args)35 {36 string json = @"{37 ""hometown"": {38 },39}";40 var N = JSON.Parse(json);41 var children = N["children"];42 foreach (var child in children)43 {44 Console.WriteLine(child);45 }46 Console.ReadLine();47 }48 }49}50using System;51using System.Collections.Generic;52using System.Linq;53using System.Text;54using System.Threading.Tasks;55using SimpleJSON;56{57 {58 static void Main(string[] args)59 {60 string json = @"{61 ""hometown"": {62 },63}";64 var N = JSON.Parse(json);65 var children = N["children"];

Full Screen

Full Screen

string

Using AI Code Generation

copy

Full Screen

1using SimpleJSON;2using System.Collections;3using System.Collections.Generic;4using UnityEngine;5{6 void Start()7 {8 string jsonString = "{ \"name\": \"John\", \"age\": 30 }";9 var N = JSON.Parse(jsonString);10 var enumerator = N.GetEnumerator();11 while (enumerator.MoveNext())12 {13 Debug.Log(enumerator.Current.Key + " : " + enumerator.Current.Value);14 }15 }16 void Update()17 {18 }19}20using SimpleJSON;21using System.Collections;22using System.Collections.Generic;23using UnityEngine;24{25 void Start()26 {27 string jsonString = "{ \"name\": \"John\", \"age\": 30 }";28 var N = JSON.Parse(jsonString);29 var enumerator = N.GetEnumerator();30 foreach (var item in N)31 {32 Debug.Log(item.Key + " : " + item.Value);33 }34 }35 void Update()36 {37 }38}39using SimpleJSON;40using System.Collections;41using System.Collections.Generic;42using UnityEngine;43{44 void Start()45 {46 string jsonString = "{ \"name\": \"John\", \"age\": 30 }";47 var N = JSON.Parse(jsonString);48 var enumerator = N.GetEnumerator();49 foreach (var item in N)50 {51 Debug.Log(item.Key + " : " + item.Value);52 }53 }54 void Update()

Full Screen

Full Screen

string

Using AI Code Generation

copy

Full Screen

1using System;2using SimpleJSON;3{4 public static void Main()5 {6 string jsonString = @"{""name"": ""John"", ""age"": 30, ""cars"": [""Ford"", ""BMW"", ""Fiat""]}";7 var json = JSON.Parse(jsonString);8 JToken token = json["cars"];9 foreach (var item in token)10 {11 Console.WriteLine(item);12 }13 }14}

Full Screen

Full Screen

string

Using AI Code Generation

copy

Full Screen

1using UnityEngine;2using System.Collections;3using SimpleJSON;4public class LinqEnumerator : MonoBehaviour {5 string jsonString = @"{""name"":""John"",""age"":30,""cars"":[{""name"":""Ford"",""models"":[{""name"":""Fiesta""},{""name"":""Focus""},{""name"":""Mustang""}]},{""name"":""BMW"",""models"":[{""name"":""320""},{""name"":""X3""},{""name"":""X5""}]},{""name"":""Fiat"",""models"":[{""name"":""500""},{""name"":""Panda""}]}]}";6 void Start()7 {8 var N = JSON.Parse(jsonString);9 var cars = N["cars"].LinqEnumerator();10 foreach (var car in cars)11 {12 Debug.Log(car["name"]);13 var models = car["models"].LinqEnumerator();14 foreach (var model in models)15 {16 Debug.Log(model["name"]);17 }18 }19 }20}21using UnityEngine;22using System.Collections;23using SimpleJSON;24public class LinqEnumerator : MonoBehaviour {25 string jsonString = @"{""name"":""John"",""age"":30,""cars"":[{""name"":""Ford"",""models"":[{""name"":""Fiesta""},{""name"":""Focus""},{""name"":""Mustang""}]},{""name"":""BMW"",""models"":[{""name"":""320""},{""name"":""X3""},{""name"":""X5""}]},{""name"":""Fiat"",""models"":[{""name"":""500""},{""name"":""Panda""}]}]}";26 void Start()27 {28 var N = JSON.Parse(jsonString);29 var cars = N["cars"].LinqEnumerator();30 foreach (var car in cars)31 {32 Debug.Log(car["name"]);33 var models = car["models"].LinqEnumerator();34 foreach (var model in models)35 {36 Debug.Log(model["name"]);37 }38 }39 }40}41using UnityEngine;42using System.Collections;

Full Screen

Full Screen

string

Using AI Code Generation

copy

Full Screen

1using System;2using SimpleJSON;3{4 static void Main()5 {6 string jsonString = "{\"name\":\"Pragim\",\"age\":30}";7 var json = JSON.Parse(jsonString);8 foreach (var item in json)9 {10 Console.WriteLine("{0} : {1}", item.Key, item.Value);11 }12 }13}

Full Screen

Full Screen

string

Using AI Code Generation

copy

Full Screen

1using System;2using SimpleJSON;3{4 static void Main()5 {6 string json = @"{""name"":""John"",""age"":30,""cars"":[""Ford"",""BMW"",""Fiat""]}";7 string name = JSON.Parse(json).Linq["name"];8 Console.WriteLine(name);9 }10}11using System;12using SimpleJSON;13{14 static void Main()15 {16 string json = @"{""name"":""John"",""age"":30,""cars"":[""Ford"",""BMW"",""Fiat""]}";17 string name = JSON.Parse(json).Linq["name"];18 Console.WriteLine(name);19 }20}21using System;22using SimpleJSON;23{24 static void Main()25 {26 string json = @"{""name"":""John"",""age"":30,""cars"":[""Ford"",""BMW"",""Fiat""]}";27 string name = JSON.Parse(json).Linq["name"];28 Console.WriteLine(name);29 }30}31using System;32using SimpleJSON;33{34 static void Main()35 {36 string json = @"{""name"":""John"",""age"":30,""cars"":[""Ford"",""BMW"",""Fiat""]}";37 string name = JSON.Parse(json).Linq["name"];38 Console.WriteLine(name);39 }40}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful