Exemple #1
0
        public IEnumerable <bool> Parse()
        {
            LastCharIndex = -1;
            JSONPart Expected = JSONPart.VALUE;
            Stack <Dictionary <string, JsonObject> > ListStack = new Stack <Dictionary <string, JsonObject> >();
            Stack <JsonObject> JsonStack         = new Stack <JsonObject>();
            JsonObject         CurrentJsonObject = new JsonObject("");
            var trimChars = new char[] { '"', '\'', ' ', '\n', '\r', '\t' };
            var startTime = DateTime.Now;

            while (LastCharIndex < Serialized.Length - 1)
            {
                var charIndex = -1;
                switch (Expected)
                {
                case JSONPart.VALUE:
                    charIndex = Serialized.IndexOfAny(new char[] { '{', '}', ',' }, LastCharIndex + 1);
                    if (charIndex == -1)
                    {
                        throw new UnexpectedCharacterException(new char[] { '{', '}', ',' }, "EOF", LastCharIndex);
                    }
                    //Console.WriteLine("Expecting Value...");
                    //Console.WriteLine("Found " + Serialized[charIndex] + " (" + charIndex + ")");
                    switch (Serialized[charIndex])
                    {
                    case '{':
                        CurrentJsonObject.SetValue(new Dictionary <string, JsonObject>());
                        JsonStack.Push(CurrentJsonObject);
                        CurrentJsonObject = new JsonObject();
                        Expected          = JSONPart.KEY;
                        break;

                    case '}':
                    case ',':
                        var value = Serialized.Substring(LastCharIndex + 1, charIndex - LastCharIndex - 1).Trim(trimChars);
                        //Console.WriteLine("value is: '" + value + "'");
                        CurrentJsonObject.SetValue(value);

                        if (Serialized[charIndex] == '}')
                        {
                            if (charIndex < Serialized.Length - 1 && Serialized[charIndex + 1] == ',')
                            {
                                charIndex++;
                            }
                            CurrentJsonObject = JsonStack.Pop();
                        }

                        Expected = JSONPart.KEY;
                        break;
                    }
                    LastCharIndex = charIndex;
                    break;

                case JSONPart.KEY:
                    charIndex = Serialized.IndexOfAny(new char[] { '}', ':' }, LastCharIndex + 1);
                    //Console.WriteLine("Expecting Key...");
                    //Console.WriteLine("Found " + Serialized[charIndex] + " (" + charIndex + ")");
                    if (charIndex == -1)
                    {
                        throw new UnexpectedCharacterException(new char[] { '}', ':' }, "EOF", LastCharIndex);
                    }

                    switch (Serialized[charIndex])
                    {
                    case '}':
                        if (charIndex < Serialized.Length - 1 && Serialized[charIndex + 1] == ',')
                        {
                            charIndex++;
                        }
                        CurrentJsonObject = JsonStack.Pop();
                        Expected          = JSONPart.KEY;
                        break;

                    case ':':
                        var key = Serialized.Substring(LastCharIndex + 1, charIndex - LastCharIndex - 1).Trim(trimChars);
                        //Console.WriteLine("key is: '" + key + "'");
                        CurrentJsonObject = new JsonObject(key);
                        JsonStack.Peek().GetValue()
                        .Add(CurrentJsonObject.Key, CurrentJsonObject);
                        Expected = JSONPart.VALUE;
                        break;
                    }
                    LastCharIndex = charIndex;
                    break;
                }
                //Console.WriteLine("Iteration done, CurrentJsonObject is: '" + CurrentJsonObject.Key + "'");
                if (DateTime.Now - startTime > TimeSpan.FromMilliseconds(30))
                {
                    yield return(false);

                    startTime = DateTime.Now;
                }
            }

            Result = CurrentJsonObject;
            yield return(true);
        }
