'Deserialize Bson file

I have a Bson file generated with the tool mongodump. I want to deserialize within C# code. For that purpose, it seems that I can use mongodb C# driver or the Json.net library. I tried the both of them but I can't get them to work.

Using the Json.net library : "input" is the path to the Bson file

JsonSerializer serializer = new JsonSerializer();
BsonReader reader = new BsonReader(File.OpenRead(input));
serializer.Deserialize<List<Json.Profil>>(reader);

On Deserialize(), I get the following exception :

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[MoulinetteConsole.Json.Profil]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

[EDIT] If a export the bson to a json file using the tool bsondump, I get :

{ "id" : "1",
  "value" : { "foo" : null, "bar" : "test"}
}
{ "id" : "2",
  "value" : { "foo" : null, "bar" : "test"}
}

Moreover, If I replace:

serializer.Deserialize<List<Json.Profil>>(reader);

by

serializer.Deserialize<Json.Profil>(reader);

I do not longer get the exception, but I only retrieve the first record of the collection.



Solution 1:[1]

Finally found the answer. Lets say you are desrializing Animals and you have a class Animal on your c# code that mapps correctly to what you are deserializing. In that case this code will work:

Run this command to make sure you have a bson file:

mongodump --db=MyDatabase --collection=Animals --out=/tmp/output 

Then import the bson generated file as:

var path = "/tmp/output/MyDatabase/Animals.bson";

using var fileStream = File.OpenRead(path);
FileInfo fi = new FileInfo(path);
var size = fi.Length;
while (fileStream.Position<size)
{
    var animal = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<Animal>(fileStream);
    Console.WriteLine(animal.Name);
}

Solution 2:[2]

Your file probably contains Json looking like this

{
    "foo":1,
    "bar":"baz"
}

but by trying to deserialize to List<>, the deserializer is expecting

[{
    "foo":1,
    "bar":"baz"
},
{
    "foo":2,
    "bar":"baz"
},
...]

Simply changing the deserialization line to:

serializer.Deserialize<Json.Profil>(reader);

should work.

This is what the error is telling you:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[MoulinetteConsole.Json.Profil]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

Solution 3:[3]

Try setting the property for BSON Reader -- ReadRootValueAsArray = true

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tono Nam
Solution 2 dav_i
Solution 3 Rohit Mallya