示例#1
0
 public JSONNode Parse(FlashCompatibleTextReader reader)
 {
     this.reader = reader;
     if (reader.Peek() == -1)
     {
         return(null);
     }
     return(ReadObject());
 }
示例#2
0
    //---------------------------------------------------------------------------------
    // Parse
    //---------------------------------------------------------------------------------
    public JSONNode Parse(FlashCompatibleTextReader reader)
    {
        this.reader = reader;

        //check empty string
        if(reader.Peek() == -1)
            return null;

        return ReadObject();
    }
 //---------------------------------------------------------------------------------
 // SkipWhitespace
 //---------------------------------------------------------------------------------
 private void SkipWhitespace()
 {
     while (true)
     {
         int peek = reader.Peek();
         if (peek == -1 || !Char.IsWhiteSpace((char)peek))
         {
             return;
         }
         reader.Read();
     }
 }
 private void SkipWhitespace()
 {
     while (true)
     {
         int num = reader.Peek();
         if (num == -1 || !char.IsWhiteSpace((char)num))
         {
             break;
         }
         reader.Read();
     }
 }
示例#5
0
    //---------------------------------------------------------------------------------
    // ReadObject
    //---------------------------------------------------------------------------------
    private JSONNode ReadObject()
    {
        JSONNode node = new JSONNode();

        SkipWhitespace();
        // read '{'
        if (reader.Peek() != '{')
        {
            Debug.LogError("malformed json: no starting '{'");
            return(null);
        }
        reader.Read();
        while (reader.Peek() != '}')
        {
            if (reader.Peek() == ',')
            {
                reader.Read();
            }
            if (reader.Peek() == '}')
            {
                break;
            }

            SkipWhitespace();
            // read field name
            string fieldName = ReadFieldName().Trim();

            // read ':'
            SkipWhitespace();
            reader.Read();
            //Console.WriteLine("after : found " + ((char) reader.Peek()));

            // read value
            IJSONFieldValue val = ReadValue();

            //Console.WriteLine("adding field " + fieldName + " " + val);
            node.AddField(fieldName, val);
            SkipWhitespace();
        }
        reader.Read();         // remove trailing "}"

        return(node);
    }
