Ef -> Wcf -> Json

Hi all,

I have a Windows Communication Foundation service which queries the Entity Framework and retrieves data in the form of Entity objects. I’d like to serialize these objects to JSON and send them back to the client to be interpreted in Javascript.

It seems this is tricky, according posts like these:

http://bloggingabout.net/blogs/program.x/archive/2009/03/18/wcf-json-serialization-woes.aspx

http://blogs.microsoft.co.il/blogs/idof/archive/2008/09/30/serializing-entity-framework-object-to-json.aspx

At this time, what’s the best way of doing this using Visual Studio 2010 Beta 2? Data transfer objects are one way but is there anything more elegant?

Thanks,

Calvin

Google “Newtonsoft JSON” It is a .net class library that somplifies working with json. And it is quite easy to use.

You can serialize and deserialize objects quite easily. Check the documentation.

I hope this helps you out.

I would also recommend not seralizing you actual domain entity and use some form of simplified DTO. Lazy loaded collections/relationships can cause all kinds of problems and you don’t want to polute your entity with Json related attributes. A DTO mapped with AutoMapper is generally a good way to go

^^^^What he said. I’d add you can do some slick stuff with interfaces, extension methods and validation buddies to keep the overhead managable.