Nswag: Support for blazor serialiser: Microsoft.JSInterop.Json

Created on 14 Jun 2019  路  5Comments  路  Source: RicoSuter/NSwag

I just checked out using nswag studio to generate a csharp client, for use within a blazor client side app.

Newtonsoft serializer is not available in blazor client side apps. However microsoft have provided the following json seriliasation mechanism for blazor apps based on Microsoft.JSInterop.Json:

          var thing =  Microsoft.JSInterop.Json.Deserialize<T>(responseText);
          var json =  Microsoft.JSInterop.Json.Serialize(thing);

Sadly I couldn't find any stream based serialisation operations, so when using this serialiser, current methods that use newtonsoft with streams would have to be replaced by the above two (i.e convert to string first).

Most helpful comment

Last time I tried Blazor it worked flawlessly with Newtonsoft.Json and the generated clients and models could be used without problems.

I think it doesn't make much sense to support Blazor's serializer as it will eventually use System.Text.Json.

However it makes sense to add an option to generate models (DTOs) to use System.Text.Json's serializer, this is tracked here: https://github.com/RicoSuter/NJsonSchema/issues/1013

I've created an epic regarding System.Text.Json support in general:
https://github.com/RicoSuter/NSwag/issues/2243

All 5 comments

You should be able to use Newtonsoft.Json in client-side Blazor apps. But the framework is switching to use the new System.Text.Json serializer.

@dougbu

@dazinator what is the exact issue you're hitting? For example does adding a reference to NewtonSoft.Json to your client-side app lead to an error?

@dougbo
In earlier days, newtonsoft did have some issues running on client side blazor due to lack of Linq - I am guessing based on your enquiry that these are now probably resolved :-) however I still think this issue still makes sense as a feature request - add support for blazor's native JSON serialisation mechanism rather than force a dependency on newtonsoft..

Last time I tried Blazor it worked flawlessly with Newtonsoft.Json and the generated clients and models could be used without problems.

I think it doesn't make much sense to support Blazor's serializer as it will eventually use System.Text.Json.

However it makes sense to add an option to generate models (DTOs) to use System.Text.Json's serializer, this is tracked here: https://github.com/RicoSuter/NJsonSchema/issues/1013

I've created an epic regarding System.Text.Json support in general:
https://github.com/RicoSuter/NSwag/issues/2243

@RicoSuter waiting for System.Text.Json works for me, closing

Was this page helpful?
0 / 5 - 0 ratings