/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
namespace MongoDB.Bson.IO
{
///
/// Contains extensions methods for IBsonReader.
///
public static class IBsonReaderExtensions
{
///
/// Positions the reader to an element by name.
///
/// The reader.
/// The name of the element.
/// True if the element was found.
public static bool FindElement(this IBsonReader reader, string name)
{
while (reader.ReadBsonType() != BsonType.EndOfDocument)
{
var elementName = reader.ReadName();
if (elementName == name)
{
return true;
}
reader.SkipValue();
}
return false;
}
///
/// Positions the reader to a string element by name.
///
/// The reader.
/// The name of the element.
/// True if the element was found.
public static string FindStringElement(this IBsonReader reader, string name)
{
BsonType bsonType;
while ((bsonType = reader.ReadBsonType()) != BsonType.EndOfDocument)
{
if (bsonType == BsonType.String)
{
var elementName = reader.ReadName();
if (elementName == name)
{
return reader.ReadString();
}
else
{
reader.SkipValue();
}
}
else
{
reader.SkipName();
reader.SkipValue();
}
}
return null;
}
///
/// Reads a BSON binary data element from the reader.
///
/// The reader.
/// The name of the element.
/// A BsonBinaryData.
public static BsonBinaryData ReadBinaryData(this IBsonReader reader, string name)
{
VerifyName(reader, name);
return reader.ReadBinaryData();
}
///
/// Reads a BSON binary data element from the reader temporarily setting the GuidRepresentation to Unspecified.
///
/// The reader.
/// A BsonBinaryData.
[Obsolete("In V3 mode use ReadBinaryData instead.")]
public static BsonBinaryData ReadBinaryDataWithGuidRepresentationUnspecified(this IBsonReader reader)
{
#pragma warning disable 618
if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2)
{
reader.PushSettings(s => s.GuidRepresentation = GuidRepresentation.Unspecified);
try
{
return reader.ReadBinaryData();
}
finally
{
reader.PopSettings();
}
}
else
{
return reader.ReadBinaryData();
}
#pragma warning restore 618
}
///
/// Reads a BSON boolean element from the reader.
///
/// The reader.
/// The name of the element.
/// A Boolean.
public static bool ReadBoolean(this IBsonReader reader, string name)
{
VerifyName(reader, name);
return reader.ReadBoolean();
}
///
/// Reads a BSON binary data element from the reader.
///
/// The reader.
/// The name of the element.
/// A byte array.
public static byte[] ReadBytes(this IBsonReader reader, string name)
{
VerifyName(reader, name);
return reader.ReadBytes();
}
///
/// Reads a BSON DateTime element from the reader.
///
/// The reader.
/// The name of the element.
/// The number of milliseconds since the Unix epoch.
public static long ReadDateTime(this IBsonReader reader, string name)
{
VerifyName(reader, name);
return reader.ReadDateTime();
}
///
/// Reads a BSON Decimal128 element from the reader.
///
/// The reader.
/// The name of the element.
/// A .
public static Decimal128 ReadDecimal128(this IBsonReader reader, string name)
{
VerifyName(reader, name);
return reader.ReadDecimal128();
}
///
/// Reads a BSON Double element from the reader.
///
/// The reader.
/// The name of the element.
/// A Double.
public static double ReadDouble(this IBsonReader reader, string name)
{
VerifyName(reader, name);
return reader.ReadDouble();
}
///
/// Reads a BSON Int32 element from the reader.
///
/// The reader.
/// The name of the element.
/// An Int32.
public static int ReadInt32(this IBsonReader reader, string name)
{
VerifyName(reader, name);
return reader.ReadInt32();
}
///
/// Reads a BSON Int64 element from the reader.
///
/// The reader.
/// The name of the element.
/// An Int64.
public static long ReadInt64(this IBsonReader reader, string name)
{
VerifyName(reader, name);
return reader.ReadInt64();
}
///
/// Reads a BSON JavaScript element from the reader.
///
/// The reader.
/// The name of the element.
/// A string.
public static string ReadJavaScript(this IBsonReader reader, string name)
{
VerifyName(reader, name);
return reader.ReadJavaScript();
}
///
/// Reads a BSON JavaScript with scope element from the reader (call ReadStartDocument next to read the scope).
///
/// The reader.
/// The name of the element.
/// A string.
public static string ReadJavaScriptWithScope(this IBsonReader reader, string name)
{
VerifyName(reader, name);
return reader.ReadJavaScriptWithScope();
}
///
/// Reads a BSON MaxKey element from the reader.
///
/// The reader.
/// The name of the element.
public static void ReadMaxKey(this IBsonReader reader, string name)
{
VerifyName(reader, name);
reader.ReadMaxKey();
}
///
/// Reads a BSON MinKey element from the reader.
///
/// The reader.
/// The name of the element.
public static void ReadMinKey(this IBsonReader reader, string name)
{
VerifyName(reader, name);
reader.ReadMinKey();
}
///
/// Reads the name of an element from the reader.
///
/// The reader.
/// The name of the element.
public static string ReadName(this IBsonReader reader)
{
return reader.ReadName(Utf8NameDecoder.Instance);
}
///
/// Reads the name of an element from the reader.
///
/// The reader.
/// The name of the element.
public static void ReadName(this IBsonReader reader, string name)
{
VerifyName(reader, name);
}
///
/// Reads a BSON null element from the reader.
///
/// The reader.
/// The name of the element.
public static void ReadNull(this IBsonReader reader, string name)
{
VerifyName(reader, name);
reader.ReadNull();
}
///
/// Reads a BSON ObjectId element from the reader.
///
/// The reader.
/// The name of the element.
/// An ObjectId.
public static ObjectId ReadObjectId(this IBsonReader reader, string name)
{
VerifyName(reader, name);
return reader.ReadObjectId();
}
///
/// Reads a raw BSON array.
///
/// The reader.
/// The name.
///
/// The raw BSON array.
///
public static IByteBuffer ReadRawBsonArray(this IBsonReader reader, string name)
{
VerifyName(reader, name);
return reader.ReadRawBsonArray();
}
///
/// Reads a raw BSON document.
///
/// The reader.
/// The name.
/// The raw BSON document.
public static IByteBuffer ReadRawBsonDocument(this IBsonReader reader, string name)
{
VerifyName(reader, name);
return reader.ReadRawBsonDocument();
}
///
/// Reads a BSON regular expression element from the reader.
///
/// The reader.
/// The name of the element.
/// A BsonRegularExpression.
public static BsonRegularExpression ReadRegularExpression(this IBsonReader reader, string name)
{
VerifyName(reader, name);
return reader.ReadRegularExpression();
}
///
/// Reads a BSON string element from the reader.
///
/// The reader.
/// The name of the element.
/// A String.
public static string ReadString(this IBsonReader reader, string name)
{
VerifyName(reader, name);
return reader.ReadString();
}
///
/// Reads a BSON symbol element from the reader.
///
/// The reader.
/// The name of the element.
/// A string.
public static string ReadSymbol(this IBsonReader reader, string name)
{
VerifyName(reader, name);
return reader.ReadSymbol();
}
///
/// Reads a BSON timestamp element from the reader.
///
/// The combined timestamp/increment.
/// The reader.
/// The name of the element.
public static long ReadTimestamp(this IBsonReader reader, string name)
{
VerifyName(reader, name);
return reader.ReadTimestamp();
}
///
/// Reads a BSON undefined element from the reader.
///
/// The reader.
/// The name of the element.
public static void ReadUndefined(this IBsonReader reader, string name)
{
VerifyName(reader, name);
reader.ReadUndefined();
}
private static void VerifyName(IBsonReader reader, string expectedName)
{
var actualName = reader.ReadName();
if (actualName != expectedName)
{
var message = string.Format(
"Expected element name to be '{0}', not '{1}'.",
expectedName, actualName);
throw new FormatException(message);
}
}
}
}