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
21
votes
5 answers

How to add property in existing json using System.Text.Json library?

{ "TestData":{ "Year__of__Account":"2019", "Tax___x0025_":"0.06", "Buildings__1":"1000", "Contents__1":"400", "Total_Insurable_Value":"100", …
Kunal Patil
  • 261
  • 1
  • 2
  • 5
20
votes
2 answers

Use System.Text.Json to deserialize properties with private setters

Is there a way to use System.Text.Json.JsonSerializer.Deserialize with object that contains private setters properties, and fill those properties? (like Newtonsoft.Json does)
Gabriel Anton
  • 444
  • 1
  • 6
  • 14
20
votes
4 answers

System.Text.Json.JsonException: The JSON value could not be converted

I'm using Ubuntu and dotnet 3.1, running vscode's c# extension. I need to create a List from a JSON file, my controller will do some calculations with this model List that I will pass to it So, here is my code and the error I'm getting. First, I…
nanquim
  • 1,786
  • 7
  • 32
  • 50
20
votes
6 answers

Asynchonously deserializing a list using System.Text.Json

Lets say that I request a large json file that contains a list of many objects. I don't want them to be in memory all at once, but I would rather read and process them one by one. So I need to turn an async System.IO.Stream stream into an…
Rick de Water
  • 2,388
  • 3
  • 19
  • 37
19
votes
3 answers

Why does System.Text.Json throw a `NotSupportedException` for `System.IntPtr` when I'm not using `System.IntPtr`?

I have a .NET Framework 4.8 / AngularJS app that I'm trying to upgrade to .NET 6 so that I can start experimenting with newer front-end frameworks (like Blazor). The upgrade is going okay, but I consistently see this error when calling one of my API…
Keegan Kozler
  • 361
  • 1
  • 3
  • 9
19
votes
1 answer

How can I parse JSON with comments using System.Text.Json?

I have some JSON that includes comments (even though comments aren't strictly allowed in the JSON spec.) How can I parse this JSON using System.Text.Json? The JSON I have received is as folows: // A person { "Id" : 1 /* Person's ID */, …
dbc
  • 104,963
  • 20
  • 228
  • 340
18
votes
1 answer

How to deserialize an empty string to a null value for all `Nullable` value types using System.Text.Json?

In .Net Core 3.1 and using System.Text.Json library, I'm facing an issue that didn't occur in Newtonsoft library. If I send an empty string in JSON for some properties of type (type in backend) DateTime? or int?, it returns 400 status code with an…
Dabbas
  • 3,112
  • 7
  • 42
  • 75
18
votes
4 answers

In System.Text.Json is it possible to specify custom indentation rules?

Edit: I made an issue at the .Net runtime repo yesterday which was closed to by "layomia" with this message: "Adding extension points like this comes with a performance cost at the lower-level reader and writer and does not present a good balance…
Moritz Schäfer
  • 189
  • 1
  • 7
17
votes
1 answer

Each parameter in the deserialization constructor on type must bind to an object property or field on deserialization

I have the following simple classes : public abstract class GitObject { public Repository Repository { get; set; } public abstract string Serialize(); public abstract void Deserialize(string data); public class Blob : GitObject …
kklaw
  • 452
  • 2
  • 4
  • 14
17
votes
2 answers

What is equivalent in JToken.DeepEquals in System.Text.Json?

I want to migrate my code from Newtonsoft Json.Net to Microsoft standard System.Text.Json. But I could not find an alternative for JToken.DeepEqual Basically the code must compare two JSON in unit test. Reference JSON, and Result JSON. I used the…
György Gulyás
  • 1,290
  • 11
  • 37
17
votes
3 answers

Cannot Load Assemblies For .Net Standard library (System.Text.Json)

I am writing a .Net Standard 2.0 library that will be used by a binary PowerShell module. The library will be basically an API client with a lot of classes for dealing with the JSON responses. Prior to trying to deserialise the strings, I confirmed…
Ash
  • 3,030
  • 3
  • 15
  • 33
17
votes
4 answers

JsonSerializer.Deserialize fails

Consider the code... using System; using System.Text.Json; public class Program { public static void Main() { int id = 9; string str = "{\"id\": " + id + "}"; var u = JsonSerializer.Deserialize(str); …
t.j.
  • 1,227
  • 3
  • 16
  • 30
16
votes
2 answers

Is it possible to deserialize json string into dynamic object using System.Text.Json?

I am using System.Text.Json package to use the serialization and deserialization. I can deserialize a json string into an object when the type is explicitly specified like below. var data = JsonSerializer.Deserialize(jsonString); But…
Jyina
  • 2,530
  • 9
  • 42
  • 80
16
votes
2 answers

System.Text.Json Field Serialization in .NET 5 not shown in Swashbuckle API Definition

Problem I'm using ASP.NET Core with .NET 5 and am using the System.Text.Json serializer to serialize types containing fields (like System.Numerics.Vector3 (X, Y and Z are fields), although any type with fields behaves the same here). I've verified…
blenderfreaky
  • 738
  • 7
  • 26
16
votes
5 answers

How to force System.Text.Json serializer throw exception when property is missing?

Json.NET behaviour could be defined by attributes: either use default or just throw an exception if json payload does not contain required property. Yet System.Text.Json serializer silently does nothing. Having class: public sealed class Foo { …
Pavel Voronin
  • 13,503
  • 7
  • 71
  • 137
1 2
3
64 65