反序列化 mongodb 生成的包含 bson 数据类型的 json

zxe*_*xed 5 c# serialization json mongodb bson

我收到了一些 JSON 数据文件 - 然而,它的每个对象中都包含 BSON 数据类型;最重要的是,它是一个非常大的 tojson 转储(数百万条记录)。

我正在尝试反序列化数据,但正如预期的那样,它失败了。

JSON 文件包含以下内容:

"someKey" : NumberLong("1234567889"),
Run Code Online (Sandbox Code Playgroud)

它还包含 ISODate...

有没有办法用 Json.net 来处理这个问题?似乎可能有一些设置让它使用自定义函数而不是特定键的内置解析器?

*更新为包括用于非常大(100GB+ 文件)的流+文本阅读器的代码

using (StreamReader file = File.OpenText(@"\\largedump.txt"))
            using (JsonTextReader reader = new JsonTextReader(file))
            {
                reader.SupportMultipleContent = true;    
                var serializer = new JsonSerializer();
                while (reader.Read())
                {
                    if (reader.TokenType == JsonToken.StartObject)
                    {
                        Contacts c = serializer.Deserialize<Contacts>(reader);
                        Console.WriteLine(c.orgId);
                    }
                }
            }
Run Code Online (Sandbox Code Playgroud)

pro*_*r79 2

您可以使用 mongo 驱动程序 bson 序列化器:

使用 MongoDB.Bson.Serialization;

  var bjson = @"{
                        '_id' : ObjectId('57ac672e34780e59784d7d2a'),
                        'ActivePick' : null,
                        'EventCodeId' : null,
                        'Frame' : { '$binary' : 'AgY=', '$type' : '00' },
                        'FrameTimeStamp' : ISODate('2016-08-11T11:53:18.541Z'),
                        'ServerUserId' : 0,
                        'ServerUserName' : null,
                        'SesionId' : 0,
                        'TraderId' : null,
                        'TraderName' : null
                    }";

        var bsonDocument = BsonDocument.Parse(bjson);
        var myObj = BsonSerializer.Deserialize<FrameDocument>(bsonDocument);
Run Code Online (Sandbox Code Playgroud)

来源在这里

编辑

我对给定的方法没有任何问题。请参阅 github 解决方案,因为它的序列化没有问题。

            string line;
            using (TextReader file = File.OpenText("ImportDataFromBJsonFile\\a.json"))
            {
                while ((line = file.ReadLine()) != null)
                {
                    var bsonDocument = BsonDocument.Parse(line);
                    var myObj = BsonSerializer.Deserialize<Zxed>(bsonDocument);
                }
            }
Run Code Online (Sandbox Code Playgroud)

来源sln 项目