zxed
zxed

Reputation: 336

Deserialize json generated by mongodb that contains bson datatypes

I received a few JSON data files- however, it has the BSON datatypes included in each object; on top of that, its a really large tojson dump (millions of records).

I am trying to deserialize the data and as expected it fails.

The JSON file has things like:

"someKey" : NumberLong("1234567889"),

It also has ISODate in there...

Is there a way to handle this with Json.net? Seems like there is probably some setting to have it use a custom function rather than the built in parser for specific keys?

*Updated to include code for the stream+textreader for the very large (100GB+ file)

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);
                    }
                }
            }

Upvotes: 5

Views: 6433

Answers (1)

profesor79
profesor79

Reputation: 9473

You can use mongo driver bson serializer:

using 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);

source here

EDIT

I had no issues with given approach. Please see github solution as it is serializing without issues.

            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)

Upvotes: 2

Related Questions