This project has moved and is read-only. For the latest updates, please go here.

Mixing LINQ-to-JSON and JsonConvert

Jun 18, 2009 at 2:44 PM
Edited Jun 18, 2009 at 2:44 PM

Is it possible to mix LINQ and JsonConverters?

I'm trying to do something like this:

var jsonReader = new JsonTextReader(new StringReader(data));
var jsonSerializer = new JsonSerializer();
var json = (JObject) jsonSerializer.Deserialize(jsonReader);

Revision r = json.Value<Revision>("base");

Where Revision is a class that has a JsonConverter specified:

[JsonObject(MemberSerialization.OptOut), JsonConverter(typeof(RevisionConverter))]
public class Revision ... 

It fails with an invalid cast, apparently not using the JsonConverter specified on the class.

I can work around it by converting "base" back to a JSON string and then deserializing that with JsonConvert.DeserializeObject, but that seems unnecessarily clunky.

Thanks for any guidance!

Jun 18, 2009 at 2:53 PM
Edited Jun 18, 2009 at 3:14 PM

By the way, assuming there's only one object in "data", the first three lines of my code are equivalent to:

var json = JObject.Parse(data);

Correct? Or is there a difference?

Jun 19, 2009 at 12:46 AM

Your own .NET class can have a LINQ to JSON property but a LINQ to JSON object can't contain your own .NET class.