private static bool TryReadList(ref DdbReader reader, out List <AttributeValue> value)
        {
            var success = false;

            reader.State.PushDocument();

            try
            {
                ref var current = ref reader.State.GetCurrent();

                if (reader.State.UseFastPath)
                {
                    while (true)
                    {
                        // Start object or end array
                        reader.JsonReaderValue.ReadWithVerify();

                        if (reader.JsonReaderValue.TokenType == JsonTokenType.EndArray)
                        {
                            break;
                        }

                        // Attribute type
                        reader.JsonReaderValue.ReadWithVerify();
                        current.AttributeType = DdbJsonReader.GetDdbAttributeType(ref reader.JsonReaderValue);

                        // Attribute value
                        reader.JsonReaderValue.ReadWithVerify();

                        TryReadValue(ref reader, ref current);

                        // End object
                        reader.JsonReaderValue.ReadWithVerify();
                    }

                    value = CreateListFromBuffer(ref current.AttributesBuffer);

                    return(success = true);
                }
                else
                {
                    Unsafe.SkipInit(out value);

                    if (current.PropertyState != DdbStackFramePropertyState.None)
                    {
                        if (current.PropertyState < DdbStackFramePropertyState.ReadValueType)
                        {
                            if (!reader.JsonReaderValue.Read())
                            {
                                return(success = false);
                            }

                            current.AttributeType = DdbJsonReader.GetDdbAttributeType(ref reader.JsonReaderValue);
                            current.PropertyState = DdbStackFramePropertyState.ReadValueType;
                        }

                        if (current.PropertyState < DdbStackFramePropertyState.ReadValue)
                        {
                            if (!reader.JsonReaderValue.Read())
                            {
                                return(success = false);
                            }

                            current.PropertyState = DdbStackFramePropertyState.ReadValue;
                        }

                        if (current.PropertyState < DdbStackFramePropertyState.TryRead)
                        {
                            if (!TryReadValue(ref reader, ref current))
                            {
                                return(success = false);
                            }

                            current.PropertyState = DdbStackFramePropertyState.TryRead;
                        }

                        // End object
                        if (!reader.JsonReaderValue.Read())
                        {
                            return(success = false);
                        }

                        current.PropertyState = DdbStackFramePropertyState.None;
                    }

                    while (true)
                    {
                        if (!reader.JsonReaderValue.Read())
                        {
                            return(success = false);
                        }

                        current.PropertyState = DdbStackFramePropertyState.ReadValueStart;

                        if (reader.JsonReaderValue.TokenType == JsonTokenType.EndArray)
                        {
                            break;
                        }

                        if (!reader.JsonReaderValue.Read())
                        {
                            return(success = false);
                        }

                        current.AttributeType = DdbJsonReader.GetDdbAttributeType(ref reader.JsonReaderValue);
                        current.PropertyState = DdbStackFramePropertyState.ReadValueType;

                        if (!reader.JsonReaderValue.Read())
                        {
                            return(success = false);
                        }

                        current.PropertyState = DdbStackFramePropertyState.ReadValue;

                        if (!TryReadValue(ref reader, ref current))
                        {
                            return(success = false);
                        }

                        current.PropertyState = DdbStackFramePropertyState.TryRead;

                        // End object
                        if (!reader.JsonReaderValue.Read())
                        {
                            return(success = false);
                        }

                        current.PropertyState = DdbStackFramePropertyState.None;
                    }

                    value = CreateListFromBuffer(ref current.AttributesBuffer);

                    return(success = true);
                }
            }
Esempio n. 2
0
        internal override bool TryRead(ref DdbReader reader, out TDictionary value)
        {
            Unsafe.SkipInit(out value);
            var success = false;

            reader.State.Push();
            try
            {
                ref var current = ref reader.State.GetCurrent();
                Dictionary <TKey, TValue> entity;

                if (reader.State.UseFastPath)
                {
                    entity = new Dictionary <TKey, TValue>();

                    if (ValueConverter.UseDirectRead)
                    {
                        while (true)
                        {
                            // Property name
                            reader.JsonReaderValue.ReadWithVerify();

                            if (reader.JsonReaderValue.TokenType == JsonTokenType.EndObject)
                            {
                                break;
                            }

                            var pairKey = KeyConverter.Read(ref reader);

                            // Start object
                            reader.JsonReaderValue.ReadWithVerify();

                            // Attribute type
                            reader.JsonReaderValue.ReadWithVerify();

                            current.AttributeType = DdbJsonReader.GetDdbAttributeType(ref reader.JsonReaderValue);

                            // Attribute value
                            reader.JsonReaderValue.ReadWithVerify();

                            entity.Add(pairKey, ValueConverter.Read(ref reader));

                            // End object
                            reader.JsonReaderValue.ReadWithVerify();
                        }
                    }
                    else
                    {
                        while (true)
                        {
                            // Property name
                            reader.JsonReaderValue.ReadWithVerify();

                            if (reader.JsonReaderValue.TokenType == JsonTokenType.EndObject)
                            {
                                break;
                            }

                            var pairKey = KeyConverter.Read(ref reader);

                            // Start object
                            reader.JsonReaderValue.ReadWithVerify();

                            // Attribute type
                            reader.JsonReaderValue.ReadWithVerify();

                            current.AttributeType = DdbJsonReader.GetDdbAttributeType(ref reader.JsonReaderValue);

                            // Attribute value
                            reader.JsonReaderValue.ReadWithVerify();

                            ValueConverter.TryRead(ref reader, out var pairValue);
                            entity.Add(pairKey, pairValue);

                            // End object
                            reader.JsonReaderValue.ReadWithVerify();
                        }
                    }

                    value = ToResult(entity);

                    return(success = true);
                }
                else
                {
                    if (current.ObjectState < DdbStackFrameObjectState.CreatedObject)
                    {
                        current.ReturnValue = entity = new Dictionary <TKey, TValue>();
                        current.ObjectState = DdbStackFrameObjectState.CreatedObject;
                    }
                    else
                    {
                        entity = (Dictionary <TKey, TValue>)current.ReturnValue !;
                    }

                    while (true)
                    {
                        if (current.PropertyState < DdbStackFramePropertyState.ReadName)
                        {
                            // Property name
                            if (!reader.JsonReaderValue.Read())
                            {
                                return(success = false);
                            }
                        }

                        TKey pairKey;
                        if (current.PropertyState < DdbStackFramePropertyState.Name)
                        {
                            if (reader.JsonReaderValue.TokenType == JsonTokenType.EndObject)
                            {
                                break;
                            }

                            pairKey = KeyConverter.Read(ref reader);
                            current.PropertyState = DdbStackFramePropertyState.Name;
                        }
                        else
                        {
                            pairKey = (TKey)current.DictionaryKey !;
                        }

                        if (current.PropertyState < DdbStackFramePropertyState.ReadValueStart)
                        {
                            if (!reader.JsonReaderValue.Read())
                            {
                                current.DictionaryKey = pairKey;
                                return(success = false);
                            }

                            current.PropertyState = DdbStackFramePropertyState.ReadValueStart;
                        }

                        if (current.PropertyState < DdbStackFramePropertyState.ReadValueType)
                        {
                            if (!reader.JsonReaderValue.Read())
                            {
                                current.DictionaryKey = pairKey;
                                return(success = false);
                            }

                            current.AttributeType = DdbJsonReader.GetDdbAttributeType(ref reader.JsonReaderValue);
                            current.PropertyState = DdbStackFramePropertyState.ReadValueType;
                        }

                        if (current.PropertyState < DdbStackFramePropertyState.ReadValue)
                        {
                            if (!SingleValueReadWithReadAhead(ValueConverter.CanSeek, ref reader))
                            {
                                current.DictionaryKey = pairKey;
                                return(success = false);
                            }

                            current.PropertyState = DdbStackFramePropertyState.ReadValue;
                        }

                        if (current.PropertyState < DdbStackFramePropertyState.TryRead)
                        {
                            TValue pairValue;
                            if (ValueConverter.UseDirectRead)
                            {
                                pairValue = ValueConverter.Read(ref reader);
                            }
                            else
                            {
                                if (!ValueConverter.TryRead(ref reader, out pairValue))
                                {
                                    current.DictionaryKey = pairKey;
                                    return(success = false);
                                }
                            }

                            entity.Add(pairKey, pairValue);

                            current.PropertyState = DdbStackFramePropertyState.TryRead;
                        }

                        // End object
                        if (!reader.JsonReaderValue.Read())
                        {
                            return(success = false);
                        }

                        current.PropertyState = DdbStackFramePropertyState.None;
                        current.DictionaryKey = null;
                        current.PropertyInfo  = null;
                    }

                    value = ToResult(entity);
                    return(success = true);
                }
            }
Esempio n. 3
0
        public static bool TryReadMap(ref DdbReader reader, out Document value)
        {
            var success = false;

            reader.State.PushDocument();
            try
            {
                ref var current = ref reader.State.GetCurrent();

                if (reader.State.UseFastPath)
                {
                    while (true)
                    {
                        // Property name
                        reader.JsonReaderValue.ReadWithVerify();

                        if (reader.JsonReaderValue.TokenType == JsonTokenType.EndObject)
                        {
                            break;
                        }

                        current.StringBuffer.Add(GetCachedString(ref reader.JsonReaderValue, ref reader.State));

                        // Start object
                        reader.JsonReaderValue.ReadWithVerify();

                        // Attribute type
                        reader.JsonReaderValue.ReadWithVerify();

                        current.AttributeType = DdbJsonReader.GetDdbAttributeType(ref reader.JsonReaderValue);

                        // Attribute value
                        reader.JsonReaderValue.ReadWithVerify();

                        TryReadValue(ref reader, ref current);

                        // End object
                        reader.JsonReaderValue.ReadWithVerify();
                    }

                    value = DocumentDdbReader.CreateDocumentFromBuffer(ref current) !;

                    return(success = true);
                }
                else
                {
                    Unsafe.SkipInit(out value);

                    if (current.PropertyState != DdbStackFramePropertyState.None)
                    {
                        if (current.PropertyState < DdbStackFramePropertyState.Name)
                        {
                            current.StringBuffer.Add(GetCachedString(ref reader.JsonReaderValue, ref reader.State));
                            current.PropertyState = DdbStackFramePropertyState.Name;
                        }

                        if (current.PropertyState < DdbStackFramePropertyState.ReadValueStart)
                        {
                            if (!reader.JsonReaderValue.Read())
                            {
                                return(success = false);
                            }

                            current.PropertyState = DdbStackFramePropertyState.ReadValueStart;
                        }

                        if (current.PropertyState < DdbStackFramePropertyState.ReadValueType)
                        {
                            if (!reader.JsonReaderValue.Read())
                            {
                                return(success = false);
                            }

                            current.AttributeType = DdbJsonReader.GetDdbAttributeType(ref reader.JsonReaderValue);
                            current.PropertyState = DdbStackFramePropertyState.ReadValueType;
                        }

                        if (current.PropertyState < DdbStackFramePropertyState.ReadValue)
                        {
                            if (!reader.JsonReaderValue.Read())
                            {
                                return(success = false);
                            }

                            current.PropertyState = DdbStackFramePropertyState.ReadValue;
                        }

                        if (current.PropertyState < DdbStackFramePropertyState.TryRead)
                        {
                            if (!TryReadValue(ref reader, ref current))
                            {
                                return(success = false);
                            }

                            current.PropertyState = DdbStackFramePropertyState.TryRead;
                        }

                        // End object
                        if (!reader.JsonReaderValue.Read())
                        {
                            return(success = false);
                        }

                        current.PropertyState = DdbStackFramePropertyState.None;
                    }

                    while (true)
                    {
                        // Property name
                        if (!reader.JsonReaderValue.Read())
                        {
                            return(success = false);
                        }

                        if (reader.JsonReaderValue.TokenType == JsonTokenType.EndObject)
                        {
                            break;
                        }

                        current.StringBuffer.Add(GetCachedString(ref reader.JsonReaderValue, ref reader.State));
                        current.PropertyState = DdbStackFramePropertyState.Name;

                        if (!reader.JsonReaderValue.Read())
                        {
                            return(success = false);
                        }

                        current.PropertyState = DdbStackFramePropertyState.ReadValueStart;

                        if (!reader.JsonReaderValue.Read())
                        {
                            return(success = false);
                        }

                        current.AttributeType = DdbJsonReader.GetDdbAttributeType(ref reader.JsonReaderValue);
                        current.PropertyState = DdbStackFramePropertyState.ReadValueType;

                        if (!reader.JsonReaderValue.Read())
                        {
                            return(success = false);
                        }

                        current.PropertyState = DdbStackFramePropertyState.ReadValue;

                        if (!TryReadValue(ref reader, ref current))
                        {
                            return(success = false);
                        }

                        current.PropertyState = DdbStackFramePropertyState.TryRead;

                        // End object
                        if (!reader.JsonReaderValue.Read())
                        {
                            return(success = false);
                        }

                        current.PropertyState = DdbStackFramePropertyState.None;
                    }

                    value = DocumentDdbReader.CreateDocumentFromBuffer(ref current);
                    return(success = true);
                }
            }
        public async Task <int> EfficientReaderWithoutInternBenchmark()
        {
            var items = await DdbJsonReader.ReadAsync(new MemoryStream(_jsonBytes), QueryParsingOptions.Instance, false).ConfigureAwait(false);

            return(items.Value !.Count);
        }