123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621 |
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.IO;
- namespace I2.Loc.SimpleJSON
- {
- public class JSONNode
- {
- 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 Value
- {
- get
- {
- return string.Empty;
- }
- set
- {
- }
- }
- public virtual int Count
- {
- get
- {
- return 0;
- }
- }
- public virtual void Add(JSONNode aItem)
- {
- this.Add(string.Empty, aItem);
- }
- 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 (JSONNode C in this.Childs)
- {
- foreach (JSONNode D in C.DeepChilds)
- {
- yield return D;
- }
- }
- yield break;
- }
- }
- public override string ToString()
- {
- return "JSONNode";
- }
- public virtual string ToString(string aPrefix)
- {
- return "JSONNode";
- }
- public virtual int AsInt
- {
- get
- {
- int result = 0;
- if (int.TryParse(this.Value, out result))
- {
- return result;
- }
- return 0;
- }
- set
- {
- this.Value = value.ToString();
- }
- }
- public virtual float AsFloat
- {
- get
- {
- float result = 0f;
- if (float.TryParse(this.Value, out result))
- {
- return result;
- }
- return 0f;
- }
- set
- {
- this.Value = value.ToString();
- }
- }
- public virtual double AsDouble
- {
- get
- {
- double result = 0.0;
- if (double.TryParse(this.Value, out result))
- {
- return result;
- }
- return 0.0;
- }
- set
- {
- this.Value = value.ToString();
- }
- }
- public virtual bool AsBool
- {
- get
- {
- bool result = false;
- if (bool.TryParse(this.Value, out result))
- {
- return result;
- }
- return !string.IsNullOrEmpty(this.Value);
- }
- set
- {
- this.Value = ((!value) ? "false" : "true");
- }
- }
- public virtual JSONArray AsArray
- {
- get
- {
- return this as JSONArray;
- }
- }
- public virtual JSONClass AsObject
- {
- get
- {
- return this as JSONClass;
- }
- }
- public static implicit operator JSONNode(string s)
- {
- return new JSONData(s);
- }
- public static implicit operator string(JSONNode d)
- {
- return (!(d == null)) ? d.Value : null;
- }
- public static bool operator ==(JSONNode a, object b)
- {
- return (b == null && a is JSONLazyCreator) || object.ReferenceEquals(a, b);
- }
- public static bool operator !=(JSONNode a, object b)
- {
- return !(a == b);
- }
- public override bool Equals(object obj)
- {
- return object.ReferenceEquals(this, obj);
- }
- public override int GetHashCode()
- {
- return base.GetHashCode();
- }
- internal static string Escape(string aText)
- {
- string text = string.Empty;
- foreach (char c in aText)
- {
- switch (c)
- {
- case '\b':
- text += "\\b";
- break;
- case '\t':
- text += "\\t";
- break;
- case '\n':
- text += "\\n";
- break;
- default:
- if (c != '"')
- {
- if (c != '\\')
- {
- text += c;
- }
- else
- {
- text += "\\\\";
- }
- }
- else
- {
- text += "\\\"";
- }
- break;
- case '\f':
- text += "\\f";
- break;
- case '\r':
- text += "\\r";
- break;
- }
- }
- return text;
- }
- public static JSONNode Parse(string aJSON)
- {
- Stack<JSONNode> stack = new Stack<JSONNode>();
- JSONNode jsonnode = null;
- int i = 0;
- string text = string.Empty;
- string text2 = string.Empty;
- bool flag = false;
- while (i < aJSON.Length)
- {
- char c = aJSON[i];
- switch (c)
- {
- case '\t':
- goto IL_333;
- case '\n':
- case '\r':
- break;
- default:
- switch (c)
- {
- case '[':
- if (flag)
- {
- text += aJSON[i];
- goto IL_45C;
- }
- stack.Push(new JSONArray());
- if (jsonnode != null)
- {
- text2 = text2.Trim();
- if (jsonnode is JSONArray)
- {
- jsonnode.Add(stack.Peek());
- }
- else if (text2 != string.Empty)
- {
- jsonnode.Add(text2, stack.Peek());
- }
- }
- text2 = string.Empty;
- text = string.Empty;
- jsonnode = stack.Peek();
- goto IL_45C;
- case '\\':
- i++;
- if (flag)
- {
- char c2 = aJSON[i];
- switch (c2)
- {
- case 'r':
- text += '\r';
- break;
- default:
- if (c2 != 'b')
- {
- if (c2 != 'f')
- {
- if (c2 != 'n')
- {
- text += c2;
- }
- else
- {
- text += '\n';
- }
- }
- else
- {
- text += '\f';
- }
- }
- else
- {
- text += '\b';
- }
- break;
- case 't':
- text += '\t';
- break;
- case 'u':
- {
- string s = aJSON.Substring(i + 1, 4);
- text += (char)int.Parse(s, NumberStyles.AllowHexSpecifier);
- i += 4;
- break;
- }
- }
- }
- goto IL_45C;
- case ']':
- break;
- default:
- switch (c)
- {
- case ' ':
- goto IL_333;
- default:
- switch (c)
- {
- case '{':
- if (flag)
- {
- text += aJSON[i];
- goto IL_45C;
- }
- stack.Push(new JSONClass());
- if (jsonnode != null)
- {
- text2 = text2.Trim();
- if (jsonnode is JSONArray)
- {
- jsonnode.Add(stack.Peek());
- }
- else if (text2 != string.Empty)
- {
- jsonnode.Add(text2, stack.Peek());
- }
- }
- text2 = string.Empty;
- text = string.Empty;
- jsonnode = stack.Peek();
- goto IL_45C;
- default:
- if (c != ',')
- {
- if (c != ':')
- {
- text += aJSON[i];
- goto IL_45C;
- }
- if (flag)
- {
- text += aJSON[i];
- goto IL_45C;
- }
- text2 = text;
- text = string.Empty;
- goto IL_45C;
- }
- else
- {
- if (flag)
- {
- text += aJSON[i];
- goto IL_45C;
- }
- if (text != string.Empty)
- {
- if (jsonnode is JSONArray)
- {
- jsonnode.Add(text);
- }
- else if (text2 != string.Empty)
- {
- jsonnode.Add(text2, text);
- }
- }
- text2 = string.Empty;
- text = string.Empty;
- goto IL_45C;
- }
- break;
- case '}':
- break;
- }
- break;
- case '"':
- flag ^= true;
- goto IL_45C;
- }
- break;
- }
- if (flag)
- {
- text += aJSON[i];
- }
- else
- {
- if (stack.Count == 0)
- {
- throw new Exception("JSON Parse: Too many closing brackets");
- }
- stack.Pop();
- if (text != string.Empty)
- {
- text2 = text2.Trim();
- if (jsonnode is JSONArray)
- {
- jsonnode.Add(text);
- }
- else if (text2 != string.Empty)
- {
- jsonnode.Add(text2, text);
- }
- }
- text2 = string.Empty;
- text = string.Empty;
- if (stack.Count > 0)
- {
- jsonnode = stack.Peek();
- }
- }
- break;
- }
- IL_45C:
- i++;
- continue;
- IL_333:
- if (flag)
- {
- text += aJSON[i];
- }
- goto IL_45C;
- }
- if (flag)
- {
- throw new Exception("JSON Parse: Quotation marks seems to be messed up.");
- }
- return jsonnode;
- }
- public virtual void Serialize(BinaryWriter aWriter)
- {
- }
- public void SaveToStream(Stream aData)
- {
- BinaryWriter aWriter = new BinaryWriter(aData);
- this.Serialize(aWriter);
- }
- 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");
- }
- 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");
- }
- public void SaveToFile(string aFileName)
- {
- Directory.CreateDirectory(new FileInfo(aFileName).Directory.FullName);
- using (FileStream fileStream = File.OpenWrite(aFileName))
- {
- this.SaveToStream(fileStream);
- }
- }
- public string SaveToBase64()
- {
- string result;
- using (MemoryStream memoryStream = new MemoryStream())
- {
- this.SaveToStream(memoryStream);
- memoryStream.Position = 0L;
- result = Convert.ToBase64String(memoryStream.ToArray());
- }
- return result;
- }
- public static JSONNode Deserialize(BinaryReader aReader)
- {
- JSONBinaryTag jsonbinaryTag = (JSONBinaryTag)aReader.ReadByte();
- switch (jsonbinaryTag)
- {
- case JSONBinaryTag.Array:
- {
- int num = aReader.ReadInt32();
- JSONArray jsonarray = new JSONArray();
- for (int i = 0; i < num; i++)
- {
- jsonarray.Add(JSONNode.Deserialize(aReader));
- }
- return jsonarray;
- }
- case JSONBinaryTag.Class:
- {
- int num2 = aReader.ReadInt32();
- JSONClass jsonclass = new JSONClass();
- for (int j = 0; j < num2; j++)
- {
- string aKey = aReader.ReadString();
- JSONNode aItem = JSONNode.Deserialize(aReader);
- jsonclass.Add(aKey, aItem);
- }
- return jsonclass;
- }
- 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: " + jsonbinaryTag);
- }
- }
- 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(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");
- }
- public static JSONNode LoadFromStream(Stream aData)
- {
- JSONNode result;
- using (BinaryReader binaryReader = new BinaryReader(aData))
- {
- result = JSONNode.Deserialize(binaryReader);
- }
- return result;
- }
- public static JSONNode LoadFromFile(string aFileName)
- {
- JSONNode result;
- using (FileStream fileStream = File.OpenRead(aFileName))
- {
- result = JSONNode.LoadFromStream(fileStream);
- }
- return result;
- }
- public static JSONNode LoadFromBase64(string aBase64)
- {
- byte[] buffer = Convert.FromBase64String(aBase64);
- return JSONNode.LoadFromStream(new MemoryStream(buffer)
- {
- Position = 0L
- });
- }
- }
- }
|