3

I'm trying to make a generic handler post a JSONJ object based on my entity type SYSTEM_AUDIT_SHEET:

SYSTEM_AUDIT_SHEET sheet = ctx.SYSTEM_AUDIT_SHEET
                              .Where(s => s.SYSTEM_KEY == system_key_dec)
                              .Select(s => s)
                              .OrderByDescending(s => s.AUDIT_SHEET_VERSION)
                              .First();

HttpContext.Current.Response.Write(serializer.Serialize(sheet));

But I get the following error:

A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.SYSTEM_AUDIT_SHEET_521A7B786A51FC0F83641182DD72C8DFE6C082418D30BBB977B403409A74CE17'.

Why can't I convert the entity to JSON?

2
  • Create a new class similar to the object you want to serialize but without having circular reference. Fill the data in that class from sheet object and serialize it. It's a bad practice to serialize Entity Framework Object directly because of this sort of problem. Commented Oct 24, 2011 at 13:58
  • Duplicate question; answers are "use JSON.Net" which has several solutions to this problem without copy/pasting classes. stackoverflow.com/questions/4606232/… Commented Oct 24, 2011 at 14:54

2 Answers 2

1

You cannot convert objects to json that reference themselves as this would create an infinitely long json string.

For example, the following pseudo-code wouldn't work because it sets up a circular reference (Dog >> Bone >> Dog...):

class Dog {
    private Bone myBone;
    public Dog() {
        myBone = new Bone(this);
    }
}

class Bone {
    private Dog buriedBy;
    public Bone(Dog d) {
        buriedBy = d;
    }
}

There seem to be some good solutions by googling 'json circular reference'. See the top two stack overflow links.

Sign up to request clarification or add additional context in comments.

3 Comments

'Google It' is not a very helpful answer. Could you provide a link?
I provided an answer and a way of getting more material. There were multiple links I referenced on the search page... really giving a specific google search term was the most effective way of providing the additional reference material.
1

The problem is probably that your SYSTEM_AUDIT_SHEET either contains a property that references instances of type SYSTEM_AUDIT_SHEET, or it contains a property that points to objects that have pointers to SYSTEM_AUDIT_SHEET instances. Serializing such a circle of pointers would result in a serialization process that would never end.

You will need to transform your SYSTEM_AUDIT_SHEET to a type that does not (directly or indirectly) reference itself before doing the serialization. You can create a brand new type and write code to instantiate such a type from your SYSTEM_AUDIT_SHEET (AutoMapper might come in handy here). However, I tend to find that in most cases it is easier to just use an anonymous type:

SYSTEM_AUDIT_SHEET sheet = /*some sheet*/
var json = new {
  sheet.Id,
  sheet.RevisionNumber,
  sheet.Title
};
return serializer.Serialize(json);

EDIT

If you want to use AutoMapper and assuming that your sheet looks something like

class SYSTEM_AUDIT_SHEET 
{
    public int Id { get; set; }
    public SYSTEM_AUDIT_SHEET SomeOtherAuditSheet { get;set;}
    public string Title { get;set;}
}

you could create a type like

class JSON_SYSTEM_AUDIT_SHEET
{
    public int Id { get; set; }
    public int SomeOtherAuditSheetsId { get;set;}
    public string Title { get;set;}
}

When your application starts (say, in Application_Start) you configure AutoMapper:

AutoMapper.Mapper.CreateMap<SYSTEM_AUDIT_SHEET, JSON_SYSTEM_AUDIT_SHEET>()
  .ForMember(dest => dest.SomeOtherAuditSheetsId, opt => opt.MapFrom(src => src.SomeOtherAuditSheet.Id));

The Id and Title properties will be mapped directly across from SYSTEM_AUDIT_SHEET to JSON_SYSTEM_AUDIT_SHEET because they have the same names in both types. The property SomeOtherAuditSheetsId needs special configuration, because there is no property with that exact name on the source type.

When you want to convert SYSTEM_AUDIT_SHEET to JSON_SYSTEM_AUDIT_SHEET you do:

return AutoMapper.Mapper.Map<SYSTEM_AUDIT_SHEET , JSON_SYSTEM_AUDIT_SHEET >(sheet);

You may want to have a look at AutoMapper's flattening features.

Hope this helps.

2 Comments

Yea but since there are like 30 properties i dont want to do that manually. I suppose thats what automapper is for? Could you show me how its used?
+1 for use of AutoMapper. Anyone aware of an existing tool or template that automatically duplicates a POCO for this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.