How to convert an entity to Json?

Convert Entity Framework Object to JSON Object

  • 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?

  • Answer:

    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.

Johan at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

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 (http://automapper.codeplex.com/ 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 https://github.com/AutoMapper/AutoMapper/wiki/Flattening. Hope this helps.

Rune

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.