Questions tagged [system.text.json]

System.Text.Json is the built-in JSON facilities added in .NET Core 3.0.

The System.Text.Json namespace provides high-performance, low-allocating, and standards-compliant capabilities to process JSON, which includes serializing objects to JSON text and deserializing JSON text to objects, with UTF-8 support built-in. It also provides types to read and write JSON text encoded as UTF-8, and to create an in-memory document object model (DOM) for random access of the JSON elements.

Additional details can be found in the how to article.

973 questions
132
votes
7 answers

ASP.NET Core 3.0 System.Text.Json Camel Case Serialization

In ASP.NET Core 3.0 Web API project, how do you specify System.Text.Json serialization options to serialize/deserialize Pascal Case properties to Camel Case and vice versa automatically? Given a model with Pascal Case properties such as: public…
Alexander Staroselsky
  • 37,209
  • 15
  • 79
  • 91
115
votes
4 answers

System.Text.Json.JsonElement ToObject workaround

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. For example: var str = ""; // Some JSON string var jObj = JObject.Parse(str); var myClass =…
Stalfos
  • 1,348
  • 2
  • 9
  • 12
107
votes
16 answers

Is polymorphic deserialization possible in System.Text.Json?

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?
SkyStorm
  • 1,173
  • 2
  • 8
  • 6
92
votes
5 answers

ASP.NET MVC Core API Serialize Enums to String

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…
Andrei
  • 42,814
  • 35
  • 154
  • 218
85
votes
4 answers

dotnet core System.Text.Json unescape unicode string

Using JsonSerializer.Serialize(obj) will produce an 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 = "你好"}; …
Joey
  • 1,233
  • 3
  • 11
  • 18
77
votes
9 answers

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

Instead of this: JsonSerializerOptions options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase // etc. }; var so = JsonSerializer.Deserialize(someJsonString, options); I would like to do something…
Trevor Reid
  • 3,310
  • 4
  • 27
  • 46
69
votes
5 answers

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

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 => { …
D. English
  • 1,671
  • 1
  • 8
  • 7
69
votes
9 answers

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

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 =…
Nick
  • 4,556
  • 3
  • 29
  • 53
65
votes
2 answers

Equivalent of JObject in System.Text.Json

I have DTO class that has a property of type JObject. This DTO class is send/receive over HTTP between multiple services. JObject is used because the ExtractedData does not have predefined properties public class MyDTO { public JObject…
LP13
  • 30,567
  • 53
  • 217
  • 400
61
votes
3 answers

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

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:…
Craig Smitham
  • 3,395
  • 4
  • 31
  • 38
59
votes
10 answers

Ignore property when null using the new Net Core 3.0 Json

When using JSON.Net in ASP.Net Core 2.2 I was able to ignore a property when its value was null when serializing to JSON: [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public DateTime? Created { get; set; } But when using the new…
Miguel Moura
  • 36,732
  • 85
  • 259
  • 481
48
votes
2 answers

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

What is the equivalent of Newtonsoft.Json's JsonProperty attribute in System.Text.Json? Example: using Newtonsoft.Json; public class Example { [JsonProperty("test2")] public string Test { get; set; } } References: Newtonsoft.Json…
FranzHuber23
  • 3,311
  • 5
  • 24
  • 63
48
votes
8 answers

.Net Core 3.0 JsonSerializer populate existing object

I'm preparing a migration from ASP.NET Core 2.2 to 3.0. As I don't use more advanced JSON features (but maybe one as described below), and 3.0 now comes with a built-in namespace/classes for JSON, System.Text.Json, I decided to see if I could drop…
Asons
  • 84,923
  • 12
  • 110
  • 165
46
votes
7 answers

System.Text.Json: Deserialize JSON with automatic casting

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…
Johnny Oshika
  • 54,741
  • 40
  • 181
  • 275
44
votes
3 answers

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

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…
BillHaggerty
  • 6,157
  • 10
  • 35
  • 68
1
2 3
64 65