Example #1
0
        public T this[int index]
        {
            get
            {
                EnsurePositionsBuilt();
                if (_stream == null)
                {
                    _stream = _streamProvider();
                }

                if (index < 0 || index > _itemPositions.Length)
                {
                    throw new IndexOutOfRangeException("index");
                }

                // Seek to the item
                long position = _itemPositions[index];
                _stream.Seek(position, SeekOrigin.Begin);

                // Build a JsonTextReader
                using (JsonInnerTextReader reader = new JsonInnerTextReader(new StreamReader(_stream)))
                {
                    reader.CloseInput = false;
                    reader.Read();

                    return(_jsonSerializer.Deserialize <T>(reader));
                }
            }

            set => throw new NotSupportedException();
Example #2
0
        public T this[int index]
        {
            get
            {
                EnsurePositionsBuilt();
                if (_stream == null)
                {
                    _stream = _streamProvider();
                }

                if (index < 0 || index > _itemPositions.Length)
                {
                    throw new IndexOutOfRangeException("index");
                }

                // Return last, if re-requested
                if (index == _lastAccessedItemIndex)
                {
                    return(_lastAccessedItem);
                }

                // Seek to the item
                long position = _itemPositions[index];
                _stream.Seek(position, SeekOrigin.Begin);

                // Build a JsonTextReader
                using (JsonInnerTextReader reader = new JsonInnerTextReader(new StreamReader(_stream)))
                {
                    reader.CloseInput = false;
                    reader.Read();

                    // Deserialize and transform the item, if required
                    T item = _jsonSerializer.Deserialize <T>(reader);
                    if (_transformer != null)
                    {
                        item = _transformer(item);
                    }

                    _lastAccessedItemIndex = index;
                    _lastAccessedItem      = item;
                    return(item);
                }
            }

            set
            {
                // Don't throw if setting same instance getter returned (SarifRewritingVisitor on DeferredList)
                if (index == _lastAccessedItemIndex && object.ReferenceEquals(value, _lastAccessedItem))
                {
                    return;
                }

                throw new NotSupportedException();
            }
        }