예제 #1
0
파일: Json.cs 프로젝트: nir94/Eddie
        private static bool FromJson(string s, bool canThrowException, out object result, out string remain)
        {
            try
            {
                s = s.TrimStart();

                if (s == "")
                {
                    throw new Exception("Empty value");
                }

                if (s[0] == '[')
                {
                    s = s.Substring(1).TrimStart();
                    Json a = new Json();
                    a.InitAsArray();
                    for (;;)
                    {
                        if (s[0] == ']')
                        {
                            break;
                        }
                        object v;
                        FromJson(s, canThrowException, out v, out s);
                        a.Append(v);
                        s = s.TrimStart();
                        if (s[0] == ',')
                        {
                            s = s.Substring(1).TrimStart();
                        }
                    }
                    result = a;
                    remain = s.Substring(1).TrimStart();
                    return(true);
                }
                else if (s[0] == '{')
                {
                    s = s.Substring(1).TrimStart();
                    Json a = new Json();
                    a.InitAsDictionary();
                    for (;;)
                    {
                        if (s[0] == '}')
                        {
                            break;
                        }
                        object k;
                        FromJson(s, canThrowException, out k, out s);
                        s = s.TrimStart();
                        if (s[0] == ':')
                        {
                            s = s.Substring(1).TrimStart();
                            object v;
                            FromJson(s, canThrowException, out v, out s);
                            a.SetKey(k as string, v);
                            s = s.TrimStart();
                            if (s[0] == ',')
                            {
                                s = s.Substring(1).TrimStart();
                            }
                        }
                        else
                        {
                            throw new Exception("Syntax error");
                        }
                    }
                    result = a;
                    remain = s.Substring(1).TrimStart();
                    return(true);
                }
                else
                {
                    // Direct value
                    bool inQuote = false;

                    int i = 0;
                    for (i = 0; i < s.Length + 1; i++)
                    {
                        char ch = (char)0;
                        if (i < s.Length)
                        {
                            ch = s[i];
                        }

                        if (inQuote)
                        {
                            if ((ch == '\"') && ((i == 0) || (s[i - 1] != '\\')))
                            {
                                inQuote = false;
                            }
                        }
                        else
                        {
                            if ((ch == '\"') && ((i == 0) || (s[i - 1] != '\\')))
                            {
                                inQuote = true;
                            }
                            else if ((ch == (char)0) ||
                                     (ch == ',') ||
                                     (ch == ':') ||
                                     (ch == '}') ||
                                     (ch == ']'))
                            {
                                // Valore singolo
                                string value = s.Substring(0, i).Trim();

                                if ((value.StartsWith("\"")) && (value.EndsWith("\"")))
                                {
                                    result = DecodeString(value.Substring(1, value.Length - 2));
                                    remain = s.Substring(i);
                                    return(true);
                                }

                                if (value == "null")
                                {
                                    result = null;
                                    remain = s.Substring(i);
                                    return(true);
                                }

                                if (value == "true")
                                {
                                    result = true;
                                    remain = s.Substring(i);
                                    return(true);
                                }

                                if (value == "false")
                                {
                                    result = false;
                                    remain = s.Substring(i);
                                    return(true);
                                }

                                int dI = 0;
                                if (int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out dI))
                                {
                                    result = dI;
                                    remain = s.Substring(i);
                                    return(true);
                                }

                                double dD = 0;
                                if (double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out dD))
                                {
                                    result = dD;
                                    remain = s.Substring(i);
                                    return(true);
                                }

                                throw new Exception("Cannot detect type of value '" + value + "'");
                            }
                        }
                    }
                    throw new Exception("Syntax error");
                }
            }
            catch (Exception e)
            {
                if (canThrowException)
                {
                    throw new Exception("JsonParser:" + e.Message);
                }
            }
            result = null;
            remain = s;
            return(false);
        }