Json.net deserialize json with circular link in MVC4

I try not to delete the deserialization of the circular graph of objects,

Json.net works well when serializing circular references: adding $ id to objects and replacing objects with $ ref = * objectId. When I send the same data back to the MVC action, it will not deserialize correctly - replacing $ refs with empty objects.

I use json.net for serialization and deserialization, deserialization is implemented in a custom IValueProvider - https://json.codeplex.com/discussions/347099

I noticed that when deserializing an anonymous object

JsonConverter.Deserialize(json); 

he will not engage in circular links. but when deserializing using a generic type

 JsonConverter.Deserialize<EntityType>(json) 

it will deserialize correctly.

But I can not find the type in GetValueProvider (ControllerContext controllerContext)

any help would be appreciated

Edit- In my current fix, I pass all json as a string and use

 JsonConverter.Deserialize<EntityType>(json) 

with the right type in the controller action, but this is definitely not the right way to work with json + mvc4 ... I need a better way to integrate it into mvc, started the generosity

Edit - More Code Type:

 [JsonObject(IsReference = true)] public class DynamicEntity : EntityWithID<Guid> { .... public virtual IList<DynamicEntity> ReferenceFields { get; set; } } 

json to deserialize is the output of the Serialize method for Json.net.

 {"$id":"1","ReferenceFields":[{"$ref":"1"}],"Id":"9f9de7f3-865e-4511-aeac-a2ff01193b06"} 

The problem is integration with MVC, as json goes back and forth between the server and the client. I already have js methods to change it back and forth in the same exact format - it is checked how I use it now:

 public ActionResult EntitySaveOrUpdate(string entity) { var entityToSave = JsonConvert.DeserializeObject<DynamicEntity>(entity); ... } 

And it works fine, but I need better integration with MVC, not deserialization in all of my actions ...

+7
c # serialization asp.net-mvc asp.net-mvc-4
source share
2 answers
 The challenge is dynamically typing at compile-time an un-typed languages (JSON). 

Since JSON is actually a Java-Script Designation of objects , and JavaScript is weakly typed (or not typed), the compilation -time-translator is not sure that "1" is of type int , double or .

Please note: this problem does not occur when using DynamicEntity , since Run-Time is allowed to interpret similarly to JavaScript .. p>

  • This post on how dynamics and statically anonymous types handle deserialization is a bit outdated, but still relevant. Their approach is to use JsonConvert.DeserializeAnonymousType() - a dynamic method - to return anonymous.

  • This post extends to Web Api DataContractJsonSerializer and how it restricts your parameters to anonymous types. The answer is suggested.

"to replace MediaTypeFormatter and plug in our own JSON serializer"

This means a lot of work for your limited needs.

What you find is not in line with the Linq projection, your DynamicEntity is probably the good approach you will find.

+2
source share

You tried to write a JsonConverter and register it in your ValueProviderFactory.

 JSONSerializer.Converters.Add(new DynamicEntityObjectConverter()); 

And inside the converter, you can manually use the JsonConverter.Deserialize<EntityType>(json) , which works for a circular reference.

+1
source share

All Articles