0

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\"}"
Cher
  • 2,789
  • 10
  • 37
  • 64
  • What's the definition of `BrowsersObj`? – Thomas Weller Aug 10 '16 at 17:09
  • Json.NET can serialize a type without a public constructor, but will fail to deserialize such a type. Possibly your data model includes such a type. Beyond that we need a [mcve]. – dbc Aug 10 '16 at 17:14
  • I could but it's a class with 15 to 20 properties, lot's of them are class containing class... is it really necessary since error is clearly on object ID property? – Cher Aug 10 '16 at 17:14
  • Then boil the class down to a single property that reproduces the problem. That's the idea of a [mcve]. – dbc Aug 10 '16 at 17:15
  • It looks like something wonky might be happening with MongoDB ObjectIds ([see this post](http://stackoverflow.com/questions/23726939/mongo-db-object-id-deserializing-using-json-serializer)). Can you post your serialized JSON? – Tim Cooke Aug 10 '16 at 17:17
  • updated it with an example more complete – Cher Aug 10 '16 at 17:25
  • Are you actually running that and getting that value? I suspect it would give you json like this: `{ "a":ObjectId("57ab57ba86597bac513ce130") }`, which is not valid JSON, but is, in fact, valid BSON. Your original question was serializing a much more complex class, which I suspect had somehow overridden something to output BSON instead of JSON. It seems possible that they overrode something in the more complex class you're trying to serialize/deserialize. – Tim Cooke Aug 10 '16 at 17:37
  • The variable messageSerialized value is as in the question I wrote. I don't have the ObjectId(). Does it mean the serialized I use do something incorrect? – Cher Aug 10 '16 at 17:40
  • 1
    OK. Perhaps you'll find a workaround in [this solution](http://stackoverflow.com/a/37966098/2041550). If so, please let me know so I can upvote it. – Tim Cooke Aug 10 '16 at 17:44
  • found something else, if it doesn't work I'll try it thanks – Cher Aug 10 '16 at 18:06

1 Answers1

0

Error converting value "here is an object ID" to type MongoDB.Bson.ObjectId'.

Yes, look at the error and as can see it's trying to deserialize the string "here is an object ID" to Bson.ObjectId which will never succeed. You should post your serialized JSON string along with structure of BrowsersObj.

Per your latest edit: "57ab57ba86597bac513ce130" is not a MongoDB ObjectId rather it's a string. Check your MongoDB collection and most probably you are not storing the _id as ObjectID rather you have overwritten them to store as string

Rahul
  • 76,197
  • 13
  • 71
  • 125
  • I edited it, it's an object ID actually. I didn't write it down, updated it will add the deserialized object with structure soon – Cher Aug 10 '16 at 17:13