I know that normally the method Newtonsoft.Json.JsonConvert.DeserializeObject
is used. However in this case I do a serialization, followed by a deserialization which doesn't work.
static public void mess(test message)
{
try
{
test temp = new test(id); // The only thing I don't show is that I obtain a valid ObjectId in id
string messageSerialized = Newtonsoft.Json.JsonConvert.SerializeObject(temp);
Newtonsoft.Json.JsonConvert.DeserializeObject<test>(messageSerialized);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
I get the following exception: Error converting value "57ab57ba86597bac513ce130" to type MongoDB.Bson.ObjectId'. Path 'a', line 1, position 31."
I get that the problem is the type, however how could it serialize the type but not be able to deserialize it?
Structure of test:
public class test
{
public MongoDB.Bson.ObjectId a;
public test(MongoDB.Bson.ObjectId b)
{
a = b;
}
}
messageSerialized = "{\"a\":\"57ab57ba86597bac513ce130\"}"