system.text.json

System.Text.Json - Deserialize nested object as string

偶尔善良 提交于 2020-04-30 05:11:37
问题 I'm trying to use the System.Text.Json.JsonSerializer to deserialize the model partially, so one of the properties is read as string that contains the original JSON. public class SomeModel { public int Id { get; set; } public string Name { get; set; } public string Info { get; set; } } The example code var json = @"{ ""Id"": 1, ""Name"": ""Some Name"", ""Info"": { ""Additional"": ""Fields"", ""Are"": ""Inside"" } }"; var model = JsonSerializer.Deserialize<SomeModel>(json); should produce the

How to extract all values for all JsonProperty objects with a specified name from a JsonDocument? [closed]

元气小坏坏 提交于 2020-04-17 16:49:29
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 14 days ago . I have a free-form JsonDocument which contains a JsonProperty with name internalName . My problem is that that I don't have model to parse it into (because there is no predefined JSON schema) and the property can appear at any level, parent, child etc. How would one go about

How to extract all values for all JsonProperty objects with a specified name from a JsonDocument? [closed]

最后都变了- 提交于 2020-04-17 16:43:06
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 14 days ago . I have a free-form JsonDocument which contains a JsonProperty with name internalName . My problem is that that I don't have model to parse it into (because there is no predefined JSON schema) and the property can appear at any level, parent, child etc. How would one go about

Operation is not valid due to the current state of the object (System.Text.Json)

只谈情不闲聊 提交于 2020-04-07 05:05:58
问题 We've got an API, which simply posts incoming JSON documents to a message bus, having assigned a GUID to each. We're upgrading from .Net Core 2.2 to 3.1 and were aiming to replace NewtonSoft with the new System.Text.Json library. We deserialise the incoming document, assign the GUID to one of the fields and then reserialise before sending to the message bus. Unfortunately, the reserialisation is failing with the exception Operation is not valid due to the current state of the object . Here's

Operation is not valid due to the current state of the object (System.Text.Json)

本秂侑毒 提交于 2020-04-07 05:05:47
问题 We've got an API, which simply posts incoming JSON documents to a message bus, having assigned a GUID to each. We're upgrading from .Net Core 2.2 to 3.1 and were aiming to replace NewtonSoft with the new System.Text.Json library. We deserialise the incoming document, assign the GUID to one of the fields and then reserialise before sending to the message bus. Unfortunately, the reserialisation is failing with the exception Operation is not valid due to the current state of the object . Here's

dotnet core System.Text.Json unescape unicode string

爷,独闯天下 提交于 2020-04-06 02:55:29
问题 Using JsonSerializer.Serialize(obj); will produce a escaped string, but I want the unescaped version. for example: using System; using System.Text.Json; public class Program { public static void Main() { var a = new A{Name = "你好"}; var s = JsonSerializer.Serialize(a); Console.WriteLine(s); } } class A { public string Name {get; set;} } will produce a string {"Name":"\u4F60\u597D"} but I want {"Name":"你好"} I created a code snippet at https://dotnetfiddle.net/w73vnO please help me. 回答1: You

How do you read a simple value out of some json using System.Text.Json?

柔情痞子 提交于 2020-03-18 12:08:48
问题 I have this json {"id":"48e86841-f62c-42c9-ae20-b54ba8c35d6d"} How do I get the 48e86841-f62c-42c9-ae20-b54ba8c35d6d out of it? All examples I can find show to do something like var o = System.Text.Json.JsonSerializer.Deserialize<some-type>(json); o.id // <- here's the ID! But I don't have a type that fits this definition and I don't want to create one. I've tried deserializing to dynamic but I was unable to get that working. var result = System.Text.Json.JsonSerializer.Deserialize<dynamic>

ASP.NET MVC Core 3.0 API Serialize Enums to String

会有一股神秘感。 提交于 2020-03-18 04:28:07
问题 How to serialize Enum fields to String instead of an Int in ASP.NET MVC Core 3.0? I'm not able to do it the old way. services.AddMvc().AddJsonOptions(opts => { opts.JsonSerializerOptions.Converters.Add(new StringEnumConverter()); }) I'm getting an error: cannot convert from 'Newtonsoft.Json.Converters.StringEnumConverter' to 'System.Text.Json.Serialization.JsonConverter' 回答1: New System.Text.Json serialization ASP.NET MVC Core 3.0 uses built-in JSON serialization. Use System.Text.Json

System.Text.Json: How do I specify a custom name for an enum value?

纵然是瞬间 提交于 2020-02-25 00:53:15
问题 Using the System.Text.Json serializer capabilities in .NET Core, how can I specify a custom value for an enum value, similar to JsonPropertyName ? For example: public enum Example { Trick, Treat, [JsonPropertyName("Trick-Or-Treat")] // Error: Attribute 'JsonPropertyName' is not valid on this declaration type. It is only valid on 'property, indexer' declarations. TrickOrTreat } 回答1: This is not currently supported out of the box in .net-core-3.0 . There is currently an open issue Support for

System.Text.Json: How do I specify a custom name for an enum value?

不想你离开。 提交于 2020-02-25 00:52:08
问题 Using the System.Text.Json serializer capabilities in .NET Core, how can I specify a custom value for an enum value, similar to JsonPropertyName ? For example: public enum Example { Trick, Treat, [JsonPropertyName("Trick-Or-Treat")] // Error: Attribute 'JsonPropertyName' is not valid on this declaration type. It is only valid on 'property, indexer' declarations. TrickOrTreat } 回答1: This is not currently supported out of the box in .net-core-3.0 . There is currently an open issue Support for