47 using System.Collections.Generic;
66 #region common interface 68 public virtual JSONNode this[
int aIndex] {
get {
return null; }
set { } }
69 public virtual JSONNode this[
string aKey] {
get {
return null; }
set { } }
70 public virtual string Value {
get {
return ""; }
set { } }
71 public virtual int Count {
get {
return 0; } }
82 public virtual IEnumerable<JSONNode>
Childs {
get { yield
break;} }
88 foreach (var D
in C.DeepChilds)
102 #endregion common interface 104 #region typecasting properties 105 public virtual int AsInt 110 if (
int.TryParse(
Value,out v))
116 Value = value.ToString();
124 if (
float.TryParse(
Value,out v))
130 Value = value.ToString();
138 if (
double.TryParse(
Value,out v))
144 Value = value.ToString();
147 public virtual bool AsBool 152 if (
bool.TryParse(
Value,out v))
154 return !
string.IsNullOrEmpty(
Value);
158 Value = (value)?
"true":
"false";
177 #endregion typecasting properties 186 return (d == null)?null:d.
Value;
192 return System.Object.ReferenceEquals(a,b);
201 return System.Object.ReferenceEquals(
this, obj);
205 return base.GetHashCode();
211 internal static string Escape(
string aText)
214 foreach(
char c
in aText)
218 case '\\' : result +=
"\\\\";
break;
219 case '\"' : result +=
"\\\"";
break;
220 case '\n' : result +=
"\\n" ;
break;
221 case '\r' : result +=
"\\r" ;
break;
222 case '\t' : result +=
"\\t" ;
break;
223 case '\b' : result +=
"\\b" ;
break;
224 case '\f' : result +=
"\\f" ;
break;
225 default : result += c ;
break;
233 Stack<JSONNode> stack =
new Stack<JSONNode>();
237 string TokenName =
"";
238 bool QuoteMode =
false;
239 while (i < aJSON.Length)
252 TokenName = TokenName.Trim();
254 ctx.
Add(stack.Peek());
255 else if (TokenName !=
"")
256 ctx.Add(TokenName,stack.Peek());
273 TokenName = TokenName.Trim();
275 ctx.Add(stack.Peek());
276 else if (TokenName !=
"")
277 ctx.Add(TokenName,stack.Peek());
291 if (stack.Count == 0)
292 throw new Exception(
"JSON Parse: Too many closing brackets");
297 TokenName = TokenName.Trim();
300 else if (TokenName !=
"")
301 ctx.Add(TokenName,Token);
333 else if (TokenName !=
"")
334 ctx.Add(TokenName, Token);
357 case 't' : Token +=
'\t';
break;
358 case 'r' : Token +=
'\r';
break;
359 case 'n' : Token +=
'\n';
break;
360 case 'b' : Token +=
'\b';
break;
361 case 'f' : Token +=
'\f';
break;
364 string s = aJSON.Substring(i+1,4);
365 Token += (char)
int.
Parse(s,
System.Globalization.NumberStyles.AllowHexSpecifier);
369 default : Token += C;
break;
382 throw new Exception(
"JSON Parse: Quotation marks seems to be messed up.");
391 var W =
new System.IO.BinaryWriter(aData);
398 using (var gzipOut =
new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(aData))
400 gzipOut.IsStreamOwner =
false;
410 using(var F =
System.IO.File.OpenWrite(aFileName))
415 throw new Exception(
"Can't use File IO stuff in webplayer");
420 using (var stream =
new System.IO.MemoryStream())
424 return System.Convert.ToBase64String(stream.ToArray());
431 throw new Exception(
"Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON");
435 throw new Exception(
"Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON");
439 throw new Exception(
"Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON");
447 using(var F =
System.IO.File.OpenWrite(aFileName))
452 throw new Exception(
"Can't use File IO stuff in webplayer");
457 using (var stream =
new System.IO.MemoryStream())
461 return System.Convert.ToBase64String(stream.ToArray());
471 int count = aReader.ReadInt32();
473 for(
int i = 0; i < count; i++)
479 int count = aReader.ReadInt32();
481 for(
int i = 0; i < count; i++)
483 string key = aReader.ReadString();
491 return new JSONData(aReader.ReadString());
495 return new JSONData(aReader.ReadInt32());
499 return new JSONData(aReader.ReadDouble());
503 return new JSONData(aReader.ReadBoolean());
507 return new JSONData(aReader.ReadSingle());
512 throw new Exception(
"Error deserializing JSON. Unknown tag: " + type);
520 var zin =
new ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(aData);
526 using(var F =
System.IO.File.OpenRead(aFileName))
531 throw new Exception(
"Can't use File IO stuff in webplayer");
536 var tmp =
System.Convert.FromBase64String(aBase64);
537 var stream =
new System.IO.MemoryStream(tmp);
544 throw new Exception(
"Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON");
548 throw new Exception(
"Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON");
552 throw new Exception(
"Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON");
558 using(var R =
new System.IO.BinaryReader(aData))
566 using(var F =
System.IO.File.OpenRead(aFileName))
571 throw new Exception(
"Can't use File IO stuff in webplayer");
576 var tmp =
System.Convert.FromBase64String(aBase64);
577 var stream =
new System.IO.MemoryStream(tmp);
585 private List<JSONNode> m_List =
new List<JSONNode>();
586 public override JSONNode this[
int aIndex]
590 if (aIndex<0 || aIndex >= m_List.Count)
592 return m_List[aIndex];
596 if (aIndex<0 || aIndex >= m_List.Count)
599 m_List[aIndex] = value;
602 public override JSONNode this[
string aKey]
605 set{ m_List.Add(value); }
607 public override int Count 609 get {
return m_List.Count; }
617 if (aIndex < 0 || aIndex >= m_List.Count)
620 m_List.RemoveAt(aIndex);
628 public override IEnumerable<JSONNode>
Childs 643 string result =
"[ ";
646 if (result.Length > 2)
655 string result =
"[ ";
658 if (result.Length > 3)
660 result +=
"\n" + aPrefix +
" ";
663 result +=
"\n" + aPrefix +
"]";
669 aWriter.Write(m_List.Count);
670 for(
int i = 0; i < m_List.Count; i++)
672 m_List[i].Serialize(aWriter);
679 private Dictionary<string,JSONNode> m_Dict =
new Dictionary<string,JSONNode>();
680 public override JSONNode this[
string aKey]
684 if (m_Dict.ContainsKey(aKey))
691 if (m_Dict.ContainsKey(aKey))
692 m_Dict[aKey] = value;
694 m_Dict.Add(aKey,value);
697 public override JSONNode this[
int aIndex]
701 if (aIndex < 0 || aIndex >= m_Dict.Count)
703 return m_Dict.ElementAt(aIndex).
Value;
707 if (aIndex < 0 || aIndex >= m_Dict.Count)
709 string key = m_Dict.ElementAt(aIndex).Key;
713 public override int Count 715 get {
return m_Dict.Count; }
721 if (!
string.IsNullOrEmpty(aKey))
723 if (m_Dict.ContainsKey(aKey))
724 m_Dict[aKey] = aItem;
726 m_Dict.
Add(aKey, aItem);
729 m_Dict.Add(Guid.NewGuid().ToString(), aItem);
734 if (!m_Dict.ContainsKey(aKey))
742 if (aIndex < 0 || aIndex >= m_Dict.Count)
744 var item = m_Dict.ElementAt(aIndex);
752 var item = m_Dict.Where(k => k.Value == aNode).First();
762 public override IEnumerable<JSONNode>
Childs 766 foreach(KeyValuePair<string,JSONNode> N
in m_Dict)
767 yield
return N.Value;
773 foreach(KeyValuePair<string, JSONNode> N
in m_Dict)
779 foreach (KeyValuePair<string, JSONNode> N
in m_Dict)
781 if (result.Length > 2)
783 result +=
"\"" +
Escape(N.Key) +
"\":" + N.Value.ToString();
790 string result =
"{ ";
791 foreach (KeyValuePair<string, JSONNode> N
in m_Dict)
793 if (result.Length > 3)
795 result +=
"\n" + aPrefix +
" ";
796 result +=
"\"" +
Escape(N.Key) +
"\" : " + N.Value.ToString(aPrefix+
" ");
798 result +=
"\n" + aPrefix +
"}";
804 aWriter.Write(m_Dict.Count);
805 foreach(
string K
in m_Dict.Keys)
808 m_Dict[K].Serialize(aWriter);
816 public override string Value 818 get {
return m_Data; }
819 set { m_Data = value; }
844 return "\"" +
Escape(m_Data) +
"\"";
848 return "\"" +
Escape(m_Data) +
"\"";
855 if (tmp.m_Data ==
this.m_Data)
858 aWriter.Write(
AsInt);
862 if (tmp.m_Data ==
this.m_Data)
869 if (tmp.m_Data ==
this.m_Data)
877 if (tmp.m_Data ==
this.m_Data)
884 aWriter.Write(m_Data);
891 private string m_Key = null;
912 m_Node.
Add(m_Key, aVal);
917 public override JSONNode this[
int aIndex]
931 public override JSONNode this[
string aKey]
940 tmp.Add(aKey, value);
953 tmp.Add(aKey, aItem);
960 return System.Object.ReferenceEquals(a,b);
971 return System.Object.ReferenceEquals(
this, obj);
975 return base.GetHashCode();
987 public override int AsInt 1029 public override bool AsBool override JSONNode Remove(JSONNode aNode)
JSONLazyCreator(JSONNode aNode)
JSONLazyCreator(JSONNode aNode, string aKey)
virtual IEnumerable< JSONNode > Childs
void SaveToCompressedStream(System.IO.Stream aData)
override string ToString()
string SaveToCompressedBase64()
static JSONNode LoadFromCompressedBase64(string aBase64)
override string ToString()
IEnumerable< JSONNode > DeepChilds
override int GetHashCode()
static JSONNode Deserialize(System.IO.BinaryReader aReader)
virtual JSONNode Remove(JSONNode aNode)
virtual JSONArray AsArray
override string ToString(string aPrefix)
override JSONNode Remove(int aIndex)
override void Serialize(System.IO.BinaryWriter aWriter)
override string ToString()
override bool Equals(object obj)
void SaveToFile(string aFileName)
static JSONNode LoadFromStream(System.IO.Stream aData)
IEnumerator GetEnumerator()
virtual JSONNode Remove(string aKey)
static JSONNode LoadFromFile(string aFileName)
virtual void Add(JSONNode aItem)
static bool operator!=(JSONNode a, object b)
static string Escape(string aText)
override void Serialize(System.IO.BinaryWriter aWriter)
override void Add(string aKey, JSONNode aItem)
override JSONNode Remove(int aIndex)
virtual JSONClass AsObject
override string ToString()
override string ToString(string aPrefix)
override bool Equals(object obj)
virtual void Serialize(System.IO.BinaryWriter aWriter)
void SaveToCompressedFile(string aFileName)
override void Serialize(System.IO.BinaryWriter aWriter)
virtual JSONNode Remove(int aIndex)
override string ToString(string aPrefix)
static JSONNode LoadFromCompressedStream(System.IO.Stream aData)
override JSONNode Remove(JSONNode aNode)
static JSONNode LoadFromCompressedFile(string aFileName)
static JSONNode Parse(string aJSON)
override void Add(string aKey, JSONNode aItem)
virtual void Add(string aKey, JSONNode aItem)
override string ToString(string aPrefix)
override void Add(string aKey, JSONNode aItem)
override JSONNode Remove(string aKey)
override string ToString()
IEnumerator GetEnumerator()
virtual string ToString(string aPrefix)
override void Add(JSONNode aItem)
static bool operator==(JSONNode a, object b)
override int GetHashCode()
static JSONNode Parse(string aJSON)
static JSONNode LoadFromBase64(string aBase64)
void SaveToStream(System.IO.Stream aData)