示例#1
0
        private static JsonObject ParseJson(string jsonString, int start, out int end)
        {
            var resultDictionary = new JsonObject();

            bool           escBegin = false;
            bool           escEnd   = false;
            bool           inQuotes = false;
            string         key      = null;
            int            cEnd;
            var            tokenBuilder = new StringBuilder();
            JsonObject     child        = null;
            IList <object> arrayList    = null;
            int            autoKey      = 0;

            for (int i = start; i < jsonString.Length; i++)
            {
                char c = jsonString[i];

                if (c == '\\')
                {
                    escBegin = !escBegin;
                }

                if (!escBegin)
                {
                    if (c == '"')
                    {
                        inQuotes = !inQuotes;
                        if (!inQuotes && arrayList != null)
                        {
                            tokenBuilder.Append(c);
                            arrayList.Add(ParseValue(tokenBuilder.ToString()));
                            tokenBuilder.Length = 0;
                            continue;
                        }
                    }
                    if (!inQuotes)
                    {
                        switch (c)
                        {
                        case '{':
                            if (i != start)
                            {
                                child = ParseJson(jsonString, i, out cEnd);
                                if (arrayList != null)
                                {
                                    arrayList.Add(child);
                                }
                                else
                                {
                                    resultDictionary.Add(key, child);
                                    key = null;
                                }
                                i = cEnd;
                            }
                            continue;

                        case '}':
                            end = i;
                            if (key != null)
                            {
                                if (arrayList != null)
                                {
                                    resultDictionary.Add(key, arrayList);
                                }
                                else
                                {
                                    resultDictionary.Add(key, ParseValue(tokenBuilder.ToString()));
                                }
                            }
                            return(resultDictionary);

                        case '[':
                            arrayList = new List <object>();
                            continue;

                        case ']':
                            if (key == null)
                            {
                                key = "array" + autoKey.ToString();
                                autoKey++;
                            }
                            if (arrayList != null && tokenBuilder.Length > 0)
                            {
                                arrayList.Add(ParseValue(tokenBuilder.ToString()));
                                tokenBuilder.Length = 0;
                            }
                            resultDictionary.Add(key, arrayList);
                            arrayList = null;
                            key       = null;
                            continue;

                        case ',':
                            if (arrayList == null && key != null)
                            {
                                resultDictionary.Add(key, ParseValue(tokenBuilder.ToString()));
                                key = null;
                                tokenBuilder.Length = 0;
                            }
                            if (arrayList != null && tokenBuilder.Length > 0)
                            {
                                arrayList.Add(ParseValue(tokenBuilder.ToString()));
                                tokenBuilder.Length = 0;
                            }
                            continue;

                        case ':':
                            key = ParseValue(tokenBuilder.ToString()).ToString();
                            tokenBuilder.Length = 0;
                            continue;

                        case ' ':
                        case '\t':
                        case '\r':
                        case '\n':
                            continue;
                        }
                    }
                }

                tokenBuilder.Append(c);

                if (escEnd)
                {
                    escBegin = false;
                }

                if (escBegin)
                {
                    escEnd = true;
                }
                else
                {
                    escEnd = false;
                }
            }
            end = jsonString.Length - 1;
            return(resultDictionary); //theoretically shouldn't ever get here
        }