Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JsonSerializer.Deserialize is returning empty object

I have a webapi that returns some Json:

{"id":9,"businessName":"dummy","address":"pluto","products"[
{"id":762,"description":"Centralized needs-based website","price":1281.24,"stock":1600,"categories":[],"factory":null},
{"id":1027,"description":"Realigned 6th generation knowledge base","price":2398.16,"stock":19583,"categories":[],"factory":null},
{"id":1392,"description":"User-centric zero administration array","price":998.07,"stock":6124,"categories":[],"factory":null},
{"id":1800,"description":"Team-oriented reciprocal core","price":4422.95,"stock":17372,"categories":[],"factory":null},
{"id":2763,"description":"Sharable needs-based hierarchy","price":4122.98,"stock":17397,"categories":[],"factory":null},
{"id":6189,"description":"Re-engineered hybrid emulation","price":395.09,"stock":532,"categories":[],"factory":null}
]}

I then try to deserialize using:

using var response = await _httpClient.GetAsync($"{GetApiRouteFromEntity(entity)}/{entity.GetId()}");
        response.EnsureSuccessStatusCode();
        var responseContent = await response.Content.ReadAsStringAsync();
        T? item = JsonSerializer.Deserialize<T>(responseContent);

but this gives me an empty Factory with id 0 and all the other attributes to null

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Factory.cs

public class Factory : EntityBase
    {
        [DisplayName("ID")]
        public int Id { get; set; }

        [DisplayName("Nome Business")]
        public string? BusinessName { get; set; }

        [DisplayName("Indirizzo")]
        public string? Address { get; set; }

        [DisplayName("Prodotti")] 
        public virtual ICollection<Product> Products { get; set; }
        
        public override string ToString()
        {
            return $"[{Id}] {BusinessName}";
        }
    }

Product.cs

public class Product : EntityBase
    {
        [DisplayName("ID")]
        public int Id { get; set; }

        [DisplayName("Descrizione")]
        public string? Description { get; set; }

        [DisplayName("Prezzo")]
        public float Price { get; set; }

        [DisplayName("Magazzino")]
        public int Stock { get; set; }

        [DisplayName("Categorie")]
        public virtual ICollection<Category> Categories { get; set; }

        [DisplayName("Fabbrica")]
        public virtual Factory Factory { get; set; }

        public override string ToString()
        {
            return $"[{Id}] {Description}";
        }
    }

EntityBase.cs

public abstract class EntityBase
    {
        public virtual object GetId()
        {
            return GetType().GetProperty("Id").GetValue(this);
        }
    }

i guess its because the factory prop in the products is null but idk how to work around that

>Solution :

From the docs:

By default, property names and dictionary keys are unchanged in the JSON output, including case.

You can specify the property naming policy:

T? item = JsonSerializer.Deserialize<T>(responseContent, new JsonSerializerOptions
{
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});

to use camel case for all JSON property names, or mark all needed properties with JsonPropertyNameAttibute containing correct name:

public class Factory : EntityBase
{
    [DisplayName("ID")]
    [JsonPropertyName("id")] // and so on
    public int Id { get; set; }
    ....
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading