如何使用C#Mongodb驱动程序版本2.2.3将BsonBinaryReaderRadBSONArray()的输出deserealize到对象

时间:2016-04-27 20:52:33

标签: mongodb deserialization bson mongodb-csharp-2.0

我正在使用BSON二进制阅读器来读取bson文件中的.bson文件我遇到bson数组来读取我正在使用BSONBinaryReader.ReadRawBSONArray()。最初我正在逃避这一部分,因为它非常复杂。另一种选择是从BSONBinaryReader.ReadStartArray()开始读取数组。但是数组和文档的边界没有很好地隔离。所以我选择用BSONBinaryReader.ReadRawBSONArray()读取数组,它将返回bytebuffer,我将从中获取字节,然后将其deserilize为object。

我遇到来自bsondump的结果json的第一个数组如下:

{
"_source": "o::core_account",
"_tid": "50f5e737784c3b7820000013",
"last_modified": 1381951459,
"secretKey": "jTsRVAW0ottCEYEJHTQsmQ==",
"allLibs": [
    "_core",
    "lava"
],
"activeLibs": [
    "_core",

    "lava"
],
"_traits": [
    "o::core_account",
    "lava::customer"
],
"logo": "513a0b00784c3b142500003c",
"activeApps": [ ],
"homeApp": "_core::home",
"addresses": [ ],
"phones": [ ],
"website": "",
"_last_modified": 1449257350,
"_last_modified_by": "50bfe2689d97a3f664000004",
"aclRoles": [ ],
"aclGroups": [ ],
"aclPermissions": [ ]

}

我创建的对象类如下:

[BsonIgnoreExtraElements]
public class Traits_core_account 
{
    //[BsonRequiredAttribute]
    public string _source { get; set; }
    [BsonIdAttribute]
    public string _tid { get; set; }
    [BsonElement("last_modified")]
    public DateTime last_modified { get; set; }
    [BsonElement("secretKey")]
    public string secretKey { get; set; }
    [BsonElement("allLibs")]
    public List<string> allLibs { get; set; }
    [BsonElement("activeLibs")]
    public List<string> activeLibs { get; set; }
    [BsonElement("_traits")]
    public List<string> _traits { get; set; }
    [BsonElement("logo")]
    public string logo { get; set; }
    [BsonElement("activeApps")]
    public List<object> activeApps { get; set; }
    [BsonElement("homeApp")]
    public string homeApp { get; set; }
    [BsonElement("addresses")]
    public List<object> addresses { get; set; }
    [BsonElement("phones")]
    public List<object> phones { get; set; }
    [BsonElement("website")]
    public string website { get; set; }
    [BsonElement("_last_modified")]
    public DateTime _last_modified { get; set; }
    [BsonElement("_last_modified_by")]
    public string _last_modified_by { get; set; }
    [BsonElement("aclRoles")]
    public List<object> aclRoles { get; set; }
    [BsonElement("aclGroups")]
    public List<object> aclGroups { get; set; }
    [BsonElement("aclPermissions")]
    public List<object> aclPermissions { get; set; }
}

我使用以下代码来阅读.bson文件

string inputFileName = @"E:\Xerva\2016\BSONTOSQL\SampleFiles (lava)\lava\profiles.bson"; // initialize to a file containing BSON

using (var stream = File.OpenRead(inputFileName))

//using (var reader = new BsonBinaryReader(stream))
using (var reader = new BsonBinaryReader(stream))
{
    int counter = 0;                
    while (!reader.IsAtEndOfFile())
    {
        .......
        .......
    }
}

while循环中我正在reader.readCurrentBSONType()并检查它是否为BSONAray,然后使用下面的代码进行去分类,但对象Traits_core_account a为空。

while (reader.CurrentBsonType == BsonType.Array)
{
    IByteBuffer buffer =  reader.ReadRawBsonArray();
    Byte[] arrbyte = new byte[buffer.Length];
    buffer.GetBytes(0, arrbyte, 0, buffer.Length);
    MemoryStream ms = new MemoryStream(arrbyte);
    Traits_core_account a = BsonSerializer.Deserialize<Traits_core_account>(arrbyte);
    reader.ReadBsonType();
}

0 个答案:

没有答案