0

We are using ASP.NET Core 8 Web API. I have read this post, but I use System.Text.Json. So it is not duplicate question.

My goals are:

  1. read a json as a string in WEB API
  2. then deserialize it to class FooDto and return it to clients of WEB API.

I read the following json from database as a string:

{
    "product_id": 1
}

The dto class looks like this:

public class FooDto
{
    [JsonPropertyName("product_id")]
    public long? ProductId { get; set; }
}

The code that I tried looks like this:

JsonSerializer.Deserialize<FooDto>(json);

In addition, I tried like this:

JsonSerializerOptions serializeOptions = new ()
{
    PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
    WriteIndented = true
};
JsonSerializer.Deserialize<FooDto>(json, serializeOptions);

However, the result of the response of the ASP.NET Core Web API 8 method looks like this:

{
    "product_id": 1
}

But, the desired result should look like this. I mean, Web API should return the following result for clients:

{
    "productId": 1
}

Is it possible to use C# property name when deserializing json?

9
  • For your desired result, use this annotation in your class: [JsonPropertyName("productId")]. Whatever you define as an explicit annotation on the members of your class will override any naming applied by any conventions or serialization options Commented Oct 1, 2024 at 7:19
  • It's unclear to me what the exact problem is. If you use the naming attribute, it will use that name in deserialization and serialization. So, if you need two different names for in/out you can use that only for one direction. Commented Oct 1, 2024 at 7:23
  • My understanding is that you want to use the same class to deserialize {"product_id":1} from the db, but the API to return as {"productId":1}? If so, I am not sure that you can do it with a single class Commented Oct 1, 2024 at 7:28
  • 1
    Well, I guess it would somehow but only with significant effort and it would be somewhat brittle, I guess. Simply having a model from db and then map it to a dto is probably way quicker, testable, common practice and supported by numerous nugets that automate this for you. So, I would recommend that over fiddling with direction-sensitive json conversion ... Commented Oct 1, 2024 at 7:59
  • 1
    ^^ That would also be "least surprising" to fellow and/or future coders. Commented Oct 1, 2024 at 8:00

1 Answer 1

2

For your requirement, just remove the [JsonPropertyName] attribute and only use the following code can achieve. Also be sure you do not configure anything about Serialization/Deserialization in Program.cs:

string json = "{\r\n    \"product_id\": 1\r\n}";

JsonSerializerOptions serializeOptions = new ()
{
    PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
    WriteIndented = true
};
JsonSerializer.Deserialize<FooDto>(json, serializeOptions);
Sign up to request clarification or add additional context in comments.

2 Comments

I actually think having separate DB Entity and API DTO classes could be beneficial and the "common practice" thing to have here.
Thanks a lot! It works like a charm! :)

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.