Пример #1
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);
        }
Пример #2
0
        /// <summary>
        /// Reads the next object from the JSON string.
        /// </summary>
        /// <returns>
        /// Returns an Hashtable with all properties.
        /// </returns>
        internal JavaScriptObject ReadObject()
        {
            JavaScriptObject h = new JavaScriptObject();
            string           k;

            if (_ch == JSON_OBJECT_BEGIN)
            {
                ReadNext();
                ReadWhiteSpaces();

                if (_ch == JSON_OBJECT_END)
                {
                    ReadNext();
                    return(h);
                }

                while (_ch != END_OF_STRING)
                {
                    k = ReadString();
                    ReadWhiteSpaces();

                    if (_ch != JSON_PROPERTY_SEPARATOR)
                    {
                        break;
                    }

                    ReadNext();

                    h.AddInternal(k, GetObject());

                    ReadWhiteSpaces();

                    if (_ch == JSON_OBJECT_END)
                    {
                        ReadNext();
                        return(h);
                    }
                    else if (_ch != JSON_ITEMS_SEPARATOR)
                    {
                        break;
                    }

                    ReadNext();
                    ReadWhiteSpaces();
                }
            }

            throw new NotSupportedException("obj");
        }
Пример #3
0
        /// <summary>
        /// Converts an XML document to an IJavaScriptObject (JSON).
        /// <see cref="http://www.xml.com/pub/a/2006/05/31/converting-between-xml-and-json.html?page=1">Stefan Goessner</see>
        ///     <see cref="http://developer.yahoo.com/common/json.html#xml">Yahoo XML JSON</see>
        /// </summary>
        /// <param name="n">The XmlNode to serialize to JSON.</param>
        /// <returns>A IJavaScriptObject.</returns>
        public static IJavaScriptObject GetIJavaScriptObjectFromXmlNode(XmlNode n)
        {
            if (n == null)
            {
                return(null);
            }

            //if (xpath == "" || xpath == "/")
            //    xpath = n.Name;

            System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(@"\w+|\W+", System.Text.RegularExpressions.RegexOptions.Compiled);
            JavaScriptObject o = new JavaScriptObject();

            if (n.NodeType == XmlNodeType.Element)
            {
                for (int i = 0; i < n.Attributes.Count; i++)
                {
                    o.Add("@" + n.Attributes[i].Name, n.Attributes[i].Value);
                }

                if (n.FirstChild != null)                       // element has child nodes
                {
                    int  textChild       = 0;
                    bool hasElementChild = false;

                    for (XmlNode e = n.FirstChild; e != null; e = e.NextSibling)
                    {
                        if (e.NodeType == XmlNodeType.Element)
                        {
                            hasElementChild = true;
                        }
                        if (e.NodeType == XmlNodeType.Text && r.IsMatch(e.InnerText))
                        {
                            textChild++;                                                                                        // non-whitespace text
                        }
                    }

                    if (hasElementChild)
                    {
                        if (textChild < 2)                              // structured element with evtl. a single text node
                        {
                            for (XmlNode e = n.FirstChild; e != null; e = e.NextSibling)
                            {
                                if (e.NodeType == XmlNodeType.Text)
                                {
                                    o.Add("#text", e.InnerText);
                                }
                                else if (o.ContainsKey(e.Name))
                                {
                                    if (o[e.Name] is JavaScriptArray)
                                    {
                                        ((JavaScriptArray)o[e.Name]).Add(GetIJavaScriptObjectFromXmlNode(e));
                                    }
                                    else
                                    {
                                        IJavaScriptObject _o = o[e.Name];
                                        JavaScriptArray   a  = new JavaScriptArray();
                                        a.Add(_o);
                                        a.Add(GetIJavaScriptObjectFromXmlNode(e));
                                        o[e.Name] = a;
                                    }
                                }
                                else
                                {
                                    o.AddInternal(e.Name, GetIJavaScriptObjectFromXmlNode(e));
                                }
                            }
                        }
                    }
                    else if (textChild > 0)
                    {
                        if (n.Attributes.Count == 0)
                        {
                            return(new JavaScriptString(n.InnerText));
                        }
                        else
                        {
                            o.Add("#text", n.InnerText);
                        }
                    }
                }
                if (n.Attributes.Count == 0 && n.FirstChild == null)
                {
                    return(new JavaScriptString(n.InnerText));
                }
            }
            else if (n.NodeType == XmlNodeType.Document)
            {
                return(GetIJavaScriptObjectFromXmlNode(((XmlDocument)n).DocumentElement));
            }
            else
            {
                throw new NotSupportedException("Unhandled node type '" + n.NodeType + "'.");
            }

            return(o);
        }