Cannot be serialized to JSON because its IsReference setting is 'True'
2011-02-12 20:41
513 查看
Issue:
{"The type 'XXXXXXXXXX' 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."}
Solution:
http://blogs.microsoft.co.il/blogs/idof/archive/2008/09/30/serializing-entity-framework-object-to-json.aspx
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.
{"The type 'XXXXXXXXXX' 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."}
Solution:
http://blogs.microsoft.co.il/blogs/idof/archive/2008/09/30/serializing-entity-framework-object-to-json.aspx
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.
相关文章推荐
- The service cannot be activated because it does not support ASP.NET compatibility. ASP.NET compatibility is enabled for this application. Turn off ASP.NET compatibility mode in the web.config or add the AspNetCompatibilityRequirements attribute to the ser
- Can not delete a master page "This item cannot be deleted because it is still referenced by other pa
- Vbox error: cannot be closed because it is still attached to 1 virtual machines
- SharePoint : This item cannot be deleted because it is still referenced by other pages
- 在eclipse使用到json时出现The type java.lang.Object cannot be resolved. It is indirectly referenced from req
- The type javax.servlet.ServletContext cannot be resolved. It is indirectly referenced from required
- The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files
- The type java.util.Map$Entry cannot be resolved. It is indirectly referenced from required .class fi
- 使用openoffice将word文件转换为pdf格式遇到问题:The type com.sun.star.lang.XEventListener cannot be resolved. It is indirectly referenced from required
- The type javax.servlet.http.HttpServletRequest cannot be resolved. It is indirectly reference 问题之解决
- The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files
- 问题1-The type java.lang.String cannot be resolved. It is indirectly referenced from required .class files
- TaskStackBuilder$SupportParentable cannot be resolved. It is indirectly referenced from required .cl
- 报错:classpath resource [com/qinkangdeid/mapping/] cannot be resolved to URL because it does not exist
- The type org.apache.http.HttpResponse cannot be resolved. It is indirectly referenced from required
- The type java.io.ObjectInputStream cannot be resolved,it is indirectly referenced from required.clas
- Error!]the type java.lang.object cannot be resolved. it is indirectly referenced from required .class files
- 遇到的问题-----java The type java.lang.String cannot be resolved. It is indirectly referenced fr
- The type javax.servlet.http.HttpServletRequest cannot be resolved. It is indirectly referenced from
- The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files