0

I am using System.Web.Helpers.Json.Encode to encode an EF Table. The problem is that on my local instance -- using the same database mind you -- the table encodes just fine. "{\"Symbols\":[],\"Medium\":null,\"Element2\":null,\"Element1\":null,\"Id\":82,\"Localized\":true,\"PostalCode\":null,\"State\":\"AZ\",\"Category\":null}"

However, when the code is deployed to the server, it throws a circular reference error. I am thinking it may be a server configuration problem -- a stray checkbox, wrong library version, or some such. Before I go digging in, just wondering if other folks had encountered this and if so, could they point me in the right direction.

The code itself is not horribly complicated a simple table with about 9 fields, 4 of which are navigation -- and are null in the object. The object itself is of type dynamic.

Thoughts?

JasonKretzer
  • 192
  • 1
  • 11
  • Are you detaching the object from the db before serializing it? It is possible the nav properties are being lazy loaded while serializing and causing the circular references. – Travis J Aug 22 '13 at 20:11
  • Ah, that is probably what is going on. Thanks! – JasonKretzer Aug 23 '13 at 02:20

1 Answers1

0

I am thinking it may be a server configuration problem -- a stray checkbox, wrong library version, or some such.

No, you are thinking wrong. It's just as the error message states: the object you are trying to JSON serialize is containing circular references which is not supported.

Why it works locally then?

Maybe because the object you are serializing on the server contains circular references which have values contrary to what you have locally.

To ensure that you do not have such problems always serialize a specifically designed view mode to JSON. This view model will cut all circular references.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • The objects are exactly the same. I am using the exact same database, the object represents the exact same row. Define: "Specifically designed view mode" It has no circular references, I believe, because the navigation fields that actually link to other tables are null in this instance. The fields with data are straight data strings and ints. – JasonKretzer Aug 22 '13 at 20:02
  • Well, as the error states you have circular dependencies in your object graph which cannot be JSON serialized. Maybe if you showed your entire object graph we could have been able to further analyze the problem and help you. – Darin Dimitrov Aug 22 '13 at 20:04
  • It is weird. Been looking for hours at this and just found this to the right of this thread. I think I know what you mean be designed view mode now as well. http://stackoverflow.com/questions/14592781/json-a-circular-reference-was-detected-while-serializing-an-object-of-type?rq=1 – JasonKretzer Aug 22 '13 at 20:13