system.text.json

How to deserialize stream to object using System.Text.Json APIs

南笙酒味 提交于 2021-02-07 12:52:50
问题 I'm receiving a response from a web api call as a stream and need to deserialize it to a model. This is a generic method, so I can't say which parts of code will use this and what's the response payload. Here's the method: public async Task<T> InvokeAsync<T>(string method) { Stream response = await this.httpClientWrapper.InvokeAsync(method); var serializer = new JsonSerializer(); using var streamReader = new StreamReader(response); using var reader = new JsonTextReader(streamReader); return

Exclude an enum property of a Model from using the JsonStringEnumConverter which is globally set at the Startup?

自闭症网瘾萝莉.ら 提交于 2021-02-05 09:02:00
问题 I am developing the ASP.NET Core Application using the latest .NET Core 3.1.1 and System.Text.Json which was previously using the Newtonsoft.Json . As recommended in the Microsoft Migration guide I have done the changes. Also, as most of my enums need to be serialized as String I have configured my Startup.cs ConfigureServices to use the JsonStringEnumConverter globally. public void ConfigureServices(IServiceCollection services) { // lines omitted for brevity services.AddControllers()

Exclude an enum property of a Model from using the JsonStringEnumConverter which is globally set at the Startup?

半世苍凉 提交于 2021-02-05 09:01:32
问题 I am developing the ASP.NET Core Application using the latest .NET Core 3.1.1 and System.Text.Json which was previously using the Newtonsoft.Json . As recommended in the Microsoft Migration guide I have done the changes. Also, as most of my enums need to be serialized as String I have configured my Startup.cs ConfigureServices to use the JsonStringEnumConverter globally. public void ConfigureServices(IServiceCollection services) { // lines omitted for brevity services.AddControllers()

JsonPropertyNameAttribute is not supported record from C#9? [duplicate]

耗尽温柔 提交于 2021-02-05 07:34:45
问题 This question already has an answer here : How do I target attributes for a record class? (1 answer) Closed last month . I want to use record with JsonPropertyName attribute, but it caused an error. This is not supported? Any workaround? public record QuoteResponse([JsonPropertyName("quotes")] IReadOnlyCollection<Quote>? Quotes); Error CS0592 Attribute 'JsonPropertyName' is not valid on this declaration type. It is only valid on 'property, indexer, field' declarations. 回答1: By default

Issues with System.Text.Json serializing Unicode characters (like emojis)

别说谁变了你拦得住时间么 提交于 2021-02-05 06:52:11
问题 I am upgrading an application from .NET Core 2.2 to .NET Core 3.0, and the new System.Text.Json serializer is not behaving the same as Newtonsoft did in 2.2. On characters like a non-breaking-space (\u00A0) or emoji characters, Newtonsoft (and even Utf8Json) serialize them as their actual characters, not the Unicode code. I've created a simple .NET Fiddle to show this. var input = new Foo { Bar = "\u00A0 Test !@#$%^&*() 💯\uD83D\uDCAF 你好" }; var newtonsoft = Newtonsoft.Json.JsonConvert

How to use default serialization in a custom System.Text.Json JsonConverter?

和自甴很熟 提交于 2021-01-28 11:29:19
问题 I am writing a custom System.Text.Json.JsonConverter to upgrade an old data model to a new version. I have overridden Read() and implemented the necessary postprocessing. However, I don't need to do anything custom at all in the Write() method. How can I automatically generate the default serialization that I would get if I did not have a converter at all? Obviously I could just use different JsonSerializerOptions for deserialization and serialization, however my framework doesn't provide

How to force System.Text.Json serializer throw exception when property is missing?

瘦欲@ 提交于 2021-01-27 07:16:57
问题 Json.NET behaviour could be defined by attributes: either use default or just throw an exception if json payload does not contain required property. Yet System.Text.Json serializer silently does nothing. Having class: public sealed class Foo { [Required] public int Prop {get;set;} = 10; } and deserializing empty object: JsonSerializer.Deserialize<Foo>("{}"); I simply get an instance of Foo with Prop=10 . I could not find any setting in JsonSerializerOptions to force it throw an exception. Is

How to add property in existing json using System.Text.Json library?

孤街浪徒 提交于 2020-12-30 07:35:51
问题 { "TestData":{ "Year__of__Account":"2019", "Tax___x0025_":"0.06", "Buildings__1":"1000", "Contents__1":"400", "Total_Insurable_Value":"100", "Buildings__Prem":"2560.8", "Contents__Prem":"1707.2", "YB__1":"1950", "No__Buildings":"55", "Location_Sprinklers_YN":"No", "test":"test" } } In the above sample JSON I want to add a property called "Name" with Value "John" inside property "TestData". How can I achieve this using .net Core 3.0 System.Text.Json library. I have tried using methods of

How to configure two JSON serializers and select the correct one based on the route

我怕爱的太早我们不能终老 提交于 2020-12-12 06:13:50
问题 I have an ASP.NET Core Web API project using .Net Framework 4.7 and I'm upgrading to .Net Core 3.1. One of the reasons that I'm upgrading is to use the new System.Text.Json serializer. Currently, I have some versions of the API based on the route, like: /api/v1/controller /api/v2/controller And I will create a new one (v3) to use the new serializer. But here is the problem: I want to keep using JSON.Net on the older routes, to avoid any possible formating problem with the integrated clients.

How to configure two JSON serializers and select the correct one based on the route

江枫思渺然 提交于 2020-12-12 06:12:47
问题 I have an ASP.NET Core Web API project using .Net Framework 4.7 and I'm upgrading to .Net Core 3.1. One of the reasons that I'm upgrading is to use the new System.Text.Json serializer. Currently, I have some versions of the API based on the route, like: /api/v1/controller /api/v2/controller And I will create a new one (v3) to use the new serializer. But here is the problem: I want to keep using JSON.Net on the older routes, to avoid any possible formating problem with the integrated clients.