Serializing Entity Framework object to JSON

September 30, 2008

I’ve wrote before about trying to serialize EF objects to JSON (post in Hebrew), which to conclude, isn’t that easy.

DataContractJsonSerializer

EF objects are marked with IsReference=true and therefore cannot be serialized with DataContractJsonSerializer.

Trying to serialize EF objects with that serializer throws the following exception:
“The type ‘xxx’ cannot be serialized to JSON because its IsReference setting is ‘True’. The JSON format does not support references because there is no standardized format for representing references. To enable serialization, disable the IsReference setting on the type or an appropriate parent class of the type.”

BTW, that message is somehow misleading because you cannot “disable” the IsReference setting because all EF objects inherit from the EntityObject class which is also marked with IsReference=true

JavaScriptSerializer

According to MS connect, we should be able to use AJAX’s JavaScriptSerializer to serialize EF objects, but trying so raises the following exception:
“A circular reference was detected while serializing an object of type ‘XXX’.”

The circular reference exception will be raised when a navigable relation is double-sided (can access both sides of the relation), so the first thing to do is disable one side of the relation.

The exception will also be thrown when you use 1:1 relations (or 1:0..1 or any relation causing the creation of an EntityReference type property), in this case the exception will be of type ”System.Data.Metadata.Edm.AssociationType’.

The solution to this is to make the serializer ignore the properties of type EntityReference, using an empty implementation of a class deriving from JavaScriptConverter and registering it using the RegisterConverters method of the JavaScriptSerializer object.

So to conclude, serializing EF object to JSON is not quite of an easy task. There is, however, another solution to serialize EF objects, using the JSON serializer supplied with Ado.Net Data Services (Astoria), the catch is that the JSON serializer is an internal serializer, which means you’ll have to expose the EF as a Data Service, making this solution a bit annoying.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

5 comments

  1. KevinNovember 25, 2008 ב 6:35 pm

    Can you show me an example on how to implement this solution? Thanks

    The solution to this is to make the serializer ignore the properties of type EntityReference, using an empty implementation of a class deriving from JavaScriptConverter and registering it using the RegisterConverters method of the JavaScriptSerializer object.

    Reply
  2. Ido FlatowNovember 26, 2008 ב 2:29 am

    public class NotSerializedConverter : JavaScriptConverter
    {
    public override object Deserialize(…)
    {
    return null;
    }

    public override System.Collections.Generic.IDictionary Serialize(…)
    {
    return null;
    }

    public override System.Collections.Generic.IEnumerable SupportedTypes
    {
    get {return new List
    {typeof(T)}; }
    }
    }

    Reply
  3. TheoApril 26, 2012 ב 10:43 pm

    If you would like to take much from this post then you have to apply these methods to your won website.

    Reply
  4. Mark KamoskiMay 16, 2012 ב 8:33 pm

    Can you tell us how to set a WebApi Rest service such that it will always serialize to XML? We are specifiying the content type at the call site as…. myHttpWebRequest.ContentType = “text/xml; encoding=’utf-8′”; ….but it always seems to return JSON regardless. We want to return XML. We do not want to return JSON.

    Reply
  5. Nilesh JoshiNovember 25, 2012 ב 6:46 pm

    I faced the same issue while serializing entities to JSON using DataContractJSONSerializer (DCJS). Entity Framework + DCJS. Then I used LINQ to SQL to get the context and entity from SQL Server and using DCJS I was able to serialize entity I wanted. I think one can use Linq to Sql just serialize entities if at least that’s the goal.. !!

    Reply