system.text.json

Serialize/Deserialize a class hierarchy with .NET Core System.Text.Json

大兔子大兔子 提交于 2020-07-30 02:56:31
问题 I have a simple class hierarchy that I want to serialize using System.Text.Json. There are 3 classes. The base is Shape . Inherited ones are Box and Circle . I have a plan to use these classes as a tagged union on my frontend app so I just introduced a discriminator property Tag . I wrote a type convertor that supports serialization/deserialization of this hierarchy. What I'm trying to understand - is this a best approach to implement such functionality or not. Indeed the output result of

System.Text.Json and Dynamically Parsing polymorphic objects

这一生的挚爱 提交于 2020-07-28 04:22:51
问题 I don't believe I am wrapping my head around how to properly use JsonConverter for polymorphism in parsing json results. In my scenario, I am targeting Git Policy Configurations in TFS. A policy configuration: "value": [ { "createdBy": { "displayName": "username", "url": "url", "id": "id", "uniqueName": "user", "imageUrl": "url" }, "createdDate": "2020-03-21T18:17:24.3240783Z", "isEnabled": true, "isBlocking": true, "isDeleted": false, "settings": { "minimumApproverCount": 1,

System.Text.Json and Dynamically Parsing polymorphic objects

佐手、 提交于 2020-07-28 04:21:04
问题 I don't believe I am wrapping my head around how to properly use JsonConverter for polymorphism in parsing json results. In my scenario, I am targeting Git Policy Configurations in TFS. A policy configuration: "value": [ { "createdBy": { "displayName": "username", "url": "url", "id": "id", "uniqueName": "user", "imageUrl": "url" }, "createdDate": "2020-03-21T18:17:24.3240783Z", "isEnabled": true, "isBlocking": true, "isDeleted": false, "settings": { "minimumApproverCount": 1,

Can System.Text.Json.JsonSerializer serialize collections on a read-only property?

故事扮演 提交于 2020-06-23 08:01:45
问题 I'm having trouble getting the new System.Text.Json to deserialize collections stored on read-only properties. Consider these classes: public class SomeItem { public string Label { get; set; } } public class SomeObjectWithItems { public string Label { get; set; } // Note this property is read-only but the collection it points to is read/write public ObservableCollection<SomeItem> Items { get; } = new ObservableCollection<SomeItem>(); } Here's the JSON: { "Label": "First Set", "Items": [ {

System.Text.Json: Deserialize JSON with automatic casting

帅比萌擦擦* 提交于 2020-06-23 01:56:51
问题 Using .Net Core 3's new System.Text.Json JsonSerializer, how do you automatically cast types (e.g. int to string and string to int)? For example, this throws an exception because id in JSON is numeric while C#'s Product.Id is expecting a string: public class HomeController : Controller { public IActionResult Index() { var json = @"{""id"":1,""name"":""Foo""}"; var o = JsonSerializer.Deserialize<Product>(json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true, }); return View(); }