system.text.json

Is there a simple way to manually serialize/deserialize child objects in a custom converter in System.Text.Json?

与世无争的帅哥 提交于 2020-02-02 11:56:46
问题 NOTE: I am using Microsoft's new System.Text.Json and not Json.NET so make sure answers address this accordingly. Consider these simple POCOs: interface Vehicle {} class Car : Vehicle { string make { get; set; } int numberOfDoors { get; set; } } class Bicycle : Vehicle { int frontGears { get; set; } int backGears { get; set; } } The car can be represented in JSON like this... { "make": "Smart", "numberOfDoors": 2 } and the bicycle can be represented like this... { "frontGears": 3, "backGears"

Serializing Manatee.Json in .NET Core 3

半腔热情 提交于 2020-01-25 10:14:08
问题 Background I want to provide some JsonSchema from my .NET Core 3 application, as well as other objects serialized into JSON. Since Manatee.Json is frequently updated and provides good support for JsonSchema, they are my preferred choice. At the same time, .NET Core 3 provides excellent support for returning objects with magic that transform them into JSON documents. Example: public ActionResult<MyFancyClass> MyFancyAction() { return new MyFancyClass { Property1 = "property 1 content",

System.Text.Json serialization against an anonymous object

你说的曾经没有我的故事 提交于 2020-01-24 18:36:12
问题 I'm working on an ASP .Net Core 3.1 application, porting part of the code from another using 2.2. So far, I'd like to switch from the NewtonSoft JSON serialization library to the new one, System.Text.Json, but I have some trouble. Consider a function to serve a HTTP-GET service with this returning type: [HttpGet("myservice")] public async Task<ActionResult<object>> GetDataAsync( ... Then, the final part could be depicted as follows: var items = new List<IMyInterface>(); int totalCount = ...

Deserialize anonymous type with System.Text.Json

泄露秘密 提交于 2020-01-14 13:55:11
问题 I am updating some apps for .NET Core 3.x, and as part of that I'm trying to move from Json.NET to the new System.Text.Json classes. With Json.NET, I could deserialize an anonymous type like so: var token = JsonConvert.DeserializeAnonymousType(jsonStr, new { token = "" }).token; Is there an equivalent method in the new namespace? 回答1: As pointed out by dbc above, this just isn't possible currently. We looked at JsonDocument as a workaround, but it's also missing things like case insensitivity

System.Text.Json API is there something like IContractResolver

佐手、 提交于 2019-12-24 06:30:51
问题 In the new System.Text.Json; namespace is there something like IContractResolver i am trying to migrate my project away from Newtonsoft. This is one of the classes i am trying to move: public class SelectiveSerializer : DefaultContractResolver { private readonly string[] fields; public SelectiveSerializer(string fields) { var fieldColl = fields.Split(','); this.fields = fieldColl .Select(f => f.ToLower().Trim()) .ToArray(); } protected override JsonProperty CreateProperty(MemberInfo member,

System.Text.Json.JsonElement ToObject workaround

丶灬走出姿态 提交于 2019-12-23 11:05:09
问题 I want to know the equivalent of the ToObject<>() method in Json.NET for System.Text.Json. Using Json.NET you can use any JToken and convert it to a class. EG: var str = ""; // some json string var jObj = JObject.Parse(str); var myClass = jObj["SomeProperty"].ToObject<SomeClass>(); How would we be able to do this with .NET Core 3's new System.Text.Json var str = ""; // some json string var jDoc = JsonDocument.Parse(str); var myClass = jDoc.RootElement.GetProperty("SomeProperty"). <-- now what

Is polymorphic deserialization possible in System.Text.Json?

瘦欲@ 提交于 2019-12-22 04:37:12
问题 I try to migrate from Newtonsoft.Json to System.Text.Json. I want to deserialize abstract class. Newtonsoft.Json has TypeNameHandling for this. Is there any way to deserialize abstract class via System.Text.Json on .net core 3.0? 回答1: Please try this library I wrote as an extension to System.Text.Json to offer polymorphism: https://github.com/dahomey-technologies/Dahomey.Json If the actual type of a reference instance differs from the declared type, the discriminator property will be

Formatting DateTime in ASP.NET Core 3.0 using System.Text.Json

£可爱£侵袭症+ 提交于 2019-12-19 06:04:05
问题 I am migrating a web API from .NET Core 2.2 to 3.0 and want to use the new System.Text.Json . When using Newtonsoft I was able to format DateTime using the code below. How can I accomplish the same? .AddJsonOptions(options => { options.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc; options.SerializerSettings.DateFormatString = "yyyy'-'MM'-'dd'T'HH':'mm':'ssZ"; }); 回答1: Solved with a custom formatter. Thank you Panagiotis for the suggestion. public class

How to use class fields with System.Text.Json.JsonSerializer?

不想你离开。 提交于 2019-12-17 16:57:14
问题 I recently upgraded a solution to be all .NET Core 3 and I have a class that requires the class variables to be fields. This is a problem since the new System.Text.Json.JsonSerializer doesn't support serializing nor deserializing fields but only handles properties instead. Is there any way to ensure that the two final classes in the example below have the same exact values? using System.Text.Json; public class Car { public int Year { get; set; } // does serialize correctly public string Model

Azure Function v2 and system.text.json

旧街凉风 提交于 2019-12-13 12:52:40
问题 I am trying to implement a function that uses .net core 3 (preview 9) as a target framework and uses the new System.text.json namespace. Here is my code: using System; using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.Host; using Microsoft.Extensions.Logging; using System.Text.Json; namespace Backtester { public class Test { public string Author { get; set; } public string Currency { get; set; } } public static class Function { [FunctionName("Function")] public static void Run(