Esempio n. 1
0
        internal void AddValue(RavenJValue value, JsonToken token)
        {
            if (_tokenStack.Count != 0)
            {
                var currentToken = this.CurrentToken;
                switch (currentToken.Type)
                {
                case JTokenType.Object:
                    ((RavenJObject)currentToken)[_tempPropName] = value;
                    _tempPropName = null;
                    break;

                case JTokenType.Array:
                    ((RavenJArray)currentToken).Add(value);
                    break;

                default:
                    throw new JsonWriterException("Unexpected token: " + token);
                }
            }
            else
            {
                _value = value;
            }
        }
Esempio n. 2
0
        internal void AddValue(RavenJValue value, JsonToken token)
        {
            if (_tokenStack.Count == 0)
            {
                _value = value;
            }
            else
            {
                switch (CurrentToken.Type)
                {
                case JTokenType.Object:
                    if (string.IsNullOrEmpty(_tempPropName))
                    {
                        throw new JsonWriterException("Unexpected value token");
                    }
                    ((RavenJObject)CurrentToken)[_tempPropName] = value;
                    _tempPropName = null;
                    break;

                case JTokenType.Array:
                    if (!string.IsNullOrEmpty(_tempPropName))
                    {
                        throw new JsonWriterException("Unexpected property token");
                    }
                    ((RavenJArray)CurrentToken).Add(value);
                    break;

                default:
                    throw new JsonWriterException("Unexpected token: " + token);
                }
            }
        }
