/// <summary> /// Read a number object from the JSON string. /// </summary> /// <returns>Returns the number.</returns> internal JavaScriptNumber ReadNumber() { JavaScriptNumber n = new JavaScriptNumber(); if (_ch == '-') // negative numbers { n += "-"; ReadNext(); } // Searching for all numbers until the first character that is not // a number. while (_ch >= '0' && _ch <= '9' && _ch != END_OF_STRING) // all numbers between 0..9 { n += _ch; ReadNext(); } // In JavaScript (JSON) the decimal separator is always a point. If we // have a decimal number we read all the numbers after the separator. if (_ch == '.') { n += '.'; ReadNext(); while (_ch >= '0' && _ch <= '9' && _ch != END_OF_STRING) { n += _ch; ReadNext(); } } if (_ch == 'e' || _ch == 'E') { n += 'e'; ReadNext(); if (_ch == '-' || _ch == '+') { n += _ch; ReadNext(); } while (_ch >= '0' && _ch <= '9' && _ch != END_OF_STRING) { n += _ch; ReadNext(); } } return(n); }
/// <summary> /// Deserialzes from ajax XML. /// </summary> /// <param name="n">The n.</param> /// <returns></returns> internal static IJavaScriptObject DeserialzeFromAjaxXml(XmlNode n) { switch (n.Name) { case "array": JavaScriptArray a = new JavaScriptArray(); foreach (XmlNode item in n.ChildNodes) { a.Add(DeserialzeFromAjaxXml(item)); } return(a); case "boolean": JavaScriptBoolean b = new JavaScriptBoolean(n.InnerText == "true"); return(b); case "number": JavaScriptNumber i = new JavaScriptNumber(); i.Append(n.InnerText); return(i); case "string": JavaScriptString s = new JavaScriptString(); s.Append(n.InnerText); return(s); case "object": JavaScriptObject o = new JavaScriptObject(); foreach (XmlNode p in n.SelectNodes("property")) { if (p.Attributes["name"] == null || p.ChildNodes.Count != 1) { continue; } o.Add(p.Attributes["name"].Value, DeserialzeFromAjaxXml(p.ChildNodes[0])); } return(o); } return(null); }
/// <summary> /// Converts an IJavaScriptObject into an NET object. /// </summary> /// <param name="o">The IJavaScriptObject object to convert.</param> /// <param name="t"></param> /// <returns>Returns a .NET object.</returns> public override object Deserialize(IJavaScriptObject o, Type t) { JavaScriptNumber n = o as JavaScriptNumber; if (n == null) { throw new NotSupportedException(); } string s = n.Value; // TODO: return the default value for this primitive data type s = s.Replace(".", System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator); Decimal d = Decimal.Parse(s, System.Globalization.NumberStyles.Currency | System.Globalization.NumberStyles.AllowExponent); return(d); }
/// <summary> /// Deserialzes from ajax XML. /// </summary> /// <param name="n">The n.</param> /// <returns></returns> internal static IJavaScriptObject DeserialzeFromAjaxXml(XmlNode n) { switch (n.Name) { case "array": JavaScriptArray a = new JavaScriptArray(); foreach (XmlNode item in n.ChildNodes) a.Add(DeserialzeFromAjaxXml(item)); return a; case "boolean": JavaScriptBoolean b = new JavaScriptBoolean(n.InnerText == "true"); return b; case "number": JavaScriptNumber i = new JavaScriptNumber(); i.Append(n.InnerText); return i; case "string": JavaScriptString s = new JavaScriptString(); s.Append(n.InnerText); return s; case "object": JavaScriptObject o = new JavaScriptObject(); foreach (XmlNode p in n.SelectNodes("property")) { if (p.Attributes["name"] == null || p.ChildNodes.Count != 1) continue; o.Add(p.Attributes["name"].Value, DeserialzeFromAjaxXml(p.ChildNodes[0])); } return o; } return null; }
/// <summary> /// Read a number object from the JSON string. /// </summary> /// <returns>Returns the number.</returns> internal JavaScriptNumber ReadNumber() { JavaScriptNumber n = new JavaScriptNumber(); if (_ch == '-') // negative numbers { n += "-"; ReadNext(); } // Searching for all numbers until the first character that is not // a number. while (_ch >= '0' && _ch <= '9' && _ch != END_OF_STRING) // all numbers between 0..9 { n += _ch; ReadNext(); } // In JavaScript (JSON) the decimal separator is always a point. If we // have a decimal number we read all the numbers after the separator. if (_ch == '.') { n += '.'; ReadNext(); while (_ch >= '0' && _ch <= '9' && _ch != END_OF_STRING) { n += _ch; ReadNext(); } } if (_ch == 'e' || _ch == 'E') { n += 'e'; ReadNext(); if (_ch == '-' || _ch == '+') { n += _ch; ReadNext(); } while (_ch >= '0' && _ch <= '9' && _ch != END_OF_STRING) { n += _ch; ReadNext(); } } return n; }