Using ASP.NET Core 3, how do I make the json response not serialize a complex type?

Viewed 248

I have this model:

 public class Product
    {

        public Product()
        {
            this.Supplier = new Supplier();
        }

        public int Id { get; set; }

        public string Name { get; set; }

        public double Price { get; set; }

        public int SupplierId { get; set; }

        public ProductStatus Status { get; set; }

        public Supplier Supplier { get; set; }
    }

and another model:

 public class Supplier
    {
        public Supplier()
        {
            this.Products = new List<Product>();
        }

        public int Id { get; set; }

        public string Name { get; set; }

        public string Address { get; set; }

        public ICollection<Product> Products { get; set; }
    }

Whenever I try to send json response using the product model, I get this kind of result:

{
  "id": 1,
  "name": "HP-ENVY 15",
  "price": 800,
  "supplierId": 0,
  "status": 0,
  "supplier": {
    "id": 1,
    "name": "HP",
    "address": "Mckinley",
    "products": []
  }
}

And when trying to send a response using Supplier model:

{
  "id": 1,
  "name": "HP",
  "address": "Mckinley",
  "products": [
    {
      "id": 1,
      "name": "HP-ENVY 15",
      "price": 800,
      "supplierId": 0,
      "status": 0,
      "supplier": {
        "id": 0,
        "name": null,
        "address": null,
        "products": []
      }
    },
    {
      "id": 12,
      "name": "HP-PAVILION 14",
      "price": 550,
      "supplierId": 0,
      "status": 0,
      "supplier": {
        "id": 0,
        "name": null,
        "address": null,
        "products": []
      }
    },
    {
      "id": 13,
      "name": "HP-ENVY 17",
      "price": 1200.7,
      "supplierId": 0,
      "status": 0,
      "supplier": {
        "id": 0,
        "name": null,
        "address": null,
        "products": []
      }
    },
    {
      "id": 14,
      "name": "Printer xxx-2020",
      "price": 300.5,
      "supplierId": 0,
      "status": 0,
      "supplier": {
        "id": 0,
        "name": null,
        "address": null,
        "products": []
      }
    },
    {
      "id": 15,
      "name": "Compaq Presario",
      "price": 500.8,
      "supplierId": 0,
      "status": 0,
      "supplier": {
        "id": 0,
        "name": null,
        "address": null,
        "products": []
      }
    }
  ]
}  

Both responses tries to serialize the complext object inside them, is it possible to:

1.) When sending the json response for the Product model, It would only display Products and its Supplier only(the products property of that supplier would not be inlcluded) 2.) When sending the json response for the Supplier model, it would only display the Supplier and its products only(the supplier property for each product would not be inlcuded)

Are there json options that I need to configure on my middleware in order to achieve this? or should I create DTOs/classes where the Supplier complex object being referenced inside the Product model has no property of its product and vice-versa for the Supplier model(it has no products property).

Note: I'm aware that it's best to use viewmodel/dtos for json response, but in my example let's just say that both Product and Model are not domain classes but rather viewmodels, as my main problem is how do I prevent json from preventing serializing the object property.

1 Answers

JSON.net has the MaxDepth property for deserealizing up to a certain point.

Beware though that this will help you up to a point. It will not identify same object cyclic dependency, it will just stop at the second level of deserialization.

For the opposite thing (serialization) you can use json.net limit maxdepth when serializing to extend the JsonTextWriter

Related