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

In .NET Core 3.x, System.Text.Json does not serialize fields. From the docs: Fields are not supported in System.Text.Json in .NET Core 3.1. Custom converters can provide this functionality. In .NET 5 and later, public fields can be serialized by setting JsonSerializerOptions.IncludeFields to true or by marking the field to serialize with [JsonInclude]: using System.Text.Json; static … Read more

What is the equivalent of Newtonsoft.Json’s JsonProperty attribute in System.Text.Json?

Just in case, anyone else falls over this. The property is renamed to JsonPropertyName and comes from System.Text.Json.Serialization in the System.Text.Json nuget package. Example: using System.Text.Json.Serialization; public class Example { [JsonPropertyName(“test2”)] public string Test { get; set; } } References: Try the new System.Text.Json APIs JsonProperty.Name Property –> Not yet documented properly…

.Net Core 3.0 JsonSerializer populate existing object

So assuming that Core 3 doesn’t support this out of the box, let’s try to work around this thing. So, what’s our problem? We want a method that overwrites some properties of an existing object with the ones from a json string. So our method will have a signature of: void PopulateObject<T>(T target, string jsonSource) … Read more

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

This is not currently supported out of the box in .net-core-3.0, .net-5, .net-6.0 or .net-7.0. There is currently an issue Support for EnumMemberAttribute in JsonConverterEnum #31081[1] requesting this functionality. In the interim, you will need to create your own JsonConverterFactory that serializes enums with custom value names specified by attributes. If you need to round-trip … Read more

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

Solved with a custom formatter. Thank you Panagiotis for the suggestion. public class DateTimeConverter : JsonConverter<DateTime> { public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { Debug.Assert(typeToConvert == typeof(DateTime)); return DateTime.Parse(reader.GetString()); } public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options) { writer.WriteStringValue(value.ToUniversalTime().ToString(“yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ssZ”)); } } // in the ConfigureServices() services.AddControllers() .AddJsonOptions(options => { … Read more

How to globally set default options for System.Text.Json.JsonSerializer?

You can create an extension method. Here’s an example I use separate methods vs having to build special settings, so that all the settings will be in a single spot and easily reusable. public static class DeserializeExtensions { private static JsonSerializerOptions defaultSerializerSettings = new JsonSerializerOptions(); // set this up how you need to! private static … Read more

dotnet core System.Text.Json unescape unicode string

You need to set the JsonSerializer options not to encode those strings. JsonSerializerOptions jso = new JsonSerializerOptions(); jso.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping; Then you pass this options when you call your Serialize method. var s = JsonSerializer.Serialize(a, jso); Full code: JsonSerializerOptions jso = new JsonSerializerOptions(); jso.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping; var a = new A { Name = “你好” … Read more

ASP.NET MVC Core API Serialize Enums to String

New System.Text.Json serialization ASP.NET MVC Core 3.0 uses built-in JSON serialization. Use System.Text.Json.Serialization.JsonStringEnumConverter (with “Json” prefix): services .AddMvc() // Or .AddControllers(…) .AddJsonOptions(opts => { var enumConverter = new JsonStringEnumConverter(); opts.JsonSerializerOptions.Converters.Add(enumConverter); }) More info here. The documentation can be found here. If you prefer Newtonsoft.Json You can also use “traditional” Newtonsoft.Json serialization: Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson And then: … Read more

Is polymorphic deserialization possible in System.Text.Json?

Is polymorphic deserialization possible in System.Text.Json? The answer is yes and no, depending on what you mean by “possible”. There is no polymorphic deserialization (equivalent to Newtonsoft.Json’s TypeNameHandling) support built-in to System.Text.Json. This is because reading the .NET type name specified as a string within the JSON payload (such as $type metadata property) to create … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)