Best Vstest code snippet using SimpleJSON.LinqEnumerator
SimpleJSON.cs
Source:SimpleJSON.cs
...56 * 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 set...
JSONNode.cs
Source:JSONNode.cs
...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}...
LinqEnumerator
Using AI Code Generation
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 {12 'address': {13 },14 },15 {16 'address': {17 },18 },19 {20 'address': {21 },22 }23]";24 var jArray = JSON.Parse(json);25 var enumerator = jArray.GetEnumerator();26 while (enumerator.MoveNext())27 {28 var jObject = enumerator.Current;29 var name = jObject["name"].Value;30 var age = jObject["age"].Value;31 var address = jObject["address"];32 var street = address["street"].Value;33 var city = address["city"].Value;34 var state = address["state"].Value;35 var zip = address["zip"].Value;36 var phoneNumbers = jObject["phoneNumbers"];
LinqEnumerator
Using AI Code Generation
1using SimpleJSON;2using System.Collections;3using System.Collections.Generic;4using UnityEngine;5using UnityEngine.UI;6{7 public Text text;8 void Start()9 {10 var jsonString = Resources.Load<TextAsset>("JSON").text;11 var N = JSON.Parse(jsonString);12 select item;13 foreach(var item in result)14 {15 text.text = item["name"];16 }17 }18}19using SimpleJSON;20using System.Collections;21using System.Collections.Generic;22using UnityEngine;23using UnityEngine.UI;24{25 public Text text;26 void Start()27 {28 var jsonString = Resources.Load<TextAsset>("JSON").text;29 var N = JSON.Parse(jsonString);30 select item;31 foreach (var item in result)32 {33 text.text = item["name"];34 }35 }36}37using SimpleJSON;38using System.Collections;39using System.Collections.Generic;40using UnityEngine;41using UnityEngine.UI;42{43 public Text text;44 void Start()45 {46 var jsonString = Resources.Load<TextAsset>("JSON").text;47 var N = JSON.Parse(jsonString);48 select item;49 foreach (var item in result)50 {51 text.text = item["name"];52 }53 }54}
LinqEnumerator
Using AI Code Generation
1using System.Linq;2using SimpleJSON;3using System.Collections.Generic;4using System.IO;5using UnityEngine;6{7 void Start()8 {9 string json = File.ReadAllText(Application.dataPath + "/StreamingAssets/1.json");10 var N = JSON.Parse(json);11 var arr = N["data"].AsArray;12 var result = arr.Where(a => a["id"] == "1");13 foreach (var item in result)14 {15 Debug.Log(item["name"]);16 }17 }18}19{20 {21 },22 {23 },24 {25 }26}27Where() – It is used to
LinqEnumerator
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using SimpleJSON;5using System.IO;6{7 public static void Main()8 {9 string json = @"{10 'Address': {11 },12 }";13 var N = JSON.Parse(json);14 var list = new List<JSONNode>();15 foreach (var item in N)16 {17 list.Add(item);18 }19 Console.WriteLine(N);20 Console.WriteLine(list);21 Console.WriteLine(list.First());22 Console.WriteLine(list.Last());23 Console.WriteLine(list.Count());24 Console.WriteLine(list.Count);25 Console.WriteLine(list.First());26 Console.WriteLine(list.Last());27 Console.WriteLine(list[0]);28 Console.WriteLine(list[1]);29 Console.WriteLine(list[2]);30 Console.WriteLine(list[3]);31 Console.WriteLine(list[4]);32 Console.WriteLine(list[5]);33 Console.WriteLine(list[6]);
LinqEnumerator
Using AI Code Generation
1using System.Collections;2using System.Collections.Generic;3using UnityEngine;4using SimpleJSON;5public class LinqEnumerator : MonoBehaviour {6 public string jsonString;7 void Start () {8 jsonString = "[{\"name\":\"john\",\"age\":30},{\"name\":\"mary\",\"age\":25},{\"name\":\"peter\",\"age\":27}]";9 var N = JSON.Parse(jsonString);10 select p;11 foreach (var p in people)12 {13 Debug.Log(p);14 }15 }16}17using System.Collections;18using System.Collections.Generic;19using UnityEngine;20using SimpleJSON;21public class LinqEnumerator : MonoBehaviour {22 public string jsonString;23 void Start () {24 jsonString = "[{\"name\":\"john\",\"age\":30},{\"name\":\"mary\",\"age\":25},{\"name\":\"peter\",\"age\":27}]";25 var N = JSON.Parse(jsonString);26 select p;27 foreach (var p in people)28 {29 Debug.Log(p);30 }31 }32}33using System.Collections;34using System.Collections.Generic;35using UnityEngine;36using SimpleJSON;37public class LinqEnumerator : MonoBehaviour {38 public string jsonString;39 void Start () {40 jsonString = "[{\"name\":\"john\",\"age\":30},{\"name\":\"mary\",\"age\":25},{\"name\":\"peter\",\"age\":27}]";41 var N = JSON.Parse(jsonString);42 select p;43 foreach (var p in people)44 {45 Debug.Log(p);46 }47 }48}49using System.Collections;50using System.Collections.Generic;51using UnityEngine;52using SimpleJSON;53public class LinqEnumerator : MonoBehaviour {54 public string jsonString;55 void Start () {56 jsonString = "[{\"name\":\"john\",\"age\":30},{\"name\":\"mary\",\"age
LinqEnumerator
Using AI Code Generation
1using UnityEngine;2using System.Collections;3using System.Collections.Generic;4using System.Linq;5using SimpleJSON;6public class test : MonoBehaviour {7 string jsonString = @"{8 {9 },10 {11 }12}";13 void Start()14 {15 var N = JSON.Parse(jsonString);16 var result = LinqEnumerator(N["array"]);17 foreach (var item in result)18 {19 Debug.Log(item["name"] + ", " + item["age"]);20 }21 }22 public IEnumerable<JSONNode> LinqEnumerator(JSONNode N)23 {24 for (int i = 0; i < N.Count; i++)25 {26 yield return N[i];27 }28 }29}30using UnityEngine;31using System.Collections;32using System.Collections.Generic;33using System.Linq;34using SimpleJSON;35public class test : MonoBehaviour {36 string jsonString = @"{37 {38 },39 {40 }41}";42 void Start()43 {44 var N = JSON.Parse(jsonString);45 var result = LinqEnumerator(N["array"]);46 foreach (var item in result)47 {48 Debug.Log(item["name"] + ", " + item["age"]);49 }50 }51 public IEnumerable<JSONNode> LinqEnumerator(JSONNode N)52 {53 for (int i = 0; i < N.Count; i++)54 {55 yield return N[i];56 }57 }58}
LinqEnumerator
Using AI Code Generation
1using UnityEngine;2using System.Collections;3using System.Collections.Generic;4using SimpleJSON;5public class LinqEnumeratorTest : MonoBehaviour {6 void Start() {7 var json = JSON.Parse(@"{8 }");9 var cars = json["cars"].AsEnumerable;10 foreach (var car in cars) {11 Debug.Log(car.Value);12 }13 }14}15using UnityEngine;16using System.Collections;17using System.Collections.Generic;18using SimpleJSON;19public class LinqEnumeratorTest : MonoBehaviour {20 void Start() {21 var json = JSON.Parse(@"{22 }");23 var cars = json["cars"].AsEnumerable;24 foreach (var car in cars) {25 Debug.Log(car.Value);26 }27 }28}29using UnityEngine;30using System.Collections;31using System.Collections.Generic;32using SimpleJSON;33public class LinqEnumeratorTest : MonoBehaviour {34 void Start() {35 var json = JSON.Parse(@"{36 }");37 var cars = json["cars"].AsEnumerable;38 foreach (var car in cars) {39 Debug.Log(car.Value);40 }41 }42}43using UnityEngine;44using System.Collections;45using System.Collections.Generic;46using SimpleJSON;47public class LinqEnumeratorTest : MonoBehaviour {48 void Start() {49 var json = JSON.Parse(@"{50 }");51 var cars = json["cars"].AsEnumerable;52 foreach (var car in cars) {53 Debug.Log(car.Value);54 }55 }56}
LinqEnumerator
Using AI Code Generation
1using UnityEngine;2using System.Collections;3using SimpleJSON;4public class LinqTest : MonoBehaviour {5 void Start () {6 string jsonString = "{ \"array\": [ { \"name\": \"apples\", \"count\": 7 }, { \"name\": \"oranges\", \"count\": 3 } ] }";7 var N = JSON.Parse(jsonString);8 var array = N["array"].AsArray;9 foreach (var item in array) {10 Debug.Log(item["name"]);11 }12 }13}14string jsonString = "{ \"array\": [ { \"name\": \"apples\", \"count\": 7 }, { \"name\": \"oranges\", \"count\": 3 } ] }";15var N = JSON.Parse(jsonString);16var array = N["array"].AsArray;17foreach (var item in array) {18 Debug.Log(item["name"]);19}20string jsonString = "{ \"array\": [ { \"name\": \"apples\", \"count\": 7 }, { \"name\": \"oranges\", \"count\": 3 } ] }";21var N = JSON.Parse(jsonString);22var array = N["array"].AsArray;23foreach (var item in array) {24 Debug.Log(item["name"].Value);25}26string jsonString = "{ \"array\": [ { \"name\": \"apples\", \"count\": 7 }, { \"name\": \"oranges\", \"count\": 3 } ] }";27var N = JSON.Parse(jsonString);28var array = N["array"].AsArray;29foreach (var item in array) {30 Debug.Log(item["name"].ToString());31}32string jsonString = "{ \"array\": [ { \"name\": \"apples\", \"count\": 7 }, { \"name\": \"oranges\", \"count\": 3 } ] }";33var N = JSON.Parse(jsonString);34var array = N["array"].AsArray;35foreach (var item in array) {36 Debug.Log(item["name"].AsObject);37}38string jsonString = "{ \"array\": [ { \"name\": \"apples\", \"count\": 7 }, {
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!