本文整理汇总了C#中IBsonReader类的典型用法代码示例。如果您正苦于以下问题:C# IBsonReader类的具体用法?C# IBsonReader怎么用?C# IBsonReader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
IBsonReader类属于命名空间,在下文中一共展示了IBsonReader类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: GetActualType
// public methods
/// <summary>
/// Gets the actual type of an object by reading the discriminator from a BsonReader.
/// </summary>
/// <param name="bsonReader">The reader.</param>
/// <param name="nominalType">The nominal type.</param>
/// <returns>The actual type.</returns>
public Type GetActualType(IBsonReader bsonReader, Type nominalType)
{
// the BsonReader is sitting at the value whose actual type needs to be found
var bsonType = bsonReader.GetCurrentBsonType();
if (bsonType == BsonType.Document)
{
// ensure KnownTypes of nominalType are registered (so IsTypeDiscriminated returns correct answer)
BsonSerializer.EnsureKnownTypesAreRegistered(nominalType);
// we can skip looking for a discriminator if nominalType has no discriminated sub types
if (BsonSerializer.IsTypeDiscriminated(nominalType))
{
var bookmark = bsonReader.GetBookmark();
bsonReader.ReadStartDocument();
var actualType = nominalType;
if (bsonReader.FindElement(_elementName))
{
var context = BsonDeserializationContext.CreateRoot(bsonReader);
var discriminator = BsonValueSerializer.Instance.Deserialize(context);
if (discriminator.IsBsonArray)
{
discriminator = discriminator.AsBsonArray.Last(); // last item is leaf class discriminator
}
actualType = BsonSerializer.LookupActualType(nominalType, discriminator);
}
bsonReader.ReturnToBookmark(bookmark);
return actualType;
}
}
return nominalType;
}
开发者ID:narutoswj,项目名称:mongo-csharp-driver,代码行数:39,代码来源:StandardDiscriminatorConvention.cs
示例2: GetActualType
public Type GetActualType(IBsonReader bsonReader, Type nominalType)
{
if (nominalType == typeof(JobTask))
{
var ret = nominalType;
var bookmark = bsonReader.GetBookmark();
bsonReader.ReadStartDocument();
if (bsonReader.FindElement(ElementName))
{
var value = bsonReader.ReadString();
ret = Type.GetType(value);
if (ret == null)
throw new Exception("Could not find type " + value);
if (!ret.IsSubclassOf(typeof(JobTask)))
throw new Exception("Database type does not inherit from JobTask.");
}
bsonReader.ReturnToBookmark(bookmark);
return ret;
}
else
{
return nominalType;
}
}
开发者ID:NerdCats,项目名称:TaskCat,代码行数:30,代码来源:JobTaskDiscriminator.cs
示例3: GetActualType
// public methods
/// <summary>
/// Gets the actual type of an object by reading the discriminator from a BsonReader.
/// </summary>
/// <param name="bsonReader">The reader.</param>
/// <param name="nominalType">The nominal type.</param>
/// <returns>The actual type.</returns>
public Type GetActualType(IBsonReader bsonReader, Type nominalType)
{
// the BsonReader is sitting at the value whose actual type needs to be found
var bsonType = bsonReader.GetCurrentBsonType();
if (bsonReader.State == BsonReaderState.Value)
{
Type primitiveType = null;
switch (bsonType)
{
case BsonType.Boolean: primitiveType = typeof(bool); break;
case BsonType.Binary:
var bookmark = bsonReader.GetBookmark();
var binaryData = bsonReader.ReadBinaryData();
var subType = binaryData.SubType;
if (subType == BsonBinarySubType.UuidStandard || subType == BsonBinarySubType.UuidLegacy)
{
primitiveType = typeof(Guid);
}
bsonReader.ReturnToBookmark(bookmark);
break;
case BsonType.DateTime: primitiveType = typeof(DateTime); break;
case BsonType.Decimal128: primitiveType = typeof(Decimal128); break;
case BsonType.Double: primitiveType = typeof(double); break;
case BsonType.Int32: primitiveType = typeof(int); break;
case BsonType.Int64: primitiveType = typeof(long); break;
case BsonType.ObjectId: primitiveType = typeof(ObjectId); break;
case BsonType.String: primitiveType = typeof(string); break;
}
// Type.IsAssignableFrom is extremely expensive, always perform a direct type check before calling Type.IsAssignableFrom
if (primitiveType != null && (primitiveType == nominalType || nominalType.GetTypeInfo().IsAssignableFrom(primitiveType)))
{
return primitiveType;
}
}
if (bsonType == BsonType.Document)
{
var bookmark = bsonReader.GetBookmark();
bsonReader.ReadStartDocument();
var actualType = nominalType;
if (bsonReader.FindElement(_elementName))
{
var context = BsonDeserializationContext.CreateRoot(bsonReader);
var discriminator = BsonValueSerializer.Instance.Deserialize(context);
if (discriminator.IsBsonArray)
{
discriminator = discriminator.AsBsonArray.Last(); // last item is leaf class discriminator
}
actualType = BsonSerializer.LookupActualType(nominalType, discriminator);
}
bsonReader.ReturnToBookmark(bookmark);
return actualType;
}
return nominalType;
}
开发者ID:mfidemraizer,项目名称:mongo-csharp-driver,代码行数:64,代码来源:ObjectDiscriminatorConvention.cs
示例4: BsonDeserializationContext
// constructors
private BsonDeserializationContext(
IBsonReader reader,
bool allowDuplicateElementNames,
IBsonSerializer dynamicArraySerializer,
IBsonSerializer dynamicDocumentSerializer)
{
_reader = reader;
_allowDuplicateElementNames = allowDuplicateElementNames;
_dynamicArraySerializer = dynamicArraySerializer;
_dynamicDocumentSerializer = dynamicDocumentSerializer;
}
开发者ID:narutoswj,项目名称:mongo-csharp-driver,代码行数:12,代码来源:BsonDeserializationContext.cs
示例5: GetActualType
public Type GetActualType(IBsonReader bsonReader, Type nominalType)
{
var bookmark = bsonReader.GetBookmark();
bsonReader.ReadStartDocument();
string typeValue = string.Empty;
if (bsonReader.FindElement(ElementName))
typeValue = bsonReader.ReadString();
else
throw new NotSupportedException();
bsonReader.ReturnToBookmark(bookmark);
var retr = Type.GetType(typeValue) ?? Type.GetType("ThreeOneThree.Proxima.Core.Entities." + typeValue);
return retr;
}
开发者ID:surgicalcoder,项目名称:Proxima,代码行数:14,代码来源:ContentTypeDiscriminatorConvention.cs
示例6: GetActualType
public Type GetActualType(IBsonReader bsonReader, Type nominalType)
{
var bookmark = bsonReader.GetBookmark();
bsonReader.ReadStartDocument();
var actualType = nominalType;
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
{
var name = bsonReader.ReadName();
if (name == "OnlyInB")
{
actualType = typeof(B);
break;
}
else if (name == "OnlyInC")
{
actualType = typeof(C);
break;
}
bsonReader.SkipValue();
}
bsonReader.ReturnToBookmark(bookmark);
return actualType;
}
开发者ID:fir3pho3nixx,项目名称:mongo-csharp-driver,代码行数:23,代码来源:MagicDiscriminatorTests.cs
示例7: DeserializeRawBsonDocument
private RawBsonDocument DeserializeRawBsonDocument(IBsonReader bsonReader)
{
var slice = bsonReader.ReadRawBsonDocument();
var nestedDocument = new RawBsonDocument(slice);
_disposableItems.Add(nestedDocument);
return nestedDocument;
}
开发者ID:fir3pho3nixx,项目名称:mongo-csharp-driver,代码行数:7,代码来源:RawBsonArray.cs
示例8: VerifyName
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);
}
}
开发者ID:narutoswj,项目名称:mongo-csharp-driver,代码行数:11,代码来源:IBsonReaderExtensions.cs
示例9: JsonReaderAdapter
// constructors
public JsonReaderAdapter(IBsonReader wrappedReader)
{
_wrappedReader = wrappedReader;
}
开发者ID:rstam,项目名称:mongo-csharp-driver-jsondotnet-original,代码行数:5,代码来源:JsonReaderAdapter.cs
示例10: DeserializeRawBsonArray
private RawBsonArray DeserializeRawBsonArray(IBsonReader bsonReader)
{
var slice = bsonReader.ReadRawBsonArray();
var nestedArray = new RawBsonArray(slice);
_disposableItems.Add(nestedArray);
return nestedArray;
}
开发者ID:fir3pho3nixx,项目名称:mongo-csharp-driver,代码行数:7,代码来源:RawBsonArray.cs
示例11: TestArrayEmpty
public void TestArrayEmpty()
{
var json = "[]";
using (_bsonReader = new JsonReader(json))
{
Assert.Equal(BsonType.Array, _bsonReader.ReadBsonType());
_bsonReader.ReadStartArray();
Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
_bsonReader.ReadEndArray();
Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
}
Assert.Equal(json, BsonSerializer.Deserialize<BsonArray>(json).ToJson());
}
开发者ID:mfidemraizer,项目名称:mongo-csharp-driver,代码行数:13,代码来源:JsonReaderTests.cs
示例12: TestStringEmpty
public void TestStringEmpty()
{
var json = "\"\"";
using (_bsonReader = new JsonReader(json))
{
Assert.Equal(BsonType.String, _bsonReader.ReadBsonType());
Assert.Equal("", _bsonReader.ReadString());
Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
}
Assert.Equal(json, BsonSerializer.Deserialize<string>(json).ToJson());
}
开发者ID:mfidemraizer,项目名称:mongo-csharp-driver,代码行数:11,代码来源:JsonReaderTests.cs
示例13: TestSymbol
public void TestSymbol()
{
var json = "{ \"$symbol\" : \"symbol\" }";
using (_bsonReader = new JsonReader(json))
{
Assert.Equal(BsonType.Symbol, _bsonReader.ReadBsonType());
Assert.Equal("symbol", _bsonReader.ReadSymbol());
Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
}
Assert.Equal(json, BsonSerializer.Deserialize<BsonSymbol>(json).ToJson());
}
开发者ID:mfidemraizer,项目名称:mongo-csharp-driver,代码行数:11,代码来源:JsonReaderTests.cs
示例14: TestInt64ExtendedJson
public void TestInt64ExtendedJson()
{
var json = "{ \"$numberLong\" : \"123\" }";
using (_bsonReader = new JsonReader(json))
{
Assert.Equal(BsonType.Int64, _bsonReader.ReadBsonType());
Assert.Equal(123, _bsonReader.ReadInt64());
Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
}
var canonicalJson = "NumberLong(123)";
Assert.Equal(canonicalJson, BsonSerializer.Deserialize<long>(new StringReader(json)).ToJson());
}
开发者ID:mfidemraizer,项目名称:mongo-csharp-driver,代码行数:12,代码来源:JsonReaderTests.cs
示例15: TestRegularExpressionStrict
public void TestRegularExpressionStrict()
{
var json = "{ \"$regex\" : \"pattern\", \"$options\" : \"imxs\" }";
using (_bsonReader = new JsonReader(json))
{
Assert.Equal(BsonType.RegularExpression, _bsonReader.ReadBsonType());
var regex = _bsonReader.ReadRegularExpression();
Assert.Equal("pattern", regex.Pattern);
Assert.Equal("imxs", regex.Options);
Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
}
var settings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict };
Assert.Equal(json, BsonSerializer.Deserialize<BsonRegularExpression>(json).ToJson(settings));
}
开发者ID:mfidemraizer,项目名称:mongo-csharp-driver,代码行数:14,代码来源:JsonReaderTests.cs
示例16: TestHexData
public void TestHexData()
{
var expectedBytes = new byte[] { 0x01, 0x23 };
var json = "HexData(0, \"123\")";
using (_bsonReader = new JsonReader(json))
{
Assert.Equal(BsonType.Binary, _bsonReader.ReadBsonType());
var bytes = _bsonReader.ReadBytes();
Assert.True(expectedBytes.SequenceEqual(bytes));
Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
}
var expectedJson = "new BinData(0, \"ASM=\")";
Assert.Equal(expectedJson, BsonSerializer.Deserialize<byte[]>(json).ToJson());
}
开发者ID:mfidemraizer,项目名称:mongo-csharp-driver,代码行数:14,代码来源:JsonReaderTests.cs
示例17: TestNestedDocument
public void TestNestedDocument()
{
var json = "{ \"a\" : { \"b\" : 1, \"c\" : 2 } }";
using (_bsonReader = new JsonReader(json))
{
Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());
_bsonReader.ReadStartDocument();
Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());
Assert.Equal("a", _bsonReader.ReadName());
_bsonReader.ReadStartDocument();
Assert.Equal("b", _bsonReader.ReadName());
Assert.Equal(1, _bsonReader.ReadInt32());
Assert.Equal("c", _bsonReader.ReadName());
Assert.Equal(2, _bsonReader.ReadInt32());
_bsonReader.ReadEndDocument();
_bsonReader.ReadEndDocument();
Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
}
Assert.Equal(json, BsonSerializer.Deserialize<BsonDocument>(json).ToJson());
}
开发者ID:mfidemraizer,项目名称:mongo-csharp-driver,代码行数:20,代码来源:JsonReaderTests.cs
示例18: TestMinKeyExtendedJsonWithCapitalK
public void TestMinKeyExtendedJsonWithCapitalK()
{
var json = "{ \"$minKey\" : 1 }";
using (_bsonReader = new JsonReader(json))
{
Assert.Equal(BsonType.MinKey, _bsonReader.ReadBsonType());
_bsonReader.ReadMinKey();
Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
}
var canonicalJson = "MinKey";
Assert.Equal(canonicalJson, BsonSerializer.Deserialize<BsonMinKey>(new StringReader(json)).ToJson());
}
开发者ID:mfidemraizer,项目名称:mongo-csharp-driver,代码行数:12,代码来源:JsonReaderTests.cs
示例19: TestMinKeyKeyword
public void TestMinKeyKeyword()
{
var json = "MinKey";
using (_bsonReader = new JsonReader(json))
{
Assert.Equal(BsonType.MinKey, _bsonReader.ReadBsonType());
_bsonReader.ReadMinKey();
Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
}
Assert.Equal(json, BsonSerializer.Deserialize<BsonMinKey>(new StringReader(json)).ToJson());
}
开发者ID:mfidemraizer,项目名称:mongo-csharp-driver,代码行数:11,代码来源:JsonReaderTests.cs
示例20: TestJavaScriptWithScope
public void TestJavaScriptWithScope()
{
string json = "{ \"$code\" : \"function f() { return n; }\", \"$scope\" : { \"n\" : 1 } }";
using (_bsonReader = new JsonReader(json))
{
Assert.Equal(BsonType.JavaScriptWithScope, _bsonReader.ReadBsonType());
Assert.Equal("function f() { return n; }", _bsonReader.ReadJavaScriptWithScope());
_bsonReader.ReadStartDocument();
Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
Assert.Equal("n", _bsonReader.ReadName());
Assert.Equal(1, _bsonReader.ReadInt32());
_bsonReader.ReadEndDocument();
Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
}
Assert.Equal(json, BsonSerializer.Deserialize<BsonJavaScriptWithScope>(json).ToJson());
}
开发者ID:mfidemraizer,项目名称:mongo-csharp-driver,代码行数:16,代码来源:JsonReaderTests.cs
注:本文中的IBsonReader类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论