Questions tagged [system.text.json]

The implementation of JSON capabilities within .NET Core 3.0 is provided by the System.Text.Json package.

Is there a way to gracefully deserialize a nested timespan property using System.Text.Json?

I am currently working on deserializing JSON data using System.Text.Json and converters. After examining the raw HTTP response content, it appears that the JSON data is valid. The converter responsible for deserializing the content into the specified obse ...

How can I distinguish between a decimal and a long numeric token when using a custom JsonConverter with Utf8JsonReader?

I am working with a json converter that needs to convert a given property value to either a decimal or a long, depending on the type of value. However, I am facing an issue where I cannot determine if the property value is a decimal or a long since the t ...

Parsing JSON into multiple classes

Within my JSON data are various types of blocks. The simple ones like text deserialize without any issues, so I won't delve into them. The challenge lies with three specific types of blocks: media, list, and quiz: { "blocks": [ { "type": " ...

The deserialization functionality in System.Text.Json's JsonSerializer.Deserialize<T>() may experience difficulties in appropriately parsing

Looking to deserialize this json using System.Text.Json, but encountering difficulties that are unclear. The following result has been obtained: PS: It should be noted that the array containing elements does not have a name, simply follow the link for ref ...

Unpacking confidential data

Recently, I came across this code snippet that caught my attention. It's from a tutorial on custom contracts in C# serialization, which can be found at this link. The code seems to do well when serializing the fields of Brains.Brain, but there seems to be ...

Guidelines on configuring JsonSerializerOption on a request-specific basis

I have developed a .NET 7 web api with an endpoint that fetches user data. public class User { public Guid? Id { get; set; } public String? Name { get; set; } public Int32? Age { get; set; } public String? HairColor { get; set; } } When th ...

Property serialization with polymorphic capabilities

I'm currently working on developing a new Client library that allows users to send serialized data to a service. As part of this process, I have created a class structure as follows: public class Data { public string Prop1 {get; set;} public Su ...

Unraveling nested quoted objects with System.Text.Json in .Net 5

These are the two classes I have: public class RootObj { public int a { get; set; } public SubObj sub { get; set; } } public class SubObj { public int b { get; set; } } The JSON string that needs to be deserialized looks like this: { " ...

What is the best way to retrieve all values for a specific JsonProperty name from a JsonDocument?

Within a free-form JsonDocument, there exists a JsonProperty named internalName. The challenge lies in parsing this data without a predefined JSON schema, as the property can be located at various levels within the JSON structure. How can one retrieve all ...

In C#, using system.text.json to deserialize an object of a "unique" type into an array of a single class

My JSON data contains multiple objects structured like this: "SeasonalInfoBySeasonID": { "aa8e0d2d-d29d-4c03-84f0-b0bda96a9fe1": { "ID": "aa8e0d2d-d29d-4c03-84f0-b0bda96a9fe1", " ...

Retrieve the object with the JsonPropertyName annotation from an Azure Function

Here is the current structure we are working with: public class Foo { [JsonPropertyName("x")] public string Prop1 { get; set; } [JsonPropertyName("y")] public string Prop2 { get; set; } [JsonPropertyName("z&q ...

Is there a way to convert NuGet.NuGetVersion to a class in C# through deserialization?

As a novice programmer working on an application for my job, I recently encountered an issue while trying to deserialize a class. The class in question is named Nuget, and its structure is as follows: public class Nuget { public string? Name { get; set ...

Unleash the power of unescaping Unicode strings with System.Text.Json in dotnet

When using JsonSerializer.Serialize(obj), it produces an escaped string; however, I am in need of the unescaped version. Here is an example: using System; using System.Text.Json; public class Program { public static void Main() { var a = n ...

Transforming a string field into a property object through deserialization

Assuming I have a serialized Json object of type MyClass like this: {"DurationField":"3 Months", *OtherProperties*} My goal is to deserialize this Json into the following class structure: public class MyClass{ *OTHER PROPERTIES* p ...

Retrieve the stored information within JsonConverter.CanConvert()

Is there a way to create a custom JsonConverter that converts a string to a TimeSpan object only if the string follows the format of "hh:mm:ss"? I have explored creating a custom JsonConverter, but I encountered an issue with the CanConvert() method as it ...

Exploring ways to retrieve the parent property name within System.Text.Json.Serialization.JsonConverter

Working with a 3rd Party API that provides data in the following format: { "dynamicFields": { "prop1": "val1", "prop2": "val2", "prop3": "val3" }, // otherFields ... } ...