Best JsonValue Serialization/Deserialization

Topics: jQuery
Apr 17, 2011 at 1:01 AM

Love the latest bits guys, great work!

So currently I am doing serialization back and forth between EF Code First POCOs using Json.Net. So I can do this in my repository:

var localObj = JsonConvert.DeserializeObject<myType>(inputParamJsonValue.ToString());
// add new object to database and it is updated by ref
return JsonValue.Parse(JsonConvert.SerializeObject(localObj));

Is there a better way using .ReadAs<T>(); ?


Apr 18, 2011 at 7:46 PM

I'm not sure I fully understand the scenario, but doesn't it make sense to use JSON.NET (or System.Json) end-to-end in this case? In that case your method would take an object and return an object and the serialization happens in a processor. I think @gblock can point you to a processor that uses JSON.NET.

The current bits already have built-in support for System.Json. If you take an object and return an object, it will use DataContractJsonSerializer. If you want to see the JSON in your method, you can take JsonValue and return a JsonValue... it's up to you.


Apr 18, 2011 at 9:14 PM

Yup, that's exactly what I ended up doing. I guess I didn't completely understand the purpose of JsonValue. I expected that it would be the means by which serialization/deserialization took place. This confirms my solution as not a hack, always good news ;)

Thanks Yavor