Exemple #2
0
        public IEnumerable <bool> Parse()
        {
            LastCharIndex = -1;
            JSONPart Expected = JSONPart.VALUE;
            Stack <Dictionary <string, JsonElement> > ListStack = new Stack <Dictionary <string, JsonElement> >();
            Stack <IJsonNonPrimitive> JsonStack = new Stack <IJsonNonPrimitive>();
            IJsonNonPrimitive         CurrentNestedJsonObject = null;
            IJsonNonPrimitive         LastNestedJsonObject    = null;
            //Func<object, JsonObject> Generator = JsonObject.NewJsonObject("", readOnly);
            var    trimChars      = new char[] { '"', '\'', ' ', '\n', '\r', '\t', '\f' };
            string Key            = "";
            var    keyDelims      = new char[] { '}', ':' };
            var    valueDelims    = new char[] { '{', '}', ',', '[', ']' };
            var    expectedDelims = valueDelims;
            var    charIndex      = -1;
            bool   isInsideList   = false;

            while (LastCharIndex < Serialized.Length - 1)
            {
                charIndex = Serialized.IndexOfAny(expectedDelims, LastCharIndex + 1);
                if (charIndex == -1)
                {
                    throw new UnexpectedCharacterException(expectedDelims, "EOF", LastCharIndex);
                }

                char foundChar = Serialized[charIndex];
                if (Expected == JSONPart.VALUE)
                {
                    //Console.WriteLine("Expecting Value...");
                    //Console.WriteLine("Found " + Serialized[charIndex] + " (" + charIndex + ")");
                    switch (foundChar)
                    {
                    case '[':
                        CurrentNestedJsonObject = new JsonList(Key);
                        JsonStack.Peek().Add(CurrentNestedJsonObject as JsonElement);
                        JsonStack.Push(CurrentNestedJsonObject);
                        //Console.WriteLine("List started");
                        break;

                    case '{':
                        //Console.WriteLine("Found new JsonObject");
                        CurrentNestedJsonObject = new JsonObject(Key);
                        if (JsonStack.Count > 0)
                        {
                            JsonStack.Peek().Add(CurrentNestedJsonObject as JsonElement);
                        }
                        JsonStack.Push(CurrentNestedJsonObject);
                        Expected       = JSONPart.KEY;
                        expectedDelims = keyDelims;
                        break;

                    case ',':
                    case '}':
                    case ']':
                        var value = Serialized.Substring(LastCharIndex + 1, charIndex - LastCharIndex - 1).Trim(trimChars);
                        //Console.WriteLine("value is: '" + value + "'");
                        JsonStack.Peek().Add(new JsonPrimitive(Key, value));
                        if (foundChar == '}' || foundChar == ']')
                        {
                            /*if (foundChar == ']')
                             * {
                             *  Console.WriteLine("Leaving List...");
                             * }
                             * else
                             * {
                             *  Console.WriteLine("Leaving JsonObject...");
                             * }*/
                            if (charIndex < Serialized.Length - 1 && Serialized[charIndex + 1] == ',')
                            {
                                charIndex++;
                            }
                            LastNestedJsonObject = JsonStack.Pop();
                        }
                        break;
                    }

                    isInsideList = JsonStack.Count == 0 || JsonStack.Peek() is JsonList;
                    if (isInsideList)
                    {
                        Key            = null;
                        Expected       = JSONPart.VALUE;
                        expectedDelims = valueDelims;
                    }
                    else
                    {
                        Expected       = JSONPart.KEY;
                        expectedDelims = keyDelims;
                    }
                }
                else if (Expected == JSONPart.KEY)
                {
                    //Console.WriteLine("Expecting Key...");
                    //Console.WriteLine("Found " + Serialized[charIndex] + " (" + charIndex + ")");

                    switch (Serialized[charIndex])
                    {
                    case ':':
                        Key = Serialized.Substring(LastCharIndex + 1, charIndex - LastCharIndex - 1).Trim(trimChars);
                        //Console.WriteLine("key is: '" + Key + "'");
                        //Generator = JsonObject.NewJsonObject(Key, readOnly);
                        Expected       = JSONPart.VALUE;
                        expectedDelims = valueDelims;
                        break;

                    case '}':
                        //Console.WriteLine("Leaving JsonObject...");
                        if (charIndex < Serialized.Length - 1 && Serialized[charIndex + 1] == ',')
                        {
                            charIndex++;
                        }
                        LastNestedJsonObject = JsonStack.Pop();
                        break;

                    default:
                        //Console.WriteLine($"Invalid character found: '{Serialized[charIndex]}', expected ':'!");
                        break;
                    }
                }

                LastCharIndex = charIndex;
                //Console.WriteLine("Iteration done, CurrentJsonObject is: '" + CurrentNestedJsonObject.Key + "'");
                if (ShouldPause())
                {
                    yield return(false);
                }
            }

            Result = LastNestedJsonObject as JsonElement;
            yield return(true);
        }