本文整理汇总了C#中MongoDB.Bson.IO.BsonReader.ReadBsonType方法的典型用法代码示例。如果您正苦于以下问题:C# BsonReader.ReadBsonType方法的具体用法?C# BsonReader.ReadBsonType怎么用?C# BsonReader.ReadBsonType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MongoDB.Bson.IO.BsonReader
的用法示例。
在下文中一共展示了BsonReader.ReadBsonType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Deserialize
public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
{
object value = null;
if (bsonReader.CurrentBsonType == BsonType.Null)
{
bsonReader.ReadNull();
}
else
{
bsonReader.ReadStartArray();
var idList = new List<ObjectId>();
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
{
var id = (ObjectId)BsonSerializer.Deserialize(bsonReader, typeof(ObjectId));
idList.Add(id);
}
bsonReader.ReadEndArray();
if (idList.Count > 0)
{
var cursor = MongoDbProvider.Database.GetCollection(DocumentType, DocumentType.Name)
.FindAs(DocumentType, Query.In("_id", BsonArray.Create(idList)));
var documents = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(DocumentType));
foreach (var document in cursor)
{
documents.Add(document);
}
value = documents;
}
}
return value;
}
示例2: Deserialize
public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
{
if (_trace)
pb.Trace.WriteLine("ZStringArraySerializer.Deserialize()");
VerifyTypes(nominalType, actualType, typeof(ZStringArray));
var bsonType = bsonReader.GetCurrentBsonType();
switch (bsonType)
{
case BsonType.Array:
bsonReader.ReadStartArray();
//return new ZString(bsonReader.ReadString());
var array = new List<string>();
bsonType = bsonReader.ReadBsonType();
while (bsonType != BsonType.EndOfDocument)
{
if (bsonType != BsonType.String)
throw new PBException("error ZStringArray cannot contain value of type {0}", bsonType);
var value = bsonReader.ReadString();
array.Add(value);
bsonType = bsonReader.ReadBsonType();
}
bsonReader.ReadEndArray();
return new ZStringArray(array.ToArray());
default:
throw new PBException("error cannot deserialize ZStringArray from BsonType {0}.", bsonType);
}
}
示例3: TestBookmark
public void TestBookmark() {
var json = "{ \"x\" : 1, \"y\" : 2 }";
using (bsonReader = BsonReader.Create(json)) {
// do everything twice returning to bookmark in between
var bookmark = bsonReader.GetBookmark();
Assert.AreEqual(BsonType.Document, bsonReader.ReadBsonType());
bsonReader.ReturnToBookmark(bookmark);
Assert.AreEqual(BsonType.Document, bsonReader.ReadBsonType());
bookmark = bsonReader.GetBookmark();
bsonReader.ReadStartDocument();
bsonReader.ReturnToBookmark(bookmark);
bsonReader.ReadStartDocument();
bookmark = bsonReader.GetBookmark();
Assert.AreEqual(BsonType.Int32, bsonReader.ReadBsonType());
bsonReader.ReturnToBookmark(bookmark);
Assert.AreEqual(BsonType.Int32, bsonReader.ReadBsonType());
bookmark = bsonReader.GetBookmark();
Assert.AreEqual("x", bsonReader.ReadName());
bsonReader.ReturnToBookmark(bookmark);
Assert.AreEqual("x", bsonReader.ReadName());
bookmark = bsonReader.GetBookmark();
Assert.AreEqual(1, bsonReader.ReadInt32());
bsonReader.ReturnToBookmark(bookmark);
Assert.AreEqual(1, bsonReader.ReadInt32());
bookmark = bsonReader.GetBookmark();
Assert.AreEqual(BsonType.Int32, bsonReader.ReadBsonType());
bsonReader.ReturnToBookmark(bookmark);
Assert.AreEqual(BsonType.Int32, bsonReader.ReadBsonType());
bookmark = bsonReader.GetBookmark();
Assert.AreEqual("y", bsonReader.ReadName());
bsonReader.ReturnToBookmark(bookmark);
Assert.AreEqual("y", bsonReader.ReadName());
bookmark = bsonReader.GetBookmark();
Assert.AreEqual(2, bsonReader.ReadInt32());
bsonReader.ReturnToBookmark(bookmark);
Assert.AreEqual(2, bsonReader.ReadInt32());
bookmark = bsonReader.GetBookmark();
Assert.AreEqual(BsonType.EndOfDocument, bsonReader.ReadBsonType());
bsonReader.ReturnToBookmark(bookmark);
Assert.AreEqual(BsonType.EndOfDocument, bsonReader.ReadBsonType());
bookmark = bsonReader.GetBookmark();
bsonReader.ReadEndDocument();
bsonReader.ReturnToBookmark(bookmark);
bsonReader.ReadEndDocument();
Assert.AreEqual(BsonReaderState.Done, bsonReader.State);
}
Assert.AreEqual(json, BsonSerializer.Deserialize<BsonDocument>(new StringReader(json)).ToJson());
}
示例4: TestArrayEmpty
public void TestArrayEmpty() {
var json = "[]";
using (bsonReader = BsonReader.Create(json)) {
Assert.AreEqual(BsonType.Array, bsonReader.ReadBsonType());
bsonReader.ReadStartArray();
Assert.AreEqual(BsonType.EndOfDocument, bsonReader.ReadBsonType());
bsonReader.ReadEndArray();
Assert.AreEqual(BsonReaderState.Done, bsonReader.State);
}
Assert.AreEqual(json, BsonSerializer.Deserialize<BsonArray>(new StringReader(json)).ToJson());
}
示例5: TestArrayOneElement
public void TestArrayOneElement()
{
var json = "[1]";
using (_bsonReader = new JsonReader(json))
{
Assert.AreEqual(BsonType.Array, _bsonReader.ReadBsonType());
_bsonReader.ReadStartArray();
Assert.AreEqual(BsonType.Int32, _bsonReader.ReadBsonType());
Assert.AreEqual(1, _bsonReader.ReadInt32());
Assert.AreEqual(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
_bsonReader.ReadEndArray();
Assert.AreEqual(BsonReaderState.Done, _bsonReader.State);
}
Assert.AreEqual(json, BsonSerializer.Deserialize<BsonArray>(json).ToJson());
}
示例6: Deserialize
/// <summary>
/// Deserializes an object from a BsonReader.
/// </summary>
/// <param name="bsonReader">The BsonReader.</param>
/// <param name="nominalType">The nominal type of the object.</param>
/// <param name="options">The serialization options.</param>
/// <returns>An object.</returns>
public override object Deserialize(
BsonReader bsonReader,
Type nominalType,
IBsonSerializationOptions options
) {
if (nominalType != typeof(object)) {
var message = string.Format("ObjectSerializer called for nominal type {0}.", nominalType.FullName);
throw new InvalidOperationException(message);
}
var bsonType = bsonReader.CurrentBsonType;
if (bsonType == BsonType.Null) {
bsonReader.ReadNull();
return null;
} else if (bsonType == BsonType.Document) {
var bookmark = bsonReader.GetBookmark();
bsonReader.ReadStartDocument();
if (bsonReader.ReadBsonType() == BsonType.EndOfDocument) {
bsonReader.ReadEndDocument();
return new object();
} else {
bsonReader.ReturnToBookmark(bookmark);
}
}
var discriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(typeof(object));
var actualType = discriminatorConvention.GetActualType(bsonReader, typeof(object));
if (actualType == typeof(object)) {
var message = string.Format("Unable to determine actual type of object to deserialize. NominalType is System.Object and BsonType is {0}.", bsonType);
throw new FileFormatException(message);
}
var serializer = BsonSerializer.LookupSerializer(actualType);
return serializer.Deserialize(bsonReader, nominalType, actualType, null);
}
示例7: _Deserialize
public OXmlParagraphElement _Deserialize(BsonReader bsonReader, IBsonSerializationOptions options)
{
OXmlParagraphElement paragraph = new OXmlParagraphElement();
while (true)
{
BsonType bsonType = bsonReader.ReadBsonType();
if (bsonType == BsonType.EndOfDocument)
break;
//if (bsonType != BsonType.String)
// throw new PBException("error ZStringArray cannot contain value of type {0}", bsonType);
//var value = bsonReader.ReadString();
string name = bsonReader.ReadName();
switch (name.ToLower())
{
case "type":
if (bsonType != BsonType.String)
throw new PBException($"wrong type value {bsonType}");
string type = bsonReader.ReadString();
if (type.ToLower() != "paragraph")
throw new PBException($"invalid Type {type} when deserialize OXmlParagraphElement");
break;
case "style":
if (bsonType == BsonType.Null)
break;
if (bsonType != BsonType.String)
throw new PBException($"wrong style value {bsonType}");
paragraph.Style = bsonReader.ReadString();
break;
default:
throw new PBException($"unknow Paragraph value \"{name}\"");
}
}
return paragraph;
}
示例8: Deserialize
// public methods
/// <summary>
/// Deserializes an object from a BsonReader.
/// </summary>
/// <param name="bsonReader">The BsonReader.</param>
/// <param name="nominalType">The nominal type of the object.</param>
/// <param name="actualType">The actual type of the object.</param>
/// <param name="options">The serialization options.</param>
/// <returns>An object.</returns>
public override object Deserialize(
BsonReader bsonReader,
Type nominalType,
Type actualType,
IBsonSerializationOptions options)
{
VerifyTypes(nominalType, actualType, typeof(BsonArray));
var bsonType = bsonReader.GetCurrentBsonType();
switch (bsonType)
{
case BsonType.Array:
bsonReader.ReadStartArray();
var array = new BsonArray();
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
{
var value = (BsonValue)BsonValueSerializer.Instance.Deserialize(bsonReader, typeof(BsonValue), null);
array.Add(value);
}
bsonReader.ReadEndArray();
return array;
default:
var message = string.Format("Cannot deserialize BsonArray from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
}
示例9: Deserialize
public override object Deserialize(
BsonReader bsonReader,
Type nominalType,
IBsonSerializationOptions options
)
{
var bsonType = bsonReader.GetCurrentBsonType();
if (bsonType == BsonType.Null)
{
bsonReader.ReadNull();
return null;
}
var nvc = new NameValueCollection();
bsonReader.ReadStartArray();
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
{
bsonReader.ReadStartArray();
var key = (string)StringSerializer.Instance.Deserialize(bsonReader, typeof(string), options);
var val = (string)StringSerializer.Instance.Deserialize(bsonReader, typeof(string), options);
bsonReader.ReadEndArray();
nvc.Add(key, val);
}
bsonReader.ReadEndArray();
return nvc;
}
示例10: ReadPageSize
private static OXmlPageSize ReadPageSize(BsonReader bsonReader)
{
bsonReader.ReadStartDocument();
OXmlPageSize value = new OXmlPageSize();
while (true)
{
BsonType bsonType = bsonReader.ReadBsonType();
if (bsonType == BsonType.EndOfDocument)
break;
string name = bsonReader.ReadName();
switch (name.ToLower())
{
case "width":
if (bsonType == BsonType.Null)
break;
if (bsonType != BsonType.Int32)
throw new PBException($"wrong PageSize width value {bsonType}");
value.Width = bsonReader.ReadInt32();
break;
case "height":
if (bsonType == BsonType.Null)
break;
if (bsonType != BsonType.Int32)
throw new PBException($"wrong PageSize height value {bsonType}");
value.Height = bsonReader.ReadInt32();
break;
default:
throw new PBException($"unknow PageSize value \"{name}\"");
}
}
bsonReader.ReadEndDocument();
return value;
}
示例11: Deserialize
public override object Deserialize(
BsonReader bsonReader,
Type nominalType,
IBsonSerializationOptions options
)
{
var bsonType = bsonReader.CurrentBsonType;
if (bsonType == BsonType.Null) {
bsonReader.ReadNull();
return null;
} else if (bsonType == BsonType.Array) {
bsonReader.ReadStartArray();
var list = new ArrayList();
var discriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(typeof(object));
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) {
var elementType = discriminatorConvention.GetActualType(bsonReader, typeof(object));
var serializer = BsonSerializer.LookupSerializer(elementType);
var element = serializer.Deserialize(bsonReader, typeof(object), elementType, null);
list.Add(element);
}
bsonReader.ReadEndArray();
return list;
} else {
var message = string.Format("Can't deserialize a {0} from BsonType {1}", nominalType.FullName, bsonType);
throw new FileFormatException(message);
}
}
示例12: Deserialize
// public methods
/// <summary>
/// Deserializes an object from a BsonReader.
/// </summary>
/// <param name="bsonReader">The BsonReader.</param>
/// <param name="nominalType">The nominal type of the object.</param>
/// <param name="actualType">The actual type of the object.</param>
/// <param name="options">The serialization options.</param>
/// <returns>
/// An object.
/// </returns>
public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
{
if (bsonReader.GetCurrentBsonType() == BsonType.Null)
{
bsonReader.ReadNull();
return null;
}
else
{
bsonReader.ReadStartDocument();
DeserializeType(bsonReader, "link");
bsonReader.ReadName("properties");
bsonReader.ReadStartDocument();
var href = bsonReader.ReadString("href");
string hrefType = null;
if (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
{
hrefType = bsonReader.ReadString("type");
}
bsonReader.ReadEndDocument();
bsonReader.ReadEndDocument();
return new GeoJsonLinkedCoordinateReferenceSystem(href, hrefType);
}
}
开发者ID:Khosrow-Azizi,项目名称:MasterExperimentV2,代码行数:36,代码来源:GeoJsonLinkedCoordinateReferenceSystemSerializer.cs
示例13: _Deserialize
public OXmlOpenHeaderElement _Deserialize(BsonReader bsonReader, IBsonSerializationOptions options)
{
OXmlOpenHeaderElement element = new OXmlOpenHeaderElement();
while (true)
{
BsonType bsonType = bsonReader.ReadBsonType();
if (bsonType == BsonType.EndOfDocument)
break;
string name = bsonReader.ReadName();
switch (name.ToLower())
{
case "type":
if (bsonType != BsonType.String)
throw new PBException($"wrong type value {bsonType}");
string type = bsonReader.ReadString();
//"openfooter"
if (type.ToLower() != "openheader")
throw new PBException($"invalid Type {type} when deserialize OXmlOpenHeader");
break;
case "headertype":
if (bsonType == BsonType.Null)
break;
if (bsonType != BsonType.String)
throw new PBException($"wrong HeaderType value {bsonType}");
element.HeaderType = bsonReader.ReadString().zParseEnum<HeaderFooterValues>();
break;
default:
//OpenHeaderFooter
throw new PBException($"unknow OpenHeader value \"{name}\"");
}
}
return element;
}
示例14: _Deserialize
public OXmlSimpleFieldElement _Deserialize(BsonReader bsonReader, IBsonSerializationOptions options)
{
OXmlSimpleFieldElement element = new OXmlSimpleFieldElement();
while (true)
{
BsonType bsonType = bsonReader.ReadBsonType();
if (bsonType == BsonType.EndOfDocument)
break;
string name = bsonReader.ReadName();
switch (name.ToLower())
{
case "type":
if (bsonType != BsonType.String)
throw new PBException($"wrong type value {bsonType}");
string type = bsonReader.ReadString();
if (type.ToLower() != "simplefield")
throw new PBException($"invalid Type {type} when deserialize OXmlSimpleFieldElement");
break;
case "instruction":
if (bsonType == BsonType.Null)
break;
if (bsonType != BsonType.String)
throw new PBException($"wrong Instruction value {bsonType}");
element.Instruction = bsonReader.ReadString();
break;
default:
throw new PBException($"unknow SimpleField value \"{name}\"");
}
}
return element;
}
示例15: Deserialize
/// <summary>
/// Deserializes an object from a BsonReader.
/// </summary>
/// <param name="bsonReader">The BsonReader.</param>
/// <param name="nominalType">The nominal type of the object.</param>
/// <param name="options">The serialization options.</param>
/// <returns>An object.</returns>
public override object Deserialize(
BsonReader bsonReader,
Type nominalType,
IBsonSerializationOptions options
) {
var bsonType = bsonReader.CurrentBsonType;
if (bsonType == BsonType.Null) {
bsonReader.ReadNull();
return null;
} else if (bsonType == BsonType.Document) {
var dictionary = CreateInstance(nominalType);
bsonReader.ReadStartDocument();
var discriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(typeof(object));
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) {
var key = bsonReader.ReadName();
var valueType = discriminatorConvention.GetActualType(bsonReader, typeof(object));
var valueSerializer = BsonSerializer.LookupSerializer(valueType);
var value = valueSerializer.Deserialize(bsonReader, typeof(object), valueType, null);
dictionary.Add(key, value);
}
bsonReader.ReadEndDocument();
return dictionary;
} else if (bsonType == BsonType.Array) {
var dictionary = CreateInstance(nominalType);
bsonReader.ReadStartArray();
var discriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(typeof(object));
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) {
bsonReader.ReadStartArray();
bsonReader.ReadBsonType();
var keyType = discriminatorConvention.GetActualType(bsonReader, typeof(object));
var keySerializer = BsonSerializer.LookupSerializer(keyType);
var key = keySerializer.Deserialize(bsonReader, typeof(object), keyType, null);
bsonReader.ReadBsonType();
var valueType = discriminatorConvention.GetActualType(bsonReader, typeof(object));
var valueSerializer = BsonSerializer.LookupSerializer(valueType);
var value = valueSerializer.Deserialize(bsonReader, typeof(object), valueType, null);
bsonReader.ReadEndArray();
dictionary.Add(key, value);
}
bsonReader.ReadEndArray();
return dictionary;
} else {
var message = string.Format("Can't deserialize a {0} from BsonType {1}.", nominalType.FullName, bsonType);
throw new FileFormatException(message);
}
}