6
var docToJson = doc.ToJson<BsonDocument>();
story Featured = JsonConvert.DeserializeObject<story>(docToJson);


public class story 
{
[JsonProperty("_id"), JsonConverter(typeof(ObjectIdConverter))]
public ObjectId Id { get; set; }
....

public class ObjectIdConverter : JsonConverter
{
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            serializer.Serialize(writer, value.ToString());
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue,        

 JsonSerializer serializer)
        {
            JToken token = JToken.Load(reader);
            return new ObjectId(token.ToObject<string>());
        }

        public override bool CanConvert(Type objectType)
        {
            return (objectType == typeof(ObjectId));
        }
      }
    }

I'm stuck I've tried half a dozen methods, I'm still getting the same error with json reader, any ideas anyone?

Last tried this from SO*

JsonReader Exception

Unexpected character encountered while parsing value: O. Path '_id', line 1, position 10.

The JSON string looks like this:

{
    "_id": ObjectId("5378f94a3513fa3374be7e20"),
    "cc": "GB",
    "userName": "xyz ",
    "userImage": "img/16.jpg",
    "createdDate": ISODate("2014-05-18T18:17:46.983Z"),
    "Headling": "Veniam, amet, incidunt veniam, ipsam nostrud natus exercitationem consectetur, eos dolorem. ",
    "subheading": "Veniam, amet, incidunt veniam, ipsam nostrud. "
}
2
  • Please post the JSON string that you are trying to deserialize. Commented May 18, 2014 at 23:29
  • Hi, it's coming from the DB, looks like this { "_id" : ObjectId("5378f94a3513fa3374be7e20"), "cc" : "GB", "userName" : "xyz ", "userImage" : "img/16.jpg", "createdDate" : ISODate("2014-05-18T18:17:46.983Z"), "Headling" : "Veniam, amet, incidunt veniam, ipsam nostrud natus exercitationem consectetur, eos dolorem. ", "subheading" : "Veniam, amet, incidunt veniam, ipsam nostrud. " } Commented May 19, 2014 at 0:16

2 Answers 2

7

You are getting this error because the value for the _id property does not conform to the JSON standard (see JSON.org). JSON values must be one of the following:

  • a string (starts and ends with quote marks ")
  • a number
  • an object (starts and ends with curly braces { and })
  • an array (starts and ends with square brackets [ and ])
  • the keywords true, false, or null

The value ObjectId("5378f94a3513fa3374be7e20") appears to be a function, which is not valid. The value ISODate("2014-05-18T18:17:46.983Z") has the same problem. You will need to somehow change your JSON to meet the standard if you want to parse it using JSON.net.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 for being technically correct, this is the DB (BSON) format, strangely the BSON does convert to JSON var docToJson = doc.ToJson<BsonDocument>(); just can't desrialize with JSON.NET, thanks for your help, there's got to be a way, several pointers on here, can't get any of them to work
If you're starting with BSON, why not just deserialize directly from that instead of trying to convert it to JSON first? Perhaps it is the doc.ToJson that is causing the problem. There is an example in the Json.Net documentation that shows how to deserialize BSON.
Wow, didn't know it existed, that's awesome, thanks Brian, you've saved me some hassle !!!
0

The problem is that the MongoDB Bson Seralization output doesn't convert the objects to the plain Json expected for Json.Net. Fortunately, is possible to convert to BsonDocument to .Net object and then Serialize that object to Json.

object dotnetObject = BsonTypeMapper.MapToDotNetValue(bsonDocument);

// Json mapped to default .Net objects
string json = Newtonsoft.Json.JsonConvert.SerializeObject(dotnetObject);

// Parsing as JObject
var jobject = JObject.Parse(json);

// Deserializing as your custom Type
var myObject = JsonConvert.DeserializeObject<MyType>(json);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.