views:

2773

answers:

4

It seems that serializing Entity Framework objects into JSON is not possible using either WCF's native DataContractJsonSerializer or ASP.NET's native JavaScript Jersializer. This is due to the reference counting issues both serializers reject. I have also tried JSON.Net, which also fails specifically on a Reference Counting issue.

My objects are Entity Framework objects, which are overloaded to perform additional business functionality (eg. authentication, etc.) and I do not want to decorate these classes with platform-specific attributes, etc. as I want to present a platform-agnostic API.

I've actually blogged about the individual steps I went though at http://bloggingabout.net/blogs/program.x/archive/2009/03/18/wcf-json-serialization-woes.aspx

Have I missed something obvious?

+3  A: 

Microsoft made an error in the way they made EF objects into data contracts. They included the base classes, and the back links.

Your best bet will be to create equivalent Data Transfer Object classes for each of the entities you want to return. These would include only the data, not the behavior, and not the EF-specific parts of an entity. You would also create methods to translate to and from your DTO classes.

Your services would then return the Data Transfer Objects.

John Saunders
+11  A: 

The way I do this is by projecting the data I want to serialize into an anonymous type and serializing that. This ensures that only the information I actually want in the JSON is serialized, and I don't inadvertently serialize something further down the object graph. It looks like this:

var records = from entity in context.Entities
              select new 
              {
                  Prop1 = entity.Prop1,
                  Prop2 = entity.Prop2,
                  ChildProp = entity.Child.Prop
              }
return Json(records);

I find anonymous types just about ideal for this. The JSON, obviously, doesn't care what type was used to produce it. And anonymous types give you complete flexibility as to what properties and structure you put into the JSON.

Craig Stuntz
thanks for this, I've been browsing for hours on this!!
Peter
Excellent solution. Is there a viable way to deserialize a javascript object back into an EF object?
Samuel Meacham
Samuel, the default model binder can generally cope with EF types. But I prefer to deserialize to an edit-specific model, then map to the EF type.
Craig Stuntz
How would you do this when you have to return a list of entities?
Prabhu
@Prabhu, the code in my post *does* return a list. Have you tried it?
Craig Stuntz
+2  A: 

Don't know if this is appropriate. I have resolved my issue, in a roundabout way. Thanks very much for you guys' help, it was most appreciated and crucial.

I've blogged it at:

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

Program.X
A: 

One more solution if you want to have better code consistency is to use JavaScriptConverter which will handle circular reference dependencies and will not serialize such references.

I've blogged about here:

http://hellowebapps.com/2010-09-26/producing-json-from-entity-framework-4-0-generated-classes/

Mehal

related questions