Beispiel #1
0
        /// <summary>
        /// Reads the name of an element from the reader (using a trie).
        /// </summary>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="trie">The trie.</param>
        /// <param name="found">Whether the element name was found in the trie.</param>
        /// <param name="value">If found is true, the value found in the trie for the element name.</param>
        /// <returns>
        /// The name of the element.
        /// </returns>
        public virtual string ReadName <TValue>(BsonTrie <TValue> trie, out bool found, out TValue value)
        {
            // overridden in BsonBinaryReader to use the trie for UTF8 decoding
            var name = ReadName();

            found = trie.TryGetValue(name, out value);
            return(name);
        }
        /// <summary>
        /// Reads a BsonType from the reader.
        /// </summary>
        /// <typeparam name="TValue">The type of the BsonTrie values.</typeparam>
        /// <param name="bsonTrie">An optional trie to search for a value that matches the next element name.</param>
        /// <param name="found">Set to true if a matching value was found in the trie.</param>
        /// <param name="value">Set to the matching value found in the trie or null if no matching value was found.</param>
        /// <returns>A BsonType.</returns>
        public override BsonType ReadBsonType <TValue>(BsonTrie <TValue> bsonTrie, out bool found, out TValue value)
        {
            if (Disposed)
            {
                ThrowObjectDisposedException();
            }
            found = false;
            value = default(TValue);
            if (State == BsonReaderState.Initial || State == BsonReaderState.ScopeDocument)
            {
                // there is an implied type of Document for the top level and for scope documents
                CurrentBsonType = BsonType.Document;
                State           = BsonReaderState.Value;
                return(CurrentBsonType);
            }
            if (State != BsonReaderState.Type)
            {
                ThrowInvalidState("ReadBsonType", BsonReaderState.Type);
            }

            switch (_context.ContextType)
            {
            case ContextType.Array:
                _currentValue = _context.GetNextValue();
                if (_currentValue == null)
                {
                    State = BsonReaderState.EndOfArray;
                    return(BsonType.EndOfDocument);
                }
                State = BsonReaderState.Value;
                break;

            case ContextType.Document:
                var currentElement = _context.GetNextElement();
                if (currentElement == null)
                {
                    State = BsonReaderState.EndOfDocument;
                    return(BsonType.EndOfDocument);
                }
                if (bsonTrie != null)
                {
                    found = bsonTrie.TryGetValue(currentElement.Name, out value);
                }
                CurrentName   = currentElement.Name;
                _currentValue = currentElement.Value;
                State         = BsonReaderState.Name;
                break;

            default:
                throw new BsonInternalException("Invalid ContextType.");
            }

            CurrentBsonType = _currentValue.BsonType;
            return(CurrentBsonType);
        }
 /// <summary>
 /// Informs the decoder of an already decoded name (so the decoder can change state if necessary).
 /// </summary>
 /// <param name="name">The name.</param>
 public void Inform(string name)
 {
     _found = _trie.TryGetValue(name, out _value);
 }