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
42
votes
2 answers

JsonConverter equivalent in using System.Text.Json

I'm starting to migrate some code I have from Newtonsoft.Json to System.Text.Json in a .net Core 3.0 app. I migrated the properties from [JsonProperty("id")] to [JsonPropertyName("id")] but I have some properties decorated with the JsonConverter…
Fritjof Berggren
  • 3,178
  • 5
  • 35
  • 57
41
votes
3 answers

How to pretty print using System.Text.Json for unknown object

Using System.Text.Json i can pretty print json using serialization option. var options = new JsonSerializerOptions{ WriteIndented = true }; jsonString = JsonSerializer.Serialize(typeToSerialize, options); However, I have string JSON and don't know…
LP13
  • 30,567
  • 53
  • 217
  • 400
36
votes
3 answers

Parsing a JSON file with .NET core 3.0/System.text.Json

I'm trying to read and parse a large JSON file that cannot fit in memory with the new JSON reader System.Text.Json in .NET Core 3.0. The example code from Microsoft takes a ReadOnlySpan as input public static void…
J. Margarine
  • 397
  • 1
  • 3
  • 8
35
votes
7 answers

Is there a built in way of using snake case as the naming policy for JSON in ASP.NET Core 3?

I managed to get it working using the following code: .AddNewtonsoftJson(options => { options.SerializerSettings.ContractResolver = new DefaultContractResolver { NamingStrategy = new SnakeCaseNamingStrategy() }; }); However this…
numberjak
  • 1,065
  • 4
  • 13
  • 28
29
votes
3 answers

Convert object to System.Text.Json.JsonElement

Let's say I have an object of type: public class MyClass { public string Data { get; set; } } And I need to convert it to System.Text.Json.JsonElement. The only way I found is: var json = JsonSerializer.Serialize(new MyClass { Data = "value"…
rytisk
  • 1,331
  • 2
  • 12
  • 19
29
votes
4 answers

System.Text.Json - Deserialize nested object as string

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 {…
kyrylomyr
  • 12,192
  • 8
  • 52
  • 79
28
votes
2 answers

Deserialize anonymous type with System.Text.Json

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 =…
superstator
  • 3,005
  • 1
  • 33
  • 43
25
votes
5 answers

System.Text.JSON doesn't deserialize what Newtonsoft does

I have a json that the new System.Text.Json.JsonSerializer.Deserialize(json_data) serialize as List with the correct numbers of elements, but then the objects inside have all the values null or 0 Same json with…
SandroRiz
  • 903
  • 4
  • 10
  • 18
25
votes
6 answers

Does the new `System.Text.Json` have a required property attribute?

I've combed through the MS docs but cannot find an attribute equivalent to the NewtonSoft JsonPropertyRequired. What I'm looking for is this: public class Videogame { [JsonProperty(Required = Required.Always)] public string Name { get; set;…
THBBFT
  • 1,161
  • 1
  • 13
  • 29
24
votes
2 answers

How to use default serialization in a custom System.Text.Json JsonConverter?

I am writing a custom System.Text.Json.JsonConverter to upgrade an old data model to a new version. I have overridden Read() and implemented the necessary postprocessing. However, I don't need to do anything custom at all in the Write()…
dbc
  • 104,963
  • 20
  • 228
  • 340
24
votes
3 answers

How to deal with nullable reference types with System.Text.Json?

I have upgraded my project to netcore 3.0 and I am in the middle of refactoring a project to use the new nullable references types feature, but got stuck pretty quickly because of the following issue. Lets say I consume a REST api which returns the…
23
votes
6 answers

dynamic c# ValueKind = Object

As i'm trying to access object values using JsonSerializer.Deserialize using debugger. Here is my result which i'm having below. OtpData = ValueKind = Object : "{ "OTP":"3245234", "UserName":"mohit840", …
user13381500
23
votes
1 answer

Modifying a JSON file using System.Text.Json

I know you can do this easily with Newtonsoft. As I am working with .NET Core 3.0, however, I am trying to use the new methods for interacting with JSON files —i.e., System.Text.Json—and I refuse to believe that what I am trying to do is all that…
Louis Miles
  • 353
  • 1
  • 2
  • 8
23
votes
2 answers

Converting newtonsoft code to System.Text.Json in .net core 3. what's equivalent of JObject.Parse and JsonProperty

I am converting my newtonsoft implementation to new JSON library in .net core 3.0. I have the following code public static bool IsValidJson(string json) { try { JObject.Parse(json); return true; } …
Kamran Shahid
  • 3,954
  • 5
  • 48
  • 93
21
votes
6 answers

Does JsonStringEnumConverter (System.Text.Json) support null values?

I am shifting my code from .NET Core 2.x to .NET Core 3.x (i.e. use the native library System.Text.Json). In doing this, I ran into some issues with how the former Newtonsoft.Json support for nullable enums does not have a clear migration path at…
Svek
  • 12,350
  • 6
  • 38
  • 69
1
2
3
64 65