/// <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> /// Reads a BsonType from the reader. /// </summary> /// <returns>A BsonType.</returns> public override BsonType ReadBsonType() { if (disposed) { ThrowObjectDisposedException(); } 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) { var message = string.Format("ReadBsonType cannot be called when State is: {0}", state); throw new InvalidOperationException(message); } 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); } currentName = currentElement.Name; currentValue = currentElement.Value; state = BsonReaderState.Name; break; default: throw new BsonInternalException("Invalid ContextType"); } currentBsonType = currentValue.BsonType; return(currentBsonType); }
/// <summary> /// Reads a BsonType from the reader. /// </summary> /// <returns>A BsonType.</returns> public override BsonType ReadBsonType() { if (_disposed) { ThrowObjectDisposedException(); } 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); } _currentName = currentElement.Name; _currentValue = currentElement.Value; _state = BsonReaderState.Name; break; default: throw new BsonInternalException("Invalid ContextType."); } _currentBsonType = _currentValue.BsonType; return(_currentBsonType); }