ThirdParty.Json.LitJson.JsonReader.Read C# (CSharp) Method

Read() public method

public Read ( ) : bool
return bool
        public bool Read()
        {
            if (end_of_input)
                return false;

            if (end_of_json)
            {
                end_of_json = false;
            }

            token = JsonToken.None;
            parser_in_string = false;
            parser_return = false;

            // Check if the first read call. If so then do an extra ReadToken because Read assumes that the previous
            // call to Read has already called ReadToken.
            if (!read_started)
            {
                read_started = true;

                if (!ReadToken())
                    return false;
            }

            do
            {
                current_symbol = current_input;
                ProcessSymbol();
                if (parser_return)
                {
                    if (this.token == JsonToken.ObjectStart || this.token == JsonToken.ArrayStart)
                    {
                        depth.Push(this.token);
                    }
                    else if (this.token == JsonToken.ObjectEnd || this.token == JsonToken.ArrayEnd)
                    {
                        // Clear out property name if is on top. This could happen if the value for the property was null.
                        if (depth.Peek() == JsonToken.PropertyName)
                            depth.Pop();

                        // Pop the opening token for this closing token. Make sure it is of the right type otherwise 
                        // the document is invalid.
                        var opening = depth.Pop();
                        if (this.token == JsonToken.ObjectEnd && opening != JsonToken.ObjectStart)
                            throw new JsonException("Error: Current token is ObjectEnd which does not match the opening " + opening.ToString());
                        if (this.token == JsonToken.ArrayEnd && opening != JsonToken.ArrayStart)
                            throw new JsonException("Error: Current token is ArrayEnd which does not match the opening " + opening.ToString());

                        // If that last element is popped then we reached the end of the JSON object.
                        if (depth.Count == 0)
                        {
                            end_of_json = true;
                        }
                    }
                    // If the top of the stack is an object start and the next read is a string then it must be a property name
                    // to add to the stack.
                    else if (depth.Count > 0 && depth.Peek() != JsonToken.PropertyName &&
                        this.token == JsonToken.String && depth.Peek() == JsonToken.ObjectStart)
                    {
                        this.token = JsonToken.PropertyName;
                        depth.Push(this.token);
                    }

                    if (
                        (this.token == JsonToken.ObjectEnd ||
                        this.token == JsonToken.ArrayEnd ||
                        this.token == JsonToken.String ||
                        this.token == JsonToken.Boolean ||
                        this.token == JsonToken.Double ||
                        this.token == JsonToken.Int ||
                        this.token == JsonToken.UInt ||
                        this.token == JsonToken.Long ||
                        this.token == JsonToken.ULong ||
                        this.token == JsonToken.Null ||
                        this.token == JsonToken.String
                        ))
                    {
                        // If we found a value but we are not in an array or object then the document is an invalid json document.
                        if (depth.Count == 0)
                        {
                            if (this.token != JsonToken.ArrayEnd && this.token != JsonToken.ObjectEnd)
                            {
                                throw new JsonException("Value without enclosing object or array");
                            }
                        }
                        // The valud of the property has been processed so pop the property name from the stack.
                        else if (depth.Peek() == JsonToken.PropertyName)
                        {
                            depth.Pop();
                        }
                    }

                    // Read the next token that will be processed the next time the Read method is called.
                    // This is done ahead of the next read so we can detect if we are at the end of the json document.
                    // Otherwise EndOfInput would not return true until an attempt to read was made.
                    if (!ReadToken() && depth.Count != 0)
                        throw new JsonException("Incomplete JSON Document");
                    return true;
                }
            } while (ReadToken());

            // If we reached the end of the document but there is still elements left in the depth stack then
            // the document is invalid JSON.
            if (depth.Count != 0)
                throw new JsonException("Incomplete JSON Document");

            end_of_input = true;
            return false;
        }

Usage Example

Ejemplo n.º 1
0
        private static IJsonWrapper ReadValue(WrapperFactory factory,
                                              JsonReader reader)
        {
            reader.Read();

            if (reader.Token == JsonToken.ArrayEnd ||
                reader.Token == JsonToken.Null)
            {
                return(null);
            }

            IJsonWrapper instance = factory();

            if (reader.Token == JsonToken.String)
            {
                instance.SetString((string)reader.Value);
                return(instance);
            }

            if (reader.Token == JsonToken.Double)
            {
                instance.SetDouble((double)reader.Value);
                return(instance);
            }

            if (reader.Token == JsonToken.Int)
            {
                instance.SetInt((int)reader.Value);
                return(instance);
            }

            if (reader.Token == JsonToken.Long)
            {
                instance.SetLong((long)reader.Value);
                return(instance);
            }

            if (reader.Token == JsonToken.Boolean)
            {
                instance.SetBoolean((bool)reader.Value);
                return(instance);
            }

            if (reader.Token == JsonToken.ArrayStart)
            {
                instance.SetJsonType(JsonType.Array);

                while (true)
                {
                    IJsonWrapper item = ReadValue(factory, reader);
                    // nij - added check to see if the item is not null.  This is to handle arrays within arrays.
                    // In those cases when the outer array read the inner array an item was returned back the current
                    // reader.Token is at the ArrayEnd for the inner array.
                    if (item == null && reader.Token == JsonToken.ArrayEnd)
                    {
                        break;
                    }

                    ((IList)instance).Add(item);
                }
            }
            else if (reader.Token == JsonToken.ObjectStart)
            {
                instance.SetJsonType(JsonType.Object);

                while (true)
                {
                    reader.Read();

                    if (reader.Token == JsonToken.ObjectEnd)
                    {
                        break;
                    }

                    string property = (string)reader.Value;

                    ((IDictionary)instance)[property] = ReadValue(
                        factory, reader);
                }
            }

            return(instance);
        }
All Usage Examples Of ThirdParty.Json.LitJson.JsonReader::Read