Esempio n. 3
0
        public static async Task <RavenJToken> LoadAsync(JsonTextReaderAsync reader)
        {
            if (reader.TokenType == JsonToken.None)
            {
                if (!await reader.ReadAsync().ConfigureAwait(false))
                {
                    throw new Exception("Error reading RavenJArray from JsonReader.");
                }
            }

            if (reader.TokenType != JsonToken.StartArray)
            {
                throw new Exception("Error reading RavenJArray from JsonReader. Current JsonReader item is not an array: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
            }

            if (await reader.ReadAsync().ConfigureAwait(false) == false)
            {
                throw new Exception("Unexpected end of json array");
            }

            var         ar  = new RavenJArray();
            RavenJToken val = null;

            do
            {
                switch (reader.TokenType)
                {
                case JsonToken.Comment:
                    // ignore comments
                    break;

                case JsonToken.EndArray:
                    return(ar);

                case JsonToken.StartObject:
                    val = await RavenJObject.LoadAsync(reader).ConfigureAwait(false);

                    ar.Items.Add(val);
                    break;

                case JsonToken.StartArray:
                    val = await LoadAsync(reader).ConfigureAwait(false);

                    ar.Items.Add(val);
                    break;

                default:
                    val = RavenJValue.Load(reader);
                    ar.Items.Add(val);
                    break;
                }
            } while (await reader.ReadAsync().ConfigureAwait(false));

            throw new Exception("Error reading RavenJArray from JsonReader.");
        }
Esempio n. 4
0
        public new static RavenJArray Load(JsonReader reader)
        {
            ValidationUtils.ArgumentNotNull(reader, "reader");

            if (reader.TokenType == JsonToken.None)
            {
                if (!reader.Read())
                {
                    throw new Exception("Error reading RavenJArray from JsonReader.");
                }
            }

            if (reader.TokenType != JsonToken.StartArray)
            {
                throw new Exception("Error reading RavenJArray from JsonReader. Current JsonReader item is not an array: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
            }

            if (reader.Read() == false)
            {
                throw new Exception("Unexpected end of json array");
            }

            var         ar  = new RavenJArray();
            RavenJToken val = null;

            do
            {
                switch (reader.TokenType)
                {
                case JsonToken.Comment:
                    // ignore comments
                    break;

                case JsonToken.EndArray:
                    return(ar);

                case JsonToken.StartObject:
                    val = RavenJObject.Load(reader);
                    ar.Items.Add(val);
                    break;

                case JsonToken.StartArray:
                    val = RavenJArray.Load(reader);
                    ar.Items.Add(val);
                    break;

                default:
                    val = RavenJValue.Load(reader);
                    ar.Items.Add(val);
                    break;
                }
            } while (reader.Read());

            throw new Exception("Error reading RavenJArray from JsonReader.");
        }
Esempio n. 5
0
        /// <summary>
        /// Loads an <see cref="RavenJObject"/> from a <see cref="JsonReader"/>.
        /// </summary>
        /// <param name="reader">A <see cref="JsonReader"/> that will be read for the content of the <see cref="RavenJObject"/>.</param>
        /// <returns>A <see cref="RavenJObject"/> that contains the JSON that was read from the specified <see cref="JsonReader"/>.</returns>
        public new static RavenJObject Load(JsonReader reader)
        {
            ValidationUtils.ArgumentNotNull(reader, "reader");

            if (reader.TokenType == JsonToken.None)
            {
                if (!reader.Read())
                {
                    throw new Exception("Error reading RavenJObject from JsonReader.");
                }
            }

            if (reader.TokenType != JsonToken.StartObject)
            {
                throw new Exception(
                          "Error reading RavenJObject from JsonReader. Current JsonReader item is not an object: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
            }

            if (reader.Read() == false)
            {
                throw new Exception("Unexpected end of json object");
            }

            string propName = null;
            var    o        = new RavenJObject();

            do
            {
                switch (reader.TokenType)
                {
                case JsonToken.Comment:
                    // ignore comments
                    break;

                case JsonToken.PropertyName:
                    propName = reader.Value.ToString();
                    break;

                case JsonToken.EndObject:
                    return(o);

                case JsonToken.StartObject:
                    if (!string.IsNullOrEmpty(propName))
                    {
                        var val = RavenJObject.Load(reader);
                        o[propName] = val;                                 // TODO: Assert when o.Properties.ContainsKey and its value != val
                        propName    = null;
                    }
                    else
                    {
                        throw new InvalidOperationException("The JsonReader should not be on a token of type {0}."
                                                            .FormatWith(CultureInfo.InvariantCulture,
                                                                        reader.TokenType));
                    }
                    break;

                case JsonToken.StartArray:
                    if (!string.IsNullOrEmpty(propName))
                    {
                        var val = RavenJArray.Load(reader);
                        o[propName] = val;                                 // TODO: Assert when o.Properties.ContainsKey and its value != val
                        propName    = null;
                    }
                    else
                    {
                        throw new InvalidOperationException("The JsonReader should not be on a token of type {0}."
                                                            .FormatWith(CultureInfo.InvariantCulture,
                                                                        reader.TokenType));
                    }
                    break;

                default:
                    if (!string.IsNullOrEmpty(propName))
                    {
                        var val = RavenJValue.Load(reader);
                        o[propName] = val;         // TODO: Assert when o.Properties.ContainsKey and its value != val
                        propName    = null;
                    }
                    else
                    {
                        throw new InvalidOperationException("The JsonReader should not be on a token of type {0}."
                                                            .FormatWith(CultureInfo.InvariantCulture,
                                                                        reader.TokenType));
                    }
                    break;
                }
            } while (reader.Read());

            throw new Exception("Error reading RavenJObject from JsonReader.");
        }
Esempio n. 6
0
        public static async Task <RavenJToken> LoadAsync(JsonTextReaderAsync reader)
        {
            if (reader.TokenType == JsonToken.None)
            {
                if (!await reader.ReadAsync().ConfigureAwait(false))
                {
                    throw new Exception("Error reading RavenJObject from JsonReader.");
                }
            }

            if (reader.TokenType != JsonToken.StartObject)
            {
                throw new Exception(
                          "Error reading RavenJObject from JsonReader. Current JsonReader item is not an object: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
            }

            if (await reader.ReadAsync().ConfigureAwait(false) == false)
            {
                throw new Exception("Unexpected end of json object");
            }

            string propName = null;
            var    o        = new RavenJObject();

            do
            {
                switch (reader.TokenType)
                {
                case JsonToken.Comment:
                    // ignore comments
                    break;

                case JsonToken.PropertyName:
                    propName = reader.Value.ToString();
                    if (String.Equals(propName, String.Empty))
                    {
                        throw new InvalidDataException("Deserializing Json object with empty string as property name is not supported.");
                    }

                    break;

                case JsonToken.EndObject:
                    return(o);

                case JsonToken.StartObject:
                    if (!string.IsNullOrEmpty(propName))
                    {
                        var val = await LoadAsync(reader).ConfigureAwait(false);

                        o[propName] = val;     // TODO: Assert when o.Properties.ContainsKey and its value != val
                        propName    = null;
                    }
                    else
                    {
                        throw new InvalidOperationException("The JsonReader should not be on a token of type {0}."
                                                            .FormatWith(CultureInfo.InvariantCulture,
                                                                        reader.TokenType));
                    }
                    break;

                case JsonToken.StartArray:
                    if (!string.IsNullOrEmpty(propName))
                    {
                        var val = await RavenJArray.LoadAsync(reader).ConfigureAwait(false);

                        o[propName] = val;     // TODO: Assert when o.Properties.ContainsKey and its value != val
                        propName    = null;
                    }
                    else
                    {
                        throw new InvalidOperationException("The JsonReader should not be on a token of type {0}."
                                                            .FormatWith(CultureInfo.InvariantCulture,
                                                                        reader.TokenType));
                    }
                    break;

                default:
                    if (!string.IsNullOrEmpty(propName))
                    {
                        var val = RavenJValue.Load(reader);
                        o[propName] = val;     // TODO: Assert when o.Properties.ContainsKey and its value != val
                        propName    = null;
                    }
                    else
                    {
                        throw new InvalidOperationException("The JsonReader should not be on a token of type {0}."
                                                            .FormatWith(CultureInfo.InvariantCulture,
                                                                        reader.TokenType));
                    }
                    break;
                }
            } while (await reader.ReadAsync().ConfigureAwait(false));

            throw new Exception("Error reading RavenJObject from JsonReader.");
        }