How do I support JSON with circular references using jQuery and ASP.NET MVC?

In short, I'm looking for a way to move complex graphs of objects between browser code (jQuery) and server code (ASP.NET MVC). JSON does not support object references, so standard serialization of a circular object of reference is not possible out of the box.

The proposed standard for JSON object references floats, which is currently implemented by DoJox and (apparently) the .NET DataContractJsonSerializer class.

Although DoJo seems like a mature JavaScript library, I can't run DoJo side by side with jQuery.

Does anyone know of a technique or plugin that will allow me to serialize, deserialize complex JSON graphical objects using jQuery?

+3
source share
2 answers

While it does not allow serialization of circular references, .NET has the [ScriptIgnore] attribute that you can place on your ViewModels, which will prevent properties from being serialized for JSON and thus avoid cyclic link errors so often with complex relationships in MVC.

In addition, instead of returning your model as is, using LINQ or anonymous objects to return only the properties that you need to represent on your client side script will help you cope with performance by minimizing the amount of data sent to the client.

+2
source

JSON.NET , . "$ id" JSON {"$ref"=1}, 1 "$ id" .

, , , . ( JSON JavaScript ( ), JSON.NET( ) " " ?), , - - , .

, . . , -, "$ id" . (, $ref), + , , , .

0

Source: https://habr.com/ru/post/1763963/


All Articles