示例#6
0
    //---------------------------------------------------------------------------------
    // Parse
    //---------------------------------------------------------------------------------
    public XMLNode Parse(FlashCompatibleTextReader reader)
    {
        this.reader = reader;

        //check empty string
        if(reader.Peek() == -1)
            return null;

        // skip xml declaration or DocTypes
        SkipPrologs();

        //check empty string
        if(reader.Peek() == -1)
            return null;

        while (true)
        {
            SkipWhitespace();
            int index;
            string tagName;

            // remove the prepend or trailing white spaces
            bool startingBracket = (char)reader.Peek() == '<';
            string currentTag = ReadTag(startingBracket).Trim();
            if(currentTag.StartsWith("<!"))
            {
                // Nothing to do, it's a comment
            }
            else if (currentTag.StartsWith("</"))
            {
                // close tag
                tagName = currentTag.Substring(2, currentTag.Length-3);

                // no open tag
                if (currentElement == null)
                {
                    Debug.LogError("Got close tag '" + tagName +
                            "' without open tag.");
                    return null;
                }

                // close tag does not match with open tag
                if (tagName != currentElement.tag)
                {
                    Debug.LogError("Expected close tag for '" +
                                currentElement.tag + "' but got '" +
                             tagName + "'.");
                    return null;
                }

                if (elements.Count == 0)
                    return currentElement;
                else
                    currentElement = (XMLNode)elements.Pop();

            }
            else
            {
                // open tag or tag with both open and close tags
                index = currentTag.IndexOf('"');
                if(index < 0)
                    index = currentTag.IndexOf('\'');

                if (index < 0) {
                    // tag with no attributes
                    if (currentTag.EndsWith("/>"))
                    {
                        // close tag as well
                        tagName = currentTag.Substring(1, currentTag.Length-3).Trim();
                        currentTag = "/>";
                    }
                    else
                    {
                        // open tag
                        tagName = currentTag.Substring(1, currentTag.Length-2).Trim();
                        currentTag = "";
                    }
                }
                else
                {
                    // tag with attributes
                    int endtagIndex = currentTag.IndexOf(" ");
                    tagName = currentTag.Substring(1, endtagIndex).Trim();
                    currentTag = currentTag.Substring(endtagIndex+1);
                }

                // create new element
                XMLNode element = new XMLNode(tagName);

                // parse the attributes
                bool isTagClosed = false;
                while (currentTag.Length > 0)
                {
                    // remove the prepend or trailing white spaces
                    currentTag = currentTag.Trim();

                    if (currentTag == "/>")
                    {
                        // close tag
                        isTagClosed = true;
                        break;
                    }
                    else if (currentTag == ">")
                    {
                        // open tag
                        break;
                    }

                    index = currentTag.IndexOf("=");
                    if (index < 0)
                    {
                        Debug.LogError("Invalid attribute for tag '" +
                                tagName + "'.");
                        return null;
                    }

                    // get attribute name
                    string attributeName = currentTag.Substring(0, index);
                    currentTag = currentTag.Substring(index+1);

                    // get attribute value
                    string attributeValue;
                    bool isQuoted = true;
                    if (currentTag.StartsWith("\""))
                    {
                        index = currentTag.IndexOf('"', 1);
                    }
                    else if (currentTag.StartsWith("'"))
                    {
                        index = currentTag.IndexOf('\'', 1);
                    }
                    else
                    {
                        isQuoted = false;
                        index = currentTag.IndexOf(' ');
                        if (index < 0) {
                            index = currentTag.IndexOf('>');
                            if (index < 0) {
                                index = currentTag.IndexOf('/');
                            }
                        }
                    }

                    if (index < 0)
                    {
                        Debug.LogError("Invalid attribute for tag '" +
                                tagName + "'.");
                        return null;
                    }

                    if (isQuoted)
                        attributeValue = currentTag.Substring(1, index -1);
                    else
                        attributeValue = currentTag.Substring(0, index - 1);

                    // add attribute to the new element
                    element.attributes[attributeName]= attributeValue;

                    currentTag = currentTag.Substring(index+1);
                }

                // read the text between the open and close tag
                if (!isTagClosed)
                    element.content = ReadText();

                // add new element as a child element of
                // the current element
                if (currentElement != null)
                    currentElement.AddChild(element);

                if (!isTagClosed)
                {
                    if (currentElement != null)
                        elements.Push(currentElement);

                    currentElement = element;
                }
                else if (currentElement == null)
                {
                    // only has one tag in the document
                    return element;
                }
            }
        }
    }
    //---------------------------------------------------------------------------------
    // Parse
    //---------------------------------------------------------------------------------
    public XMLNode Parse(FlashCompatibleTextReader reader)
    {
        this.reader = reader;

        //check empty string
        if (reader.Peek() == -1)
        {
            return(null);
        }

        // skip xml declaration or DocTypes
        SkipPrologs();

        //check empty string
        if (reader.Peek() == -1)
        {
            return(null);
        }

        while (true)
        {
            SkipWhitespace();
            int    index;
            string tagName;

            // remove the prepend or trailing white spaces
            bool   startingBracket = (char)reader.Peek() == '<';
            string currentTag      = ReadTag(startingBracket).Trim();
            if (currentTag.StartsWith("<!"))
            {
                // Nothing to do, it's a comment
            }
            else if (currentTag.StartsWith("</"))
            {
                // close tag
                tagName = currentTag.Substring(2, currentTag.Length - 3);

                // no open tag
                if (currentElement == null)
                {
                    Debug.LogError("Got close tag '" + tagName +
                                   "' without open tag.");
                    return(null);
                }

                // close tag does not match with open tag
                if (tagName != currentElement.tag)
                {
                    Debug.LogError("Expected close tag for '" +
                                   currentElement.tag + "' but got '" +
                                   tagName + "'.");
                    return(null);
                }

                if (elements.Count == 0)
                {
                    return(currentElement);
                }
                else
                {
                    currentElement = (XMLNode)elements.Pop();
                }
            }
            else
            {
                // open tag or tag with both open and close tags
                index = currentTag.IndexOf('"');
                if (index < 0)
                {
                    index = currentTag.IndexOf('\'');
                }

                if (index < 0)
                {
                    // tag with no attributes
                    if (currentTag.EndsWith("/>"))
                    {
                        // close tag as well
                        tagName    = currentTag.Substring(1, currentTag.Length - 3).Trim();
                        currentTag = "/>";
                    }
                    else
                    {
                        // open tag
                        tagName    = currentTag.Substring(1, currentTag.Length - 2).Trim();
                        currentTag = "";
                    }
                }
                else
                {
                    // tag with attributes
                    int endtagIndex = currentTag.IndexOf(" ");
                    tagName    = currentTag.Substring(1, endtagIndex).Trim();
                    currentTag = currentTag.Substring(endtagIndex + 1);
                }

                // create new element
                XMLNode element = new XMLNode(tagName);

                // parse the attributes
                bool isTagClosed = false;
                while (currentTag.Length > 0)
                {
                    // remove the prepend or trailing white spaces
                    currentTag = currentTag.Trim();

                    if (currentTag == "/>")
                    {
                        // close tag
                        isTagClosed = true;
                        break;
                    }
                    else if (currentTag == ">")
                    {
                        // open tag
                        break;
                    }

                    index = currentTag.IndexOf("=");
                    if (index < 0)
                    {
                        Debug.LogError("Invalid attribute for tag '" +
                                       tagName + "'.");
                        return(null);
                    }

                    // get attribute name
                    string attributeName = currentTag.Substring(0, index);
                    currentTag = currentTag.Substring(index + 1);

                    // get attribute value
                    string attributeValue;
                    bool   isQuoted = true;
                    if (currentTag.StartsWith("\""))
                    {
                        index = currentTag.IndexOf('"', 1);
                    }
                    else if (currentTag.StartsWith("'"))
                    {
                        index = currentTag.IndexOf('\'', 1);
                    }
                    else
                    {
                        isQuoted = false;
                        index    = currentTag.IndexOf(' ');
                        if (index < 0)
                        {
                            index = currentTag.IndexOf('>');
                            if (index < 0)
                            {
                                index = currentTag.IndexOf('/');
                            }
                        }
                    }

                    if (index < 0)
                    {
                        Debug.LogError("Invalid attribute for tag '" +
                                       tagName + "'.");
                        return(null);
                    }

                    if (isQuoted)
                    {
                        attributeValue = currentTag.Substring(1, index - 1);
                    }
                    else
                    {
                        attributeValue = currentTag.Substring(0, index - 1);
                    }

                    // add attribute to the new element
                    element.attributes[attributeName] = attributeValue;

                    currentTag = currentTag.Substring(index + 1);
                }

                // read the text between the open and close tag
                if (!isTagClosed)
                {
                    element.content = ReadText();
                }

                // add new element as a child element of
                // the current element
                if (currentElement != null)
                {
                    currentElement.AddChild(element);
                }

                if (!isTagClosed)
                {
                    if (currentElement != null)
                    {
                        elements.Push(currentElement);
                    }

                    currentElement = element;
                }
                else if (currentElement == null)
                {
                    // only has one tag in the document
                    return(element);
                }
            }
        }
    }
    public XMLNode Parse(FlashCompatibleTextReader reader)
    {
        this.reader = reader;
        if (reader.Peek() == -1)
        {
            return(null);
        }
        SkipPrologs();
        if (reader.Peek() == -1)
        {
            return(null);
        }
        XMLNode xMLNode;

        while (true)
        {
            SkipWhitespace();
            bool   startingBracket = (ushort)reader.Peek() == 60;
            string text            = ReadTag(startingBracket).Trim();
            if (!text.StartsWith("<!"))
            {
                if (text.StartsWith("</"))
                {
                    string text2 = text.Substring(2, text.Length - 3);
                    if (currentElement == null)
                    {
                        Debug.LogError((object)("Got close tag '" + text2 + "' without open tag."));
                        return(null);
                    }
                    if (text2 != currentElement.tag)
                    {
                        Debug.LogError((object)("Expected close tag for '" + currentElement.tag + "' but got '" + text2 + "'."));
                        return(null);
                    }
                    if (elements.Count == 0)
                    {
                        return(currentElement);
                    }
                    currentElement = (XMLNode)elements.Pop();
                }
                else
                {
                    int num = text.IndexOf('"');
                    if (num < 0)
                    {
                        num = text.IndexOf('\'');
                    }
                    string text2;
                    if (num < 0)
                    {
                        if (text.EndsWith("/>"))
                        {
                            text2 = text.Substring(1, text.Length - 3).Trim();
                            text  = "/>";
                        }
                        else
                        {
                            text2 = text.Substring(1, text.Length - 2).Trim();
                            text  = string.Empty;
                        }
                    }
                    else
                    {
                        int num2 = text.IndexOf(" ");
                        text2 = text.Substring(1, num2).Trim();
                        text  = text.Substring(num2 + 1);
                    }
                    xMLNode = new XMLNode(text2);
                    bool flag = false;
                    while (text.Length > 0)
                    {
                        text = text.Trim();
                        if (text == "/>")
                        {
                            flag = true;
                            break;
                        }
                        if (text == ">")
                        {
                            break;
                        }
                        num = text.IndexOf("=");
                        if (num < 0)
                        {
                            Debug.LogError((object)("Invalid attribute for tag '" + text2 + "'."));
                            return(null);
                        }
                        string key = text.Substring(0, num);
                        text = text.Substring(num + 1);
                        bool flag2 = true;
                        if (text.StartsWith("\""))
                        {
                            num = text.IndexOf('"', 1);
                        }
                        else if (text.StartsWith("'"))
                        {
                            num = text.IndexOf('\'', 1);
                        }
                        else
                        {
                            flag2 = false;
                            num   = text.IndexOf(' ');
                            if (num < 0)
                            {
                                num = text.IndexOf('>');
                                if (num < 0)
                                {
                                    num = text.IndexOf('/');
                                }
                            }
                        }
                        if (num < 0)
                        {
                            Debug.LogError((object)("Invalid attribute for tag '" + text2 + "'."));
                            return(null);
                        }
                        string value = (!flag2) ? text.Substring(0, num - 1) : text.Substring(1, num - 1);
                        xMLNode.attributes[key] = value;
                        text = text.Substring(num + 1);
                    }
                    if (!flag)
                    {
                        xMLNode.content = ReadText();
                    }
                    if (currentElement != null)
                    {
                        currentElement.AddChild(xMLNode);
                    }
                    if (!flag)
                    {
                        if (currentElement != null)
                        {
                            elements.Push(currentElement);
                        }
                        currentElement = xMLNode;
                    }
                    else if (currentElement == null)
                    {
                        break;
                    }
                }
            }
        }
        return(xMLNode);
    }
示例#9
0
    private JSONNode ReadObject()
    {
        JSONNode jSONNode = new JSONNode();

        SkipWhitespace();
        if (reader.Peek() != 123)
        {
            Debug.LogError((object)"malformed json: no starting '{'");
            return(null);
        }
        reader.Read();
        while (reader.Peek() != 125)
        {
            if (reader.Peek() == 44)
            {
                reader.Read();
            }
            if (reader.Peek() == 125)
            {
                break;
            }
            SkipWhitespace();
            string fieldName = ReadFieldName().Trim();
            SkipWhitespace();
            reader.Read();
            IJSONFieldValue val = ReadValue();
            jSONNode.AddField(fieldName, val);
            SkipWhitespace();
        }
        reader.Read();
        return(jSONNode);
    }