3

I am trying to deserialize a json string from an api.

All works except for a nested item - Location which always ends up null

This is the string structure:

[
    {
        "CompanyProfile": "<p>A&amp;B Inc etc...</p>",
        "Address": "56 Test Street, Test, UK",
        "Location": {
            "Latitude": 61.52787,
            "Longitude": -4.32095,
            "Zoom": 13,
            "Icon": null,
            "Format": 0,
            "Api": null,
            "SearchTyped": null
        },
        "Id": 1723,
        "Name": "A&B Inc"
    },
        {
        "CompanyProfile": "<p>B&amp;C Inc etc...</p>",
        "Address": "57 Test Street, Test, UK",
        "Location": {
            "Latitude": 61.2122,
            "Longitude": -4.31111,
            "Zoom": 13,
            "Icon": null,
            "Format": 0,
            "Api": null,
            "SearchTyped": null
        },
        "Id": 1723,
        "Name": "B&C Inc"
    },
]

These are the classes to map to:

public class MemberDto
{
    public int Id { get; set; }
    public string? Name { get; set; }
    public string? CompanyProfile { get; set; }
    public string? Address { get; set; }
    public Location? Location { get; internal set; }
}

public class Location
{
    public decimal Latitude { get; set; }
    public decimal Longitude { get; set; }
}

This is the deserialize code:

var result = await response.Content.ReadAsStringAsync();
var members = JsonConvert.DeserializeObject<List<MemberDto>>(result);

I know I can use ReadFromJsonAsync<List<MemberDto>>() as well but using ReadFromString so I can check the json before deserializing. Anyway the result is exactly the same for ReadFromJsonAsync.

Everything except Location is deserialized successfully

Anyone know what the issue is?

7
  • 3
    Remove internal: public Location? Location { get; internal set; } ______ public Location? Location { get; set; } Commented May 26, 2022 at 9:20
  • 3
    Why Location setter is internal? Commented May 26, 2022 at 9:20
  • Make your life easy; take your JSON, paste it into app.quicktype.io and follow the instructions in the comment header of the generated code; paste the generated code back into your app and put the one line of code from the header into the approppriate place in your app code (var members = YourRootObjectName.FromJson(result)) Commented May 26, 2022 at 9:22
  • 1
    @MarkusMeyer - thanks also for spotting answer Commented May 26, 2022 at 9:35
  • 1
    I had this same issue two days ago. It's the caveat where when you auto-add properties to a class (via quick refactoring) the setter is made internal by default. Terrible design choice by MS, because it causes these kinds of issues. (Mine was with asp.net model binding) Commented May 26, 2022 at 10:04

1 Answer 1

4

Remove the internal access modifier in the setter of Location.

public Location? Location { get; set; } 
Sign up to request clarification or add additional context in comments.

1 Comment

To add upon the answer: In general, for the deserialization to work, the setter should be public. So any non-public modifier, like internal, protected, private should be removed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.