Esempio n. 1
0
        /// <summary>
        /// Writes the next value from the given <see cref="JsonReader"/>
        /// into this writer's output. If the reader is positioned
        /// at the root of JSON data, then the entire data will be
        /// written.
        /// </summary>

        public virtual void WriteFromReader(JsonReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            if (!reader.MoveToContent())
            {
                return;
            }

            if (reader.TokenClass == JsonTokenClass.String)
            {
                WriteString(reader.Text);
            }
            else if (reader.TokenClass == JsonTokenClass.Number)
            {
                WriteNumber(reader.Text);
            }
            else if (reader.TokenClass == JsonTokenClass.Boolean)
            {
                WriteBoolean(reader.Text == JsonBoolean.TrueText);
            }
            else if (reader.TokenClass == JsonTokenClass.Null)
            {
                WriteNull();
            }
            else if (reader.TokenClass == JsonTokenClass.Array)
            {
                WriteStartArray();
                reader.Read();

                while (reader.TokenClass != JsonTokenClass.EndArray)
                {
                    WriteFromReader(reader);
                }

                WriteEndArray();
            }
            else if (reader.TokenClass == JsonTokenClass.Object)
            {
                reader.Read();
                WriteStartObject();

                while (reader.TokenClass != JsonTokenClass.EndObject)
                {
                    WriteMember(reader.ReadMember());
                    WriteFromReader(reader);
                }

                WriteEndObject();
            }
            else
            {
                throw new JsonException(string.Format("{0} not expected.", reader.TokenClass));
            }

            reader.Read();
        }
        public void CannotInitializeWithReaderOnArray()
        {
            JsonReader reader = JsonText.CreateReader("[]");

            reader.MoveToContent();
            new FreeJsonMemberReadingHelper(reader);
        }
        public void InitializeWithReaderOnObjectStart()
        {
            JsonReader reader = JsonText.CreateReader("{}");

            reader.MoveToContent();
            new FreeJsonMemberReadingHelper(reader);
        }
Esempio n. 4
0
        public void MissingObjectMemberNameValueDelimiter()
        {
            JsonReader reader = CreateReader("{x 1}");

            reader.MoveToContent();
            reader.StepOut();
        }
Esempio n. 5
0
        public void UnterminatedArray()
        {
            JsonReader reader = CreateReader("[1,2");

            reader.MoveToContent();
            reader.StepOut();
        }
Esempio n. 6
0
        public void MissingObjectMember()
        {
            JsonReader reader = CreateReader("{x:1,/*y:2*/,z:3}");

            reader.MoveToContent();
            reader.StepOut();
        }
Esempio n. 7
0
        public void UnterminatedObject()
        {
            JsonReader reader = CreateReader("{x:1,y:2");

            reader.MoveToContent();
            reader.StepOut();
        }
        public void InitializeWithReaderOnMember()
        {
            JsonReader reader = JsonText.CreateReader("{foo:bar}");

            reader.MoveToContent();
            reader.ReadToken(JsonTokenClass.Object);
            new FreeJsonMemberReadingHelper(reader);
        }
Esempio n. 9
0
        public void NullMemberNameHarmless()
        {
            JsonReader reader = CreateReader("{null:null}");

            reader.MoveToContent();
            reader.ReadToken(JsonTokenClass.Object);
            Assert.AreEqual("null", reader.ReadMember());
            reader.ReadNull();
            Assert.AreSame(JsonTokenClass.EndObject, reader.TokenClass);
            Assert.IsFalse(reader.Read());
        }
Esempio n. 10
0
        public static JsonBuffer From(JsonReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            JsonBufferReader bufferReader = reader as JsonBufferReader;

            if (bufferReader != null)
            {
                return(bufferReader.BufferValue());
            }

            if (!reader.MoveToContent())
            {
                return(Empty);
            }

            if (reader.TokenClass == JsonTokenClass.Member)
            {
                reader.Read();
            }

            bool structured = reader.TokenClass == JsonTokenClass.Array ||
                              reader.TokenClass == JsonTokenClass.Object;

            JsonBufferWriter writer = new JsonBufferWriter();

            writer.MaxDepth = reader.MaxDepth;
            writer.WriteFromReader(reader);
            JsonBuffer buffer = writer.GetBuffer();

            if (!structured)
            {
                bufferReader = buffer.CreateReader();
                bufferReader.MoveToContent();
                bufferReader.Read();
                buffer = bufferReader.BufferValue();
            }

            return(buffer);
        }
Esempio n. 11
0
        private static object ReadParameters(Method method, JsonReader reader, ImportContext importContext)
        {
            Debug.Assert(method != null);
            Debug.Assert(reader != null);
            Debug.Assert(importContext != null);

            reader.MoveToContent();

            Parameter[] parameters = method.GetParameters();

            if (reader.TokenClass == JsonTokenClass.Array)
            {
                reader.Read();
                ArrayList argList = new ArrayList(parameters.Length);

                // TODO: This loop could bomb when more args are supplied that parameters available.

                for (int i = 0; i < parameters.Length && reader.TokenClass != JsonTokenClass.EndArray; i++)
                    argList.Add(importContext.Import(parameters[i].ParameterType, reader));

                reader.StepOut();
                return argList.ToArray();
            }
            else if (reader.TokenClass == JsonTokenClass.Object)
            {
                reader.Read();
                JsonObject argByName = new JsonObject();

                while (reader.TokenClass != JsonTokenClass.EndObject)
                {
                    // TODO: Imporve this lookup.
                    // FIXME: Does not work when argument is positional.

                    Type parameterType = AnyType.Value;
                    string name = reader.ReadMember();

                    foreach (Parameter parameter in parameters)
                    {
                        if (parameter.Name.Equals(name))
                        {
                            parameterType = parameter.ParameterType;
                            break;
                        }
                    }

                    argByName.Put(name, importContext.Import(parameterType, reader));
                }

                reader.Read();
                return argByName;
            }
            else
            {
                return importContext.Import(reader);
            }
        }
Esempio n. 12
0
        public static JsonBuffer From(JsonReader reader)
        {
            if (reader == null)
                throw new ArgumentNullException("reader");

            JsonBufferReader bufferReader = reader as JsonBufferReader;
            if (bufferReader != null)
                return bufferReader.BufferValue();

            if (!reader.MoveToContent())
                return Empty;

            if (reader.TokenClass == JsonTokenClass.Member)
                reader.Read();

            bool structured = reader.TokenClass == JsonTokenClass.Array
                              || reader.TokenClass == JsonTokenClass.Object;

            JsonBufferWriter writer = new JsonBufferWriter();
            writer.WriteFromReader(reader);
            JsonBuffer buffer = writer.GetBuffer();

            if (!structured)
            {
                bufferReader = buffer.CreateReader();
                bufferReader.MoveToContent();
                bufferReader.Read();
                buffer = bufferReader.BufferValue();
            }

            return buffer;
        }
Esempio n. 13
0
        /// <summary>
        /// Writes the next value from the given <see cref="JsonReader"/>
        /// into this writer's output. If the reader is positioned
        /// at the root of JSON data, then the entire data will be
        /// written.
        /// </summary>

        public virtual void WriteFromReader(JsonReader reader)
        {
            if (reader == null)            
                throw new ArgumentNullException("reader");

            if (!reader.MoveToContent())
                return;

            if (reader.TokenClass == JsonTokenClass.String)
            {
                WriteString(reader.Text); 
            }
            else if (reader.TokenClass == JsonTokenClass.Number)
            {
                WriteNumber(reader.Text);
            }
            else if (reader.TokenClass == JsonTokenClass.Boolean)
            {
                WriteBoolean(reader.Text == JsonBoolean.TrueText); 
            }
            else if (reader.TokenClass == JsonTokenClass.Null)
            {
                WriteNull();
            }
            else if (reader.TokenClass == JsonTokenClass.Array)
            {
                WriteStartArray();
                reader.Read();

                while (reader.TokenClass != JsonTokenClass.EndArray)
                    WriteFromReader(reader);

                WriteEndArray();
            }
            else if (reader.TokenClass == JsonTokenClass.Object)
            {
                reader.Read();
                WriteStartObject();
                    
                while (reader.TokenClass != JsonTokenClass.EndObject)
                {
                    WriteMember(reader.ReadMember());
                    WriteFromReader(reader);
                }

                WriteEndObject();
            }
            else 
            {
                throw new JsonException(string.Format("{0} not expected.", reader.TokenClass));
            }

            reader.Read();
        }
