12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175 |
- #define USE_SharpZipLib_NO
- #define NET2_0
- using System;
- using System.Collections;
- using System.Collections.Generic;
- namespace SimpleJSON
- {
- public enum JSONBinaryTag
- {
- Array = 1,
- Class = 2,
- Value = 3,
- IntValue = 4,
- DoubleValue = 5,
- BoolValue = 6,
- FloatValue = 7,
- }
- public class JSONNode : ICloneable
- {
- #region common interface
- public virtual void Add(string aKey, JSONNode aItem){ }
- public virtual JSONNode this[int aIndex] { get { return null; } set { } }
- public virtual JSONNode this[string aKey] { get { return null; } set { } }
- public virtual string Key { get { return ""; } set { } }
- public virtual string Value { get { return ""; } set { } }
- public virtual int Count { get { return 0; } }
- public virtual object JObject { get; set; }
- public virtual void Add(JSONNode aItem)
- {
- Add("", aItem);
- }
- public virtual object Clone()
- {
- return new JSONNode();
- }
- public virtual JSONNode Remove(string aKey) { return null; }
- public virtual JSONNode Remove(int aIndex) { return null; }
- public virtual JSONNode Remove(JSONNode aNode) { return aNode; }
- public virtual IEnumerable<JSONNode> Childs { get { yield break;} }
- public IEnumerable<JSONNode> DeepChilds
- {
- get
- {
- foreach (var C in Childs)
- foreach (var D in C.DeepChilds)
- yield return D;
- }
- }
- public override string ToString()
- {
- return "JSONNode";
- }
- public virtual string ToString(string aPrefix)
- {
- return "JSONNode";
- }
- #endregion common interface
- #region typecasting properties
- public virtual int AsInt
- {
- get
- {
- int v = 0;
- if (int.TryParse(Value,out v))
- return v;
- return 0;
- }
- set
- {
- Value = value.ToString();
- }
- }
- public virtual long AsLong
- {
- get
- {
- long v = 0;
- if (long.TryParse(Value, out v))
- return v;
- return 0;
- }
- set
- {
- Value = value.ToString();
- }
- }
- public virtual float AsFloat
- {
- get
- {
- float v = 0.0f;
- if (float.TryParse(Value,out v))
- return v;
- return 0.0f;
- }
- set
- {
- Value = value.ToString();
- }
- }
- public virtual double AsDouble
- {
- get
- {
- double v = 0.0;
- if (double.TryParse(Value,out v))
- return v;
- return 0.0;
- }
- set
- {
- Value = value.ToString();
- }
- }
- public virtual bool AsBool
- {
- get
- {
- bool v = false;
- if (bool.TryParse(Value,out v))
- return v;
- return !string.IsNullOrEmpty(Value);
- }
- set
- {
- Value = (value)?"true":"false";
- }
- }
- public virtual JSONArray AsArray
- {
- get
- {
- return this as JSONArray;
- }
- }
- public virtual JSONClass AsObject
- {
- get
- {
- return this as JSONClass;
- }
- }
- #endregion typecasting properties
- #region operators
- public static implicit operator JSONNode(string s)
- {
- return new JSONData(s);
- }
- public static implicit operator string(JSONNode d)
- {
- return (d == null) ? null : d.Value;
- }
- public static bool operator ==(JSONNode a, object b)
- {
- if (b == null && a is JSONLazyCreator)
- return true;
- return System.Object.ReferenceEquals(a, b);
- }
-
- public static bool operator !=(JSONNode a, object b)
- {
- return !(a == b);
- }
- public override bool Equals (object obj)
- {
- return System.Object.ReferenceEquals(this, obj);
- }
- public override int GetHashCode ()
- {
- return base.GetHashCode();
- }
-
-
- #endregion operators
-
- internal static string Escape(string aText)
- {
- if (aText == null) aText = "";
- string result = "";
- foreach(char c in aText)
- {
- switch(c)
- {
- case '\\' : result += "\\\\"; break;
- case '\"' : result += "\\\""; break;
- case '\n' : result += "\\n" ; break;
- case '\r' : result += "\\r" ; break;
- case '\t' : result += "\\t" ; break;
- case '\b' : result += "\\b" ; break;
- case '\f' : result += "\\f" ; break;
- default : result += c ; break;
- }
- }
- return result;
- }
- public static JSONNode Parse(string aJSON)
- {
- Stack<JSONNode> stack = new Stack<JSONNode>();
- JSONNode ctx = null;
- int i = 0;
- string Token = "";
- string TokenName = "";
- bool QuoteMode = false;
- while (i < aJSON.Length)
- {
- switch (aJSON[i])
- {
- case '{':
- if (QuoteMode)
- {
- Token += aJSON[i];
- break;
- }
- stack.Push(new JSONClass());
- if (ctx != null)
- {
- TokenName = TokenName.Trim();
- if (ctx is JSONArray)
- ctx.Add(stack.Peek());
- else if (TokenName != "")
- ctx.Add(TokenName,stack.Peek());
- }
- TokenName = "";
- Token = "";
- ctx = stack.Peek();
- break;
- case '[':
- if (QuoteMode)
- {
- Token += aJSON[i];
- break;
- }
- stack.Push(new JSONArray());
- if (ctx != null)
- {
- TokenName = TokenName.Trim();
- if (ctx is JSONArray)
- ctx.Add(stack.Peek());
- else if (TokenName != "")
- ctx.Add(TokenName,stack.Peek());
- }
- TokenName = "";
- Token = "";
- ctx = stack.Peek();
- break;
- case '}':
- case ']':
- if (QuoteMode)
- {
- Token += aJSON[i];
- break;
- }
- if (stack.Count == 0)
- throw new Exception("JSON Parse: Too many closing brackets");
- stack.Pop();
- if (Token != "")
- {
- TokenName = TokenName.Trim();
- if (ctx is JSONArray)
- ctx.Add(Token);
- else if (TokenName != "")
- ctx.Add(TokenName,Token);
- }
- TokenName = "";
- Token = "";
- if (stack.Count>0)
- ctx = stack.Peek();
- break;
- case ':':
- if (QuoteMode)
- {
- Token += aJSON[i];
- break;
- }
- TokenName = Token;
- Token = "";
- break;
- case '"':
- QuoteMode ^= true;
- break;
- case ',':
- if (QuoteMode)
- {
- Token += aJSON[i];
- break;
- }
- if (Token != "")
- {
- if (ctx is JSONArray)
- ctx.Add(Token);
- else if (TokenName != "")
- ctx.Add(TokenName, Token);
- }
- TokenName = "";
- Token = "";
- break;
- case '\r':
- case '\n':
- break;
- case ' ':
- case '\t':
- if (QuoteMode)
- Token += aJSON[i];
- break;
- case '\\':
- ++i;
- if (QuoteMode)
- {
- char C = aJSON[i];
- switch (C)
- {
- case 't' : Token += '\t'; break;
- case 'r' : Token += '\r'; break;
- case 'n' : Token += '\n'; break;
- case 'b' : Token += '\b'; break;
- case 'f' : Token += '\f'; break;
- case 'u':
- {
- string s = aJSON.Substring(i+1,4);
- Token += (char)int.Parse(s, System.Globalization.NumberStyles.AllowHexSpecifier);
- i += 4;
- break;
- }
- default : Token += C; break;
- }
- }
- break;
- default:
- Token += aJSON[i];
- break;
- }
- ++i;
- }
- if (QuoteMode)
- {
- throw new Exception("JSON Parse: Quotation marks seems to be messed up.");
- }
- return ctx;
- }
-
- public virtual void Serialize(System.IO.BinaryWriter aWriter) {}
- public void SaveToStream(System.IO.Stream aData)
- {
- var W = new System.IO.BinaryWriter(aData);
- Serialize(W);
- }
-
- #if USE_SharpZipLib
- public void SaveToCompressedStream(System.IO.Stream aData)
- {
- using (var gzipOut = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(aData))
- {
- gzipOut.IsStreamOwner = false;
- SaveToStream(gzipOut);
- gzipOut.Close();
- }
- }
- public void SaveToCompressedFile(string aFileName)
- {
- System.IO.Directory.CreateDirectory((new System.IO.FileInfo(aFileName)).Directory.FullName);
- using(var F = System.IO.File.OpenWrite(aFileName))
- {
- SaveToCompressedStream(F);
- }
- }
- public string SaveToCompressedBase64()
- {
- using (var stream = new System.IO.MemoryStream())
- {
- SaveToCompressedStream(stream);
- stream.Position = 0;
- return System.Convert.ToBase64String(stream.ToArray());
- }
- }
- #else
- public void SaveToCompressedStream(System.IO.Stream aData)
- {
- throw new Exception("Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON");
- }
- 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");
- }
- 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");
- }
- #endif
-
- public void SaveToFile(string aFileName)
- {
- System.IO.Directory.CreateDirectory((new System.IO.FileInfo(aFileName)).Directory.FullName);
- using(var F = System.IO.File.OpenWrite(aFileName))
- {
- SaveToStream(F);
- }
- }
- public string SaveToBase64()
- {
- using (var stream = new System.IO.MemoryStream())
- {
- SaveToStream(stream);
- stream.Position = 0;
- return System.Convert.ToBase64String(stream.ToArray());
- }
- }
- public static JSONNode Deserialize(System.IO.BinaryReader aReader)
- {
- JSONBinaryTag type = (JSONBinaryTag)aReader.ReadByte();
- switch(type)
- {
- case JSONBinaryTag.Array:
- {
- int count = aReader.ReadInt32();
- JSONArray tmp = new JSONArray();
- for(int i = 0; i < count; i++)
- tmp.Add(Deserialize(aReader));
- return tmp;
- }
- case JSONBinaryTag.Class:
- {
- int count = aReader.ReadInt32();
- JSONClass tmp = new JSONClass();
- for(int i = 0; i < count; i++)
- {
- string key = aReader.ReadString();
- var val = Deserialize(aReader);
- tmp.Add(key, val);
- }
- return tmp;
- }
- case JSONBinaryTag.Value:
- {
- return new JSONData(aReader.ReadString());
- }
- case JSONBinaryTag.IntValue:
- {
- return new JSONData(aReader.ReadInt32());
- }
- case JSONBinaryTag.DoubleValue:
- {
- return new JSONData(aReader.ReadDouble());
- }
- case JSONBinaryTag.BoolValue:
- {
- return new JSONData(aReader.ReadBoolean());
- }
- case JSONBinaryTag.FloatValue:
- {
- return new JSONData(aReader.ReadSingle());
- }
-
- default:
- {
- throw new Exception("Error deserializing JSON. Unknown tag: " + type);
- }
- }
- }
-
- #if USE_SharpZipLib
- public static JSONNode LoadFromCompressedStream(System.IO.Stream aData)
- {
- var zin = new ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(aData);
- return LoadFromStream(zin);
- }
- public static JSONNode LoadFromCompressedFile(string aFileName)
- {
- using(var F = System.IO.File.OpenRead(aFileName))
- {
- return LoadFromCompressedStream(F);
- }
- }
- public static JSONNode LoadFromCompressedBase64(string aBase64)
- {
- var tmp = System.Convert.FromBase64String(aBase64);
- var stream = new System.IO.MemoryStream(tmp);
- stream.Position = 0;
- return LoadFromCompressedStream(stream);
- }
- #else
- 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");
- }
- public static JSONNode LoadFromCompressedStream(System.IO.Stream aData)
- {
- throw new Exception("Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON");
- }
- 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");
- }
- #endif
-
- public static JSONNode LoadFromStream(System.IO.Stream aData)
- {
- using(var R = new System.IO.BinaryReader(aData))
- {
- return Deserialize(R);
- }
- }
- public static JSONNode LoadFromFile(string aFileName)
- {
- using(var F = System.IO.File.OpenRead(aFileName))
- {
- return LoadFromStream(F);
- }
- }
- public static JSONNode LoadFromBase64(string aBase64)
- {
- var tmp = System.Convert.FromBase64String(aBase64);
- var stream = new System.IO.MemoryStream(tmp);
- stream.Position = 0;
- return LoadFromStream(stream);
- }
- public static string GetKey(JSONNode json, int index)
- {
- try
- {
- JSONClass jsc = (JSONClass)json;
- return jsc.GetKey(index);
- }
- catch
- {
-
- }
- return "";
- }
- } // End of JSONNode
- public class JSONArray : JSONNode, IEnumerable
- {
- private List<JSONNode> m_List = new List<JSONNode>();
- public override JSONNode this[int aIndex]
- {
- get
- {
- if (aIndex<0 || aIndex >= m_List.Count)
- return new JSONLazyCreator(this);
- return m_List[aIndex];
- }
- set
- {
- if (aIndex<0 || aIndex >= m_List.Count)
- m_List.Add(value);
- else
- m_List[aIndex] = value;
- }
- }
- public override JSONNode this[string aKey]
- {
- get{ return new JSONLazyCreator(this);}
- set{ m_List.Add(value); }
- }
- public override object Clone()
- {
- JSONArray arr = new JSONArray();
- foreach (var node in m_List)
- {
- arr.m_List.Add(node.Clone() as JSONNode);
- }
- return arr;
- }
- public override int Count
- {
- get { return m_List.Count; }
- }
- public override void Add(string aKey, JSONNode aItem)
- {
- m_List.Add(aItem);
- }
- public override JSONNode Remove(int aIndex)
- {
- if (aIndex < 0 || aIndex >= m_List.Count)
- return null;
- JSONNode tmp = m_List[aIndex];
- m_List.RemoveAt(aIndex);
- return tmp;
- }
- public override JSONNode Remove(JSONNode aNode)
- {
- m_List.Remove(aNode);
- return aNode;
- }
- public override IEnumerable<JSONNode> Childs
- {
- get
- {
- foreach(JSONNode N in m_List)
- yield return N;
- }
- }
- public IEnumerator GetEnumerator()
- {
- foreach(JSONNode N in m_List)
- yield return N;
- }
- public override string ToString()
- {
- string result = "[ ";
- foreach (JSONNode N in m_List)
- {
- if (result.Length > 2)
- result += ", ";
- result += N.ToString();
- }
- result += " ]";
- return result;
- }
- public override string ToString(string aPrefix)
- {
- string result = "[ ";
- foreach (JSONNode N in m_List)
- {
- if (result.Length > 3)
- result += ", ";
- result += "\n" + aPrefix + " ";
- result += N.ToString(aPrefix+" ");
- }
- result += "\n" + aPrefix + "]";
- return result;
- }
- public override void Serialize (System.IO.BinaryWriter aWriter)
- {
- aWriter.Write((byte)JSONBinaryTag.Array);
- aWriter.Write(m_List.Count);
- for(int i = 0; i < m_List.Count; i++)
- {
- m_List[i].Serialize(aWriter);
- }
- }
- } // End of JSONArray
- public class JSONClass : JSONNode, IEnumerable
- {
- private Dictionary<string,JSONNode> m_Dict = new Dictionary<string,JSONNode>();
-
- public override JSONNode this[string aKey]
- {
- get
- {
- if (m_Dict.ContainsKey(aKey))
- return m_Dict[aKey];
- else
- return new JSONLazyCreator(this, aKey);
- }
- set
- {
- if (m_Dict.ContainsKey(aKey))
- m_Dict[aKey] = value;
- else
- m_Dict.Add(aKey,value);
- }
- }
- public override object Clone()
- {
- JSONClass cls = new JSONClass();
- foreach (var kp in m_Dict)
- cls.m_Dict.Add(kp.Key, kp.Value.Clone() as JSONNode);
- return cls;
- }
- public override JSONNode this[int aIndex]
- {
- get
- {
- if (aIndex < 0 || aIndex >= m_Dict.Count)
- return null;
- #if NET2_0
- int i = 0;
- JSONNode v = null;
- foreach(var o in m_Dict)
- {
- if (aIndex == i)
- {
- v = o.Value;
- break;
- }
- i++;
- }
- return v;
- #else
- return m_Dict.ElementAt(aIndex).Value;
- #endif
- }
- set
- {
- if (aIndex < 0 || aIndex >= m_Dict.Count)
- return;
- #if NET2_0
- int i = 0;
- string k = "";
- foreach (var o in m_Dict)
- {
- if (aIndex == i)
- {
- k = o.Key;
- break;
- }
- i++;
- }
- if (!string.IsNullOrEmpty(k)) m_Dict[k] = value;
- #else
- string key = m_Dict.ElementAt(aIndex).Key;
- m_Dict[key] = value;
- #endif
- }
- }
- public string GetKey(int aIndex)
- {
- string key = "";
- if (aIndex < 0 || aIndex >= m_Dict.Count)
- return null;
- int i = 0;
- //JSONNode v = null;
- foreach (var o in m_Dict)
- {
- if (aIndex == i)
- {
- key = o.Key;
- //v = o.Value;
- break;
- }
- i++;
- }
- return key;
- }
- public override int Count
- {
- get { return m_Dict.Count; }
- }
- public override void Add(string aKey, JSONNode aItem)
- {
- if (!string.IsNullOrEmpty(aKey))
- {
- if (m_Dict.ContainsKey(aKey))
- m_Dict[aKey] = aItem;
- else
- m_Dict.Add(aKey, aItem);
- }
- else
- m_Dict.Add(Guid.NewGuid().ToString(), aItem);
- }
- public override JSONNode Remove(string aKey)
- {
- if (!m_Dict.ContainsKey(aKey))
- return null;
- JSONNode tmp = m_Dict[aKey];
- m_Dict.Remove(aKey);
- return tmp;
- }
- public override JSONNode Remove(int aIndex)
- {
- if (aIndex < 0 || aIndex >= m_Dict.Count)
- return null;
- #if NET2_0
- int i = 0;
- string k = "";
- JSONNode v = null;
- foreach (var o in m_Dict)
- {
- if (aIndex == i)
- {
- k = o.Key;
- v = o.Value;
- break;
- }
- i++;
- }
- if (!string.IsNullOrEmpty(k)) m_Dict.Remove(k);
- return v;
- #else
- var item = m_Dict.ElementAt(aIndex);
- m_Dict.Remove(item.Key);
- return item.Value;
- #endif
- }
- public override JSONNode Remove(JSONNode aNode)
- {
- try
- {
- #if NET2_0
- int i = 0;
- string k = "";
- foreach (var o in m_Dict)
- {
- if (o.Value == aNode)
- {
- k = o.Key;
- break;
- }
- i++;
- }
- if(!string.IsNullOrEmpty(k)) m_Dict.Remove(k);
- return aNode;
- #else
- var item = m_Dict.Where(k => k.Value == aNode).First();
- m_Dict.Remove(item.Key);
- return aNode;
- #endif
- }
- catch
- {
- return null;
- }
- }
- public override IEnumerable<JSONNode> Childs
- {
- get
- {
- foreach(KeyValuePair<string,JSONNode> N in m_Dict)
- yield return N.Value;
- }
- }
- public IEnumerator GetEnumerator()
- {
- foreach(KeyValuePair<string, JSONNode> N in m_Dict)
- yield return N;
- }
- public override string ToString()
- {
- string result = "{";
- foreach (KeyValuePair<string, JSONNode> N in m_Dict)
- {
- if (N.Value == null) continue;
- if (result.Length > 2)
- result += ", ";
- result += "\"" + Escape(N.Key) + "\":" + N.Value.ToString();
- }
- result += "}";
- return result;
- }
- public override string ToString(string aPrefix)
- {
- string result = "{ ";
- foreach (KeyValuePair<string, JSONNode> N in m_Dict)
- {
- if (result.Length > 3)
- result += ", ";
- result += "\n" + aPrefix + " ";
- result += "\"" + Escape(N.Key) + "\" : " + N.Value.ToString(aPrefix+" ");
- }
- result += "\n" + aPrefix + "}";
- return result;
- }
- public override void Serialize (System.IO.BinaryWriter aWriter)
- {
- aWriter.Write((byte)JSONBinaryTag.Class);
- aWriter.Write(m_Dict.Count);
- foreach(string K in m_Dict.Keys)
- {
- aWriter.Write(K);
- m_Dict[K].Serialize(aWriter);
- }
- }
- } // End of JSONClass
- public class JSONData : JSONNode
- {
- private string m_Data;
- public override string Value
- {
- get { return m_Data; }
- set { m_Data = value; }
- }
- public override object Clone()
- {
- JSONData d = new JSONData(m_Data);
- return d;
- }
- public JSONData(string aData)
- {
- m_Data = aData;
- }
- public JSONData(float aData)
- {
- AsFloat = aData;
- }
- public JSONData(double aData)
- {
- AsDouble = aData;
- }
- public JSONData(bool aData)
- {
- AsBool = aData;
- }
- public JSONData(int aData)
- {
- AsInt = aData;
- }
-
- public override string ToString()
- {
- return "\"" + Escape(m_Data) + "\"";
- }
- public override string ToString(string aPrefix)
- {
- return "\"" + Escape(m_Data) + "\"";
- }
- public override void Serialize (System.IO.BinaryWriter aWriter)
- {
- var tmp = new JSONData("");
-
- tmp.AsInt = AsInt;
- if (tmp.m_Data == this.m_Data)
- {
- aWriter.Write((byte)JSONBinaryTag.IntValue);
- aWriter.Write(AsInt);
- return;
- }
- tmp.AsFloat = AsFloat;
- if (tmp.m_Data == this.m_Data)
- {
- aWriter.Write((byte)JSONBinaryTag.FloatValue);
- aWriter.Write(AsFloat);
- return;
- }
- tmp.AsDouble = AsDouble;
- if (tmp.m_Data == this.m_Data)
- {
- aWriter.Write((byte)JSONBinaryTag.DoubleValue);
- aWriter.Write(AsDouble);
- return;
- }
- tmp.AsBool = AsBool;
- if (tmp.m_Data == this.m_Data)
- {
- aWriter.Write((byte)JSONBinaryTag.BoolValue);
- aWriter.Write(AsBool);
- return;
- }
- aWriter.Write((byte)JSONBinaryTag.Value);
- aWriter.Write(m_Data);
- }
- } // End of JSONData
-
- internal class JSONLazyCreator : JSONNode
- {
- private JSONNode m_Node = null;
- private string m_Key = null;
-
- public JSONLazyCreator(JSONNode aNode)
- {
- m_Node = aNode;
- m_Key = null;
- }
- public JSONLazyCreator(JSONNode aNode, string aKey)
- {
- m_Node = aNode;
- m_Key = aKey;
- }
-
- private void Set(JSONNode aVal)
- {
- if (m_Key == null)
- {
- m_Node.Add(aVal);
- }
- else
- {
- m_Node.Add(m_Key, aVal);
- }
- m_Node = null; // Be GC friendly.
- }
-
- public override JSONNode this[int aIndex]
- {
- get
- {
- return new JSONLazyCreator(this);
- }
- set
- {
- var tmp = new JSONArray();
- tmp.Add(value);
- Set(tmp);
- }
- }
-
- public override JSONNode this[string aKey]
- {
- get
- {
- return new JSONLazyCreator(this, aKey);
- }
- set
- {
- var tmp = new JSONClass();
- tmp.Add(aKey, value);
- Set(tmp);
- }
- }
- public override void Add (JSONNode aItem)
- {
- var tmp = new JSONArray();
- tmp.Add(aItem);
- Set(tmp);
- }
- public override void Add (string aKey, JSONNode aItem)
- {
- var tmp = new JSONClass();
- tmp.Add(aKey, aItem);
- Set(tmp);
- }
- public static bool operator ==(JSONLazyCreator a, object b)
- {
- if (b == null)
- return true;
- return System.Object.ReferenceEquals(a,b);
- }
-
- public static bool operator !=(JSONLazyCreator a, object b)
- {
- return !(a == b);
- }
- public override bool Equals (object obj)
- {
- if (obj == null)
- return true;
- return System.Object.ReferenceEquals(this, obj);
- }
- public override int GetHashCode ()
- {
- return base.GetHashCode();
- }
-
- public override string ToString()
- {
- return "";
- }
- public override string ToString(string aPrefix)
- {
- return "";
- }
-
- public override int AsInt
- {
- get
- {
- JSONData tmp = new JSONData(0);
- Set(tmp);
- return 0;
- }
- set
- {
- JSONData tmp = new JSONData(value);
- Set(tmp);
- }
- }
- public override float AsFloat
- {
- get
- {
- JSONData tmp = new JSONData(0.0f);
- Set(tmp);
- return 0.0f;
- }
- set
- {
- JSONData tmp = new JSONData(value);
- Set(tmp);
- }
- }
- public override double AsDouble
- {
- get
- {
- JSONData tmp = new JSONData(0.0);
- Set(tmp);
- return 0.0;
- }
- set
- {
- JSONData tmp = new JSONData(value);
- Set(tmp);
- }
- }
- public override bool AsBool
- {
- get
- {
- JSONData tmp = new JSONData(false);
- Set(tmp);
- return false;
- }
- set
- {
- JSONData tmp = new JSONData(value);
- Set(tmp);
- }
- }
- public override JSONArray AsArray
- {
- get
- {
- JSONArray tmp = new JSONArray();
- Set(tmp);
- return tmp;
- }
- }
- public override JSONClass AsObject
- {
- get
- {
- JSONClass tmp = new JSONClass();
- Set(tmp);
- return tmp;
- }
- }
- } // End of JSONLazyCreator
- public static class JSON
- {
- public static JSONNode Parse(string aJSON)
- {
- return JSONNode.Parse(aJSON);
- }
- }
- }
|