Exemplo n.º 1
0
        /// <summary>
        /// Adds a new property to the object.
        /// </summary>
        /// <param name="key">The name of the property.</param>
        /// <param name="value">The value of the property.</param>
        public void Add(string key, JavaScriptNumber value)
        {
#if (NET20)
            list.Add(key, value);
#else
            keys.Add(key);
            list.Add(key, value);
#endif
        }
Exemplo n.º 2
0
        /// <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);
        }
Exemplo n.º 3
0
        /// <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.AddInternal(p.Attributes["name"].Value, DeserialzeFromAjaxXml(p.ChildNodes[0]));
                }

                return(o);
            }

            return(null);
        }
Exemplo n.º 4
0
        /// <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);
        }
Exemplo n.º 5
0
		/// <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.AddInternal(p.Attributes["name"].Value, DeserialzeFromAjaxXml(p.ChildNodes[0]));
					}

					return o;
			}

			return null;
		}
Exemplo n.º 6
0
        /// <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;
        }