Esempio n. 14
0
        void IJsonImportable.Import(ImportContext context, JsonReader reader)
        {
            reader.MoveToContent();

            if (reader.TokenClass != JsonTokenClass.Object)
            {
                reader.Skip();
                return;
            }

            reader.Read(/* object */);

            do
            {
                var brushName = reader.ReadMember().ToLowerInvariant();
                var color = reader.ReadString();

                var foreground = EnumHelper.TryParse<ConsoleColor>(color, true) ?? DefaultBrush.Foreground;

                switch (brushName)
                {
                    case "arr":
                    case "array":
                        Array = Array.ResetForeground(foreground);
                        break;
                    case "obj":
                    case "object":
                        Object = Object.ResetForeground(foreground);
                        break;
                    case "mem":
                    case "member":
                        Member = Member.ResetForeground(foreground);
                        break;
                    case "str":
                    case "string":
                        String = String.ResetForeground(foreground);
                        break;
                    case "num":
                    case "number":
                        Number = Number.ResetForeground(foreground);
                        break;
                    case "bit":
                    case "boolean":
                        Boolean = Boolean.ResetForeground(foreground);
                        break;
                    case "nil":
                    case "null":
                        Null = Null.ResetForeground(foreground);
                        break;
                    default:
                        continue;
                }
            }
            while (reader.TokenClass != JsonTokenClass.EndObject);

            reader.Read( /* end object */);
        }
Esempio n. 15
0
        public void WriteValueFromReader(JsonReader reader)
        {
            if (reader == null)            
                throw new ArgumentNullException("reader");

            if (!reader.MoveToContent())
                return;

            switch (reader.Token)
            {
                case JsonToken.String: WriteString(reader.Text); break;
                case JsonToken.Number: WriteNumber(reader.Text); break;
                case JsonToken.Boolean : WriteBoolean(reader.Text == JsonReader.FalseText); break;
                case JsonToken.Null : WriteNull(); break;

                case JsonToken.Object :
                {
                    WriteStartObject();
                    reader.ReadMember();

                    do
                    {
                        WriteMember(reader.Text);
                        reader.Read();
                        WriteValueFromReader(reader);
                    }
                    while (reader.Token != JsonToken.EndObject);

                    WriteEndObject();
                    break;
                }
            
                case JsonToken.Array :
                {
                    WriteStartArray();
                    reader.Read();

                    while (reader.Token != JsonToken.EndArray)
                        WriteValueFromReader(reader);

                    WriteEndArray();
                    break;
                }

                default : throw new JsonException(string.Format("{0} not expected.", reader.Token));
            }

            reader.Read();
        }
        /// <summary>
        /// Attempts to locate a member with a given (case-sensitive) name
        /// and returns a <see cref="JsonReader"/> that can be used to read
        /// the value. Otherwise it returns <c>null</c>.
        /// </summary>
        /// <remarks>
        /// The caller should not use the returned <see cref="JsonReader"/>
        /// instance for any other purpose but reading the value. It is
        /// possible that this method will return the same instance as
        /// <see cref="BaseReader"/> or a separate instance.
        /// </remarks>

        public JsonReader TryReadMember(string name)
        {
            //
            // Is the member already buffered? If yes then return a reader
            // on its buffered value.
            //

            JsonBuffer value = TryPopBufferedMember(name);

            if (!value.IsEmpty)
            {
                return(value.CreateReader());
            }

            //
            // Use the base reader from here on if it has not already been
            // exhausted...
            //

            if (_ended)
            {
                return(null);
            }

            JsonReader reader = BaseReader;

            if (!_started)
            {
                _started = true;
                if (!reader.MoveToContent())
                {
                    throw new JsonException(string.Format(
                                                @"Unexpected EOF while attempting to look for member named '{0}'.",
                                                name));
                }

                JsonTokenClass clazz = reader.TokenClass;
                if (clazz != JsonTokenClass.Object &&
                    clazz != JsonTokenClass.Member)
                {
                    throw new JsonException(string.Format(
                                                @"Found {0} where a JSON Object or Member was expected.", clazz));
                }
            }

            //
            // If the base reader is sitting on the start of an object then
            // move into it. This case should only arise on the first read
            // into a JSON object.
            //

            if (reader.TokenClass == JsonTokenClass.Object)
            {
                reader.Read();
            }

            //
            // Go over the entire JSON object until its end.
            //

            while (reader.TokenClass != JsonTokenClass.EndObject)
            {
                //
                // Read the next member and if it matches what's being
                // sought then simply return the base reader that
                // should be aligned on the value.
                //

                string actualName = reader.ReadMember();
                if (string.CompareOrdinal(actualName, name) == 0)
                {
                    return(reader);
                }

                //
                // Not the sought member so buffer it to be served
                // later when it is sought or as part of the tail.
                //

                NamedJsonBufferList members = _members;
                if (members == null)
                {
                    members = _members = new NamedJsonBufferList(4);
                }
                members.Add(new NamedJsonBuffer(actualName, JsonBuffer.From(reader)));
            }

            _ended = true;

            //
            // Member not found.
            //

            return(null);
        }