1

I have several projects in my solution. Here are 2 of them:

  • API (netcoreapp3.1 asp web API - hence System.Text.Json is available in it)
  • Entities (netstandard2.0 class library - hence System.Text.Json is not available there).

Entities contains DTOs(Data Transfer Objects) for my API. I need to provide my own JSON field names for DTOs. I came across this question, so i tried to use JsonPropertyNameAttribute, which is provided by System.Text.Json (.net core 3) and it does not work.

What do i need to do to specify custom JSON field names? Change the Entities project type to netcoreapp3.1 (then there are problems with other .net standard 2.0 libraries)? Use a different way of specifying custom JSON field names for DTO (i do not want to use Newtonsoft.Json because i heard that it has compatibility issues with .net core 3+ asp web APIs)?

Update: As strickt01 pointed, there is NuGet package that provides System.Text.Json serialization and deserialization for other .net versions, such as:

  • .NET Standard 2.0 and later
  • .NET Framework 4.7.2 and later
  • .NET Core 2.0, 2.1, and 2.2
6
  • You can map DTOs to another Class specified in API project and then use JsonPropertyNameAttribute. Commented Jul 20, 2020 at 14:28
  • @JacobSobus, but i do not want to have DTO-like classes inside my API project. It is the reason why i decided to put API and Entities into two separate projects. Commented Jul 20, 2020 at 14:34
  • 1
    @JacobSobus, inside my Entities project i have 2 types of entities. The first is EF Core Models (that matches my DB srtructure). The second type is DTOs that are used inside API endpoints. And they differ. And i need a way to change the JSON serialized DTO's field name from "CreationDate" to "creation_date". Commented Jul 20, 2020 at 15:19
  • 1
    then I will go with extracting DTOs to additional project that will be used by both: API and Entities. Make it as .net core project and use JsonPropertyNameAttribute :) That will be the best option IMHO :) Commented Jul 20, 2020 at 19:06
  • 1
    @JacobSobus thanks for your advice. I think i agree with You. Commented Jul 20, 2020 at 19:21

1 Answer 1

1

If you don't want to use Newtonsoft.Json (by setting it as the default serializer in your .net core 3 project) then you do hit the catch 22 you've identified. However if your custom names all follow the same format then you can set a custom naming policy for the System.Text.Json serializer. i.e.

services.AddControllers()
.AddJsonOptions(options =>
{
    options.JsonSerializerOptions.PropertyNamingPolicy = new PascalToUnderscoreNamingPolicy(),
});

...with PascalToUnderscoreNamingPolicy being a class overriding JsonNamingPolicy.ConvertName in order to convert the likes of "CreationDate" to "creation_date".

Sign up to request clarification or add additional context in comments.

6 Comments

thank You for the answer, but my custom names are not necessarily match property names
When you say that JsonPropertyNameAttribute "does not work" what do you mean - that the attributes are ignored despite being applied to the DTOs?
by this i mean that [JsonPropertyName] attribute is provided by System.Text.Json (available only in .net core 3) and it can not be used inside .net standard project (Entities project is netstandard 2.0 class library)
System.Text.Json does support .net standard 2.0 via the nuget package (see learn.microsoft.com/en-us/dotnet/standard/serialization/…)
Thank You. I did not know it.
|

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.