/// <summary>
            /// Initializes the current instance from a JSON document using the specified JSON reader.
            /// </summary>
            /// <param name="data">The UTF-8 encoded JSON document being deserialized.</param>
            /// <param name="reader">The JSON reader used during deserialization.</param>
            internal void InitFromJson(byte[] data, ref Utf8JsonReader reader)
            {
                Debug.Assert(reader.TokenType == JsonTokenType.StartObject);

                string propertyName = null;

                while (reader.Read())
                {
                    switch (reader.TokenType)
                    {
                    case JsonTokenType.PropertyName:
                    {
                        propertyName = mPool.GetStringFromUtf8(reader.ValueSpan);
                        break;
                    }

                    case JsonTokenType.String:
                    {
                        switch (propertyName)
                        {
                        case "type":
                            mTypeProxy.Update(data, (int)reader.TokenStartIndex + 1, reader.ValueSpan.Length, true);
                            break;

                        case "reason":
                            mReasonProxy.Update(data, (int)reader.TokenStartIndex + 1, reader.ValueSpan.Length, true);
                            break;

                        case "index_uuid":
                            mIndexUuidProxy.Update(data, (int)reader.TokenStartIndex + 1, reader.ValueSpan.Length, true);
                            break;

                        case "shard":
                            mShardsProxy.Update(data, (int)reader.TokenStartIndex + 1, reader.ValueSpan.Length, true);
                            break;

                        case "index":
                            mIndexProxy.Update(data, (int)reader.TokenStartIndex + 1, reader.ValueSpan.Length, true);
                            break;
                        }

                        break;
                    }

                    case JsonTokenType.EndObject:
                    {
                        return;
                    }

                    default:
                    {
                        // unexpected token, skip!
                        reader.Skip();
                        break;
                    }
                    }
                }

                throw new ArgumentException("The reader did not deliver a closing 'EndObject'.");
            }
            /// <summary>
            /// Initializes the current instance from a JSON document using the specified JSON reader.
            /// </summary>
            /// <param name="data">The UTF-8 encoded JSON document being deserialized.</param>
            /// <param name="reader">The JSON reader used during deserialization.</param>
            internal void InitFromJson(byte[] data, ref Utf8JsonReader reader)
            {
                Debug.Assert(reader.TokenType == JsonTokenType.StartObject);

                string propertyName = null;

                while (reader.Read())
                {
                    switch (reader.TokenType)
                    {
                    case JsonTokenType.PropertyName:
                    {
                        propertyName = mPool.GetStringFromUtf8(reader.ValueSpan);
                        break;
                    }

                    case JsonTokenType.Number:
                    {
                        switch (propertyName)
                        {
                        case "failed":
                            mFailedProxy = new Int32PropertyProxy(data, (int)reader.TokenStartIndex, reader.ValueSpan.Length);
                            break;

                        case "successful":
                            mSuccessfulProxy = new Int32PropertyProxy(data, (int)reader.TokenStartIndex, reader.ValueSpan.Length);
                            break;

                        case "total":
                            mTotalProxy = new Int32PropertyProxy(data, (int)reader.TokenStartIndex, reader.ValueSpan.Length);
                            break;
                        }

                        break;
                    }

                    case JsonTokenType.EndObject:
                    {
                        return;
                    }

                    default:
                    {
                        // unexpected token, skip!
                        reader.Skip();
                        break;
                    }
                    }
                }

                throw new ArgumentException("The reader did not deliver a closing 'EndObject'.");
            }
            /// <summary>
            /// Initializes the current instance from a JSON document using the specified JSON reader.
            /// </summary>
            /// <param name="data">The UTF-8 encoded JSON document being deserialized.</param>
            /// <param name="reader">The JSON reader used during deserialization.</param>
            internal void InitFromJson(byte[] data, ref Utf8JsonReader reader)
            {
                Debug.Assert(reader.TokenType == JsonTokenType.StartObject);

                string propertyName = null;

                while (reader.Read())
                {
                    switch (reader.TokenType)
                    {
                    case JsonTokenType.PropertyName:
                    {
                        propertyName = mPool.GetStringFromUtf8(reader.ValueSpan);
                        break;
                    }

                    case JsonTokenType.StartObject:
                    {
                        switch (propertyName)
                        {
                        case "index":
                            Index = mPool.GetBulkResponseItemIndex();
                            Index.InitFromJson(data, ref reader);
                            break;

                        default:
                            // unexpected property, skip all children!
                            reader.Skip();
                            break;
                        }

                        break;
                    }

                    case JsonTokenType.EndObject:
                    {
                        return;
                    }

                    default:
                    {
                        // unexpected token, skip!
                        reader.Skip();
                        break;
                    }
                    }
                }

                throw new ArgumentException("The reader did not deliver a closing 'EndObject'.");
            }
Exemplo n.º 4
0
            /// <summary>
            /// Initializes the current instance from a JSON document using the specified JSON reader.
            /// </summary>
            /// <param name="data">The UTF-8 encoded JSON document being deserialized.</param>
            /// <param name="reader">The JSON reader used during deserialization.</param>
            internal void InitFromJson(byte[] data, ref Utf8JsonReader reader)
            {
                Debug.Assert(reader.TokenType == JsonTokenType.StartObject);

                string propertyName = null;

                while (reader.Read())
                {
                    switch (reader.TokenType)
                    {
                    case JsonTokenType.PropertyName:
                    {
                        propertyName = mPool.GetStringFromUtf8(reader.ValueSpan);
                        break;
                    }

                    case JsonTokenType.String:
                    {
                        switch (propertyName)
                        {
                        case "_index":
                            mIndexProxy.Update(data, (int)reader.TokenStartIndex + 1, reader.ValueSpan.Length, true);
                            break;

                        case "_type":
                            mTypeProxy.Update(data, (int)reader.TokenStartIndex + 1, reader.ValueSpan.Length, true);
                            break;

                        case "_id":
                            // changes with every document, do not cache!
                            mIdProxy.Update(data, (int)reader.TokenStartIndex + 1, reader.ValueSpan.Length, false);
                            break;

                        case "result":
                            mResultProxy.Update(data, (int)reader.TokenStartIndex + 1, reader.ValueSpan.Length, true);
                            break;
                        }

                        break;
                    }

                    case JsonTokenType.Number:
                    {
                        switch (propertyName)
                        {
                        case "_version":
                            mVersionProxy = new Int32PropertyProxy(data, (int)reader.TokenStartIndex, reader.ValueSpan.Length);
                            break;

                        case "_seq_no":
                            mSequenceNumberProxy = new Int32PropertyProxy(data, (int)reader.TokenStartIndex, reader.ValueSpan.Length);
                            break;

                        case "_primary_term":
                            mPrimaryTermProxy = new Int32PropertyProxy(data, (int)reader.TokenStartIndex, reader.ValueSpan.Length);
                            break;

                        case "status":
                            mStatusProxy = new Int32PropertyProxy(data, (int)reader.TokenStartIndex, reader.ValueSpan.Length);
                            break;
                        }

                        break;
                    }

                    case JsonTokenType.StartObject:
                    {
                        switch (propertyName)
                        {
                        case "_shards":
                        {
                            var model = mPool.GetBulkResponseItemIndexShards();
                            model.InitFromJson(data, ref reader);
                            Shards = model;
                            break;
                        }

                        case "error":
                        {
                            var model = mPool.GetBulkResponseItemIndexError();
                            model.InitFromJson(data, ref reader);
                            Error = model;
                            break;
                        }

                        default:
                            // unexpected property, skip all children!
                            reader.Skip();
                            break;
                        }

                        break;
                    }

                    case JsonTokenType.EndObject:
                    {
                        return;
                    }

                    default:
                    {
                        // unexpected token, skip!
                        reader.Skip();
                        break;
                    }
                    }
                }

                throw new ArgumentException("The reader did not deliver a closing 'EndObject'.");
            }
Exemplo n.º 5
0
        /// <summary>
        /// Reads the response from specified UTF-8 JSON document and initializes the specified response.
        /// Called by <see cref="BulkResponsePool"/> to initialize the response.
        /// </summary>
        /// <param name="data">JSON document to read.</param>
        /// <returns>
        /// The created response.
        /// Call <see cref="ReturnToPool"/> to release it when you're done.
        /// </returns>
        internal void InitFromJson(byte[] data)
        {
            var    reader       = new Utf8JsonReader(data, sJsonReaderOptions);
            string propertyName = null;

            // skip the first curly brace that starts all json documents
            reader.Read();
            Debug.Assert(reader.TokenType == JsonTokenType.StartObject);

            while (reader.Read())
            {
                switch (reader.TokenType)
                {
                case JsonTokenType.PropertyName:
                {
                    propertyName = mPool.GetStringFromUtf8(reader.ValueSpan);
                    break;
                }

                case JsonTokenType.True:
                case JsonTokenType.False:
                {
                    switch (propertyName)
                    {
                    case "errors":
                        mErrorsProxy = new BooleanPropertyProxy(data, (int)reader.TokenStartIndex, reader.ValueSpan.Length);
                        break;
                    }

                    break;
                }

                case JsonTokenType.Number:
                {
                    switch (propertyName)
                    {
                    case "took":
                        mTookProxy = new Int32PropertyProxy(data, (int)reader.TokenStartIndex, reader.ValueSpan.Length);
                        break;
                    }

                    break;
                }

                case JsonTokenType.StartArray:
                {
                    switch (propertyName)
                    {
                    case "items":
                        Items = DeserializeItems(data, ref reader);
                        break;

                    default:
                        // unexpected property, skip all children!
                        reader.Skip();
                        break;
                    }

                    break;
                }

                case JsonTokenType.EndObject:
                {
                    return;
                }

                default:
                {
                    // unexpected token, skip!
                    reader.Skip();
                    break;
                }
                }
            }

            throw new ArgumentException("The reader did not deliver a closing 'EndObject'.");
        }