Nswag: Epic: Support System.Text.Json

Created on 15 Jun 2019  路  16Comments  路  Source: RicoSuter/NSwag

A: Use System.Text.Json in generated C# models (DTOs):

B: Use System.Text.Json in JSON Schema generator (also used to generate OpenAPI model schemas):

Out of scope:

  • Use System.Text.Json to serialize schemas, documents, etc.

Tasks:

  • [ ] Document new property SerializerOptions

Most helpful comment

@8VAid8 Thanks for sharing your solution - we should support that out-of-the-box as a "generator template style", this is tracked mainly here:

https://github.com/RicoSuter/NJsonSchema/issues/1013

All 16 comments

We're currently moving internal libraries towards System.Text.Json and wonder what the plans for NSwag are regarding migration?

Dazinator makes 2316 sound like you would loose interoperability by not being able to use data contracts anymore. Could you perhaps comment on this and your planned schedule regarding System.Text.Json? Thanks a lot!

Do you need A or B?

We don't care about data contracts currently. So:

  • A: System.Text.Json <-- Waiting impatiently.
  • B: DataContract <-- More interesting from an academic/wider point of view...

Hello, how does it look with A?

No work done yet, but A is the one which is feasible to implement already. B is not possible because no metadata is exposed.

@RicoSuter Do you have any progress with A? I've checked a client behavior with replaced Newtonsoft by System.Text.Json. It works.

I can hardly wait to use the asynchronous deserialization (DeserializeAsync) from System.Text.Json.
It would be very handy to be able to choose between Newtonsoft and System.Text.Json for the generation of C# client.

I'd also love to see optional support for System.Text.Json :D

While this is in progress, is there a workaround to serialize enums as string? In my .net core 3.1 project with nswag.aspnetcore 13.4.2 it does not recognize services.AddJsonOptions(c=>c.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()))) and adding services.AddOpenApiDocument(c => c.DefaultEnumHandling = NJsonSchema.Generation.EnumHandling.String); break data class fields casing: id becomes Id.

Any update on this?

I didn't want to wait and just made a simple Newtonsoft -> System.Text.Json converter (it has planty bugs, i guess, but works for me) for my Blazor project. @Jacko1394 you can try this way while the feature implementation is in progress.

@8VAid8 Thanks for sharing your solution - we should support that out-of-the-box as a "generator template style", this is tracked mainly here:

https://github.com/RicoSuter/NJsonSchema/issues/1013

@dubtar @jnovick System.Text.Json support in the schema generator has been greatly improved, see https://github.com/RicoSuter/NJsonSchema/issues/1014 (NSwag v13.5.0)

Yep, thanks a ton! 13.5.0 resolved my issues - now typescript clients work for me with enums as string.

You could simply inject the serializer agnostically:
https://www.nuget.org/packages/Open.Serialization.Json/

Thanks alot @RicoSuter 馃檪 It works now out-of-the-box within my blazor wasm client.

Was this page helpful?
0 / 5 - 0 ratings