Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a maximum depth of JSON serialization in .NET Web API

Let's say I have these entitites with these relations: This is just a fictious example, and not my current entities.

  • Course
  • User
  • NewsPosts

Courses have many Users, Users have many Courses

Courses has many NewsPosts, NewsPosts has many Courses

Users has many NewsPosts, NewsPosts has Many Users

I'm using Entity Framework code first with .NET Web API, which sends back entities in the form of JSON. When I try to Get a Course, it sends back a JSON result with the relations of the entites, which is fine, but I would wish to set a limit of how many levels it serializes so it does not serialize all the relations beyond the first or second level.

GET Course/ would be serialized to:

   {
     "Users":[{
                "id":1,
                "newsPosts": [{
                                "id":1,
                                "message":"foo"
                             }]
             }],
   "newsPosts":[{
                 "id":2,
                 "message":"bar"
               }]   

   }

What i would want is to serialize only 1 or maybe 2 levels, So that the result would be:

   {
     "Users":[{
                "id":1,
                "newsPosts": []
             }],
   "newsPosts":[{
                 "id":2,
                 "message":"bar"
               }]   

   }

I have added a:

    json.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

This is to remove the referencelooping.

So in short: Is there any way of setting a maximum amount of nodes to serialize?

like image 994
Are Almaas Avatar asked May 02 '13 12:05

Are Almaas


People also ask

What is the max JSON length?

Property Value The maximum length of JSON strings. The default is 2097152 characters, which is equivalent to 4 MB of Unicode string data.

What is Maxdepth in JSON?

Gets or sets the maximum depth allowed when reading JSON. Reading past this depth will throw a JsonReaderException. A null value means there is no maximum. The default value is null.

Which is better Newtonsoft JSON or System text JSON?

Text. Json is much faster than the Newtonsoft. Json.

What is serializing a JSON?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).


1 Answers

The way I see it you have 2 options:

  1. Write a custom serializer, or use an existing library if you can find one
  2. Prepare the data you want the endpoints to send back before you attempt to serialise anything.

One of the best practices when it comes to APIs is to only send back the data which is needed because this affects the size of the data sent back.

Entity Framework works in a certain way by providing way too much and you want to limit that. A common way of solving this problem is to not have the API return Entity Framework entities. There are a number of reasons for that which can be researched separately.

So, one approach would be to start using DTOs to return data. This has a number of advantages:

  1. You are in control of the data you send back and you don't need any custom serializer or yet another library.
  2. You have control over the contents of the response, meaning sometimes you don't need all public properties in a class to be sent back, you need a subset and you can control that.
  3. The API is not tightly coupled with Entity Framework and its entities anymore.

Disadvantages:

  1. You now have another set of classes to deal with, however this complexity can be easily managed with something like AutoMapper, for example.
like image 197
Andrei Dragotoniu Avatar answered Oct 22 '22 22:10

Andrei Dragotoniu