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

Getting nested properties with System.Text.Json

I am working with System.Text.Json in my project as I am processing large files so also decided to use it for processing GraphQL responses. Due to the nature of GraphQL sometimes I get highly nested responses that are not fixed and don't make sense…
Guerrilla
  • 13,375
  • 31
  • 109
  • 210
16
votes
1 answer

SwaggerUI 5.0.0 ignoring JsonProperty name

I recently upgraded a ASP.NET Core API application to Swashbuckle/Swagger 5.0.0 with ASP.NET Core 3.1.
bytedev
  • 8,252
  • 4
  • 48
  • 56
16
votes
2 answers

How to deserialize stream to object using System.Text.Json APIs

I'm receiving a response from a web api call as a stream and need to deserialize it to a model. This is a generic method, so I can't say which parts of code will use this and what's the response payload. Here's the method: public async Task
Mike
  • 561
  • 1
  • 5
  • 20
15
votes
2 answers

JsonPropertyNameAttribute is not supported record from C#9?

I want to use record with JsonPropertyName attribute, but it caused an error. This is not supported? Any workaround? public record QuoteResponse([JsonPropertyName("quotes")] IReadOnlyCollection? Quotes); Error CS0592 Attribute…
Dmitry
  • 1,095
  • 14
  • 21
15
votes
2 answers

What's the counterpart to JObject.FromObject in System.Text.Json

With Newtonsoft Json you can convert an object to a JObject by calling JObject.FromObject(object). Is there a counterpart in System.Text.Json to get a JsonDocument from an object?
Kirill Rakhman
  • 42,195
  • 18
  • 124
  • 148
15
votes
3 answers

C# .Net Core 3.1 System.Text.Json Ignore empty collection in serialization

Using Newtonsoft we had a custom resolver for ignoring empty collections. Is there any equivalent configuration for the new system.text.json in .Net core 3.1
14
votes
3 answers

System Text JsonSerializer Deserialization of TimeSpan

In researching how to deserialize a TimeSpan using Newtonsoft's JSON.net I came across code in my current project that did not use Json.net. It used System.Text.Json.JsonSerializer and appeared to not fail on the operation of deserializing the…
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
14
votes
4 answers

.NET core 3: Order of serialization for JsonPropertyName (System.Text.Json.Serialization)

While migrating to .NET Core 3 I've switched from Newtonsoft.Json serialization to System.Text.Json.Serialization. Of all the features I want to continue using JsonPropertyName attribute. Newtonsoft version allowed ordering of serialized…
Sergey Nikitin
  • 845
  • 2
  • 13
  • 25
14
votes
3 answers

Can I deserialize Json with private constructor using System.Text.Json?

Wondering if possible to have private constructors and use the new System.Text.Json serializer. public class MyModel { public string Name { get; set; } public string Data { get; set; } private MyModel() { // use me for when…
smiggleworth
  • 534
  • 4
  • 16
13
votes
5 answers

How to find out what type a JsonValue is in System.Text.Json

So when I have a JsonNode I can just ask if it's a JsonObject or a JsonArray and work with those. But when the node is an actual value, how do I know whether it's a string, number or boolean? Of course I could just try and parse the value, but then…
Squirrelkiller
  • 2,575
  • 1
  • 22
  • 41
13
votes
1 answer

Automatic conversion of numbers to bools - migrating from Newtonsoft to System.Text.Json

I accidentally introduced a breaking change in my API by switching to System.Text.Json in my ASP.NET Core app. I had a client that was sending a JSON document and was using numbers 1 or 0 for boolean fields instead of true or false: // What they're…
Phil K
  • 4,939
  • 6
  • 31
  • 56
13
votes
0 answers

Dynamically ignore property on sealed class when serializing JSON with System.Text.Json

Question Can I dynamically ignore a property from a sealed class using System.Text.Json.JsonSerializer? Example Code Example class from another library: public sealed class FrozenClass { // [JsonIgnore] <- cannot apply because I don't own this…
KyleMit
  • 30,350
  • 66
  • 462
  • 664
13
votes
2 answers

How to configure a default JsonSerializerOptions (System.Text.Json) to be used by Azure Function v3?

I have a set of Azure Functions v3 running on .net core 3.1. I have a custom configuration of JsonSerializerOptions that I want to be used automatically by my functions when de/serializing data. Question How can I set up my Azure Functions so that…
Kzryzstof
  • 7,688
  • 10
  • 61
  • 108
13
votes
2 answers

How to handle both a single item and an array for the same property using System.Text.Json?

I am trying to deserialize some JSON that contains a value that is sometimes an array, and sometimes a single item. How can I do this with System.Text.Json and JsonSerializer? (This question is inspired by this question for Json.NET by Robert…
dbc
  • 104,963
  • 20
  • 228
  • 340
13
votes
0 answers

Is there a System.Text.Json's substitute for Json.NET's JsonProperty(Order)?

Since System.Text.Json is now the JSON lib for .NET Core 3.0, is there an attribute/param matching Json.NET's JsonProperty(Order)? I browsed through the classes extending System.Text.Json.Serialization.JsonAttribute, but none seem to serve this…
user719662