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

Deserialize json generated by mongodb that contains bson datatypes

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

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

JSON 文件包含如下内容:

"someKey" : NumberLong("1234567889"),

里面还有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);
                    }
                }
            }

您可以使用 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);

来源here

编辑

我对给定的方法没有任何问题。请参阅 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);
                }
            }

source (sln project)