예제 #1
0
        static public Hashtable TraverseObject(JSONElement el)
        {
            var    obj = new Hashtable();
            string key = null;

            foreach (var item in el.child)
            {
                if (item.scope == "key")
                {
                    key = item.value;
                }
                else if (item.scope == "value")
                {
                    switch (item.type)
                    {
                    case "nil":
                        obj.Add(key, null);
                        break;

                    case "s":
                        obj.Add(key, item.value);
                        break;

                    case "t":
                        obj.Add(key, true);
                        break;

                    case "f":
                        obj.Add(key, false);
                        break;

                    case "n":
                    {
                        double number;
                        Double.TryParse(item.value, out number);
                        obj.Add(key, number);
                        break;
                    }

                    case "array":
                        obj.Add(key, TraverseArray(item));
                        break;

                    case "object":
                        obj.Add(key, TraverseObject(item));
                        break;

                    default:
                        break;
                    }
                }
            }

            return(obj);
        }
예제 #2
0
 static public Object Generate(JSONElement el)
 {
     if (el.type == "array")
     {
         return(TraverseArray(el));
     }
     else if (el.type == "object")
     {
         return(TraverseObject(el));
     }
     return(null);
 }
예제 #3
0
        static public ArrayList TraverseArray(JSONElement el)
        {
            var arr = new ArrayList();

            foreach (var item in el.child)
            {
                switch (item.type)
                {
                case "nil":
                    arr.Add(null);
                    break;

                case "s":
                    arr.Add(item.value);
                    break;

                case "t":
                    arr.Add(true);
                    break;

                case "f":
                    arr.Add(false);
                    break;

                case "n":
                {
                    double number;
                    Double.TryParse(item.value, out number);
                    arr.Add(number);
                    break;
                }

                case "array":
                    arr.Add(TraverseArray(item));
                    break;

                case "object":
                    arr.Add(TraverseObject(item));
                    break;

                default:
                    break;
                }
            }

            return(arr);
        }
예제 #4
0
파일: Core.cs 프로젝트: forek/MyJSONParser
        static public JSONElement Parse(Tokens tokens)
        {
            var         result = new JSONElement("root");
            JSONElement p      = result;

            while (tokens.ToArray().Length > 0)
            {
                var curr = tokens.Shift();
                switch (curr.type)
                {
                case "{":
                {
                    var node = new JSONElement("object", p, null, "value");
                    p.AddChild(node);
                    p = node;
                    break;
                }

                case "[":
                {
                    var node = new JSONElement("array", p, null, "value");
                    p.AddChild(node);
                    p = node;
                    break;
                }

                case "}":
                {
                    var lastChild = p.GetLastChild();
                    if (
                        p.type == "object" &&
                        (lastChild == null || lastChild.scope == "value")
                        )
                    {
                        p = p.parent;
                    }
                    else
                    {
                        ThrowError(curr);
                    }
                    break;
                }

                case "]":
                {
                    var lastChild = p.GetLastChild();
                    if (
                        p.type == "array" &&
                        (lastChild == null || lastChild.scope == "value")
                        )
                    {
                        p = p.parent;
                    }
                    else
                    {
                        ThrowError(curr);
                    }
                    break;
                }

                case "s":
                {
                    var lastChild = p.GetLastChild();
                    if (p.type == "object")
                    {
                        if (lastChild == null || lastChild.type == ",")
                        {
                            p.AddChild(new JSONElement(curr.type, p, curr.value, "key"));
                        }
                        else if (lastChild != null && lastChild.type == ":")
                        {
                            p.AddChild(new JSONElement(curr.type, p, curr.value, "value"));
                        }
                        else
                        {
                            ThrowError(curr);
                        }
                    }
                    else if (p.type == "array")
                    {
                        if (lastChild == null || lastChild.type == ",")
                        {
                            p.AddChild(new JSONElement(curr.type, p, curr.value, "value"));
                        }
                        else
                        {
                            ThrowError(curr);
                        }
                    }
                    break;
                }

                case "n":
                case "t":
                case "f":
                case "nil":
                {
                    var lastChild = p.GetLastChild();
                    if (
                        (p.type == "object" && (lastChild != null && lastChild.type == ":")) ||
                        (p.type == "array" && (lastChild == null || lastChild.type == ","))
                        )
                    {
                        p.AddChild(new JSONElement(curr.type, p, curr.value, "value"));
                    }
                    else
                    {
                        ThrowError(curr);
                    }
                    break;
                }

                case ",":
                {
                    var lastChild = p.GetLastChild();
                    if (lastChild != null && lastChild.scope == "value")
                    {
                        p.AddChild(new JSONElement(curr.type, p, curr.value));
                    }
                    else
                    {
                        ThrowError(curr);
                    }
                    break;
                }

                case ":":
                {
                    var lastChild = p.GetLastChild();
                    if (lastChild != null && lastChild.scope == "key")
                    {
                        p.AddChild(new JSONElement(curr.type, p, curr.value));
                    }
                    else
                    {
                        ThrowError(curr);
                    }
                    break;
                }

                case "space":
                {
                    break;
                }

                default:
                {
                    ThrowError(curr);
                    break;
                }
                }
            }

            if (result != p)
            {
                throw (new ApplicationException($"Unexpected token"));
            }

            return(result.child[0]);
        }
예제 #5
0
파일: Core.cs 프로젝트: forek/MyJSONParser
 public void AddChild(JSONElement el)
 {
     child.Add(el);
 }