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
0
votes
0 answers

Using JsonConverter with Single Object and a List of Object

I am trying to implement a JsonConverter that will work when deserializing a single object as well as when that object is in a list. It works fine when using with a single object but not when the Json contains a array of the objects. Here is the…
0
votes
3 answers

Deserializing a data member that could be int or string c#

I've created an API endpoint that is receiving a List of objects. The 'AttributeValue' of the objects could be either a string or an int. Here is a generic version of my code: public class b { public List myList {get; set;} = new…
0
votes
2 answers

Avoid re-serializing of a json string

Is there a way to selectively exclude a field from re-serialization, if it already contains a valid json string? Here is what I am attempting. First, I define a class of the result row I recieve from my database. public class objectToSerialize { …
roboblocky
  • 81
  • 5
0
votes
2 answers

How do I avoid writing `$type` with System.Text.Json?

How do I avoid exposing a type discriminator when serializing a class that has opted into polymorphic deserialization? I'm using System.Text.Json's JsonDerivedType attributes to do polymorphic deserialization. So I need to specify typeDiscriminator…
Jason Weinzierl
  • 342
  • 1
  • 6
  • 12
0
votes
1 answer

How to correctly Serialize Dictionary using [JsonExtensionData] with JsonNamingPolicy.CamelCase?

I need to use JsonNamingPolicy.CamelCase to serialize my object. But when I use Dictionary with [JsonExtensionData] attribute, it seems something not work correctly. Example Code. public class Test { public Dictionary Labels {…
Changemyminds
  • 1,147
  • 9
  • 25
0
votes
2 answers

System.Text.Json custom null values

I'm using an api endpoint and they are returning invalid responses in some of the fields instead of null values for decimals. here is an example { "name": "MyName", "decimalOne": "0.01", "decimalTwo": "None", "decimalThree": "-" } I would…
kewur
  • 421
  • 5
  • 15
0
votes
2 answers

How to generate JsonPropertyName dynamically?

I have the below Model class, using System.ComponentModel.DataAnnotations; using System.Text.Json.Serialization; public class FormField { [Required] [JsonPropertyName("STD_USERTYPEID")] public string UserTypeId { get; set; } …
tRuEsAtM
  • 3,517
  • 6
  • 43
  • 83
0
votes
0 answers

The JSON value could not be converted to model in .NET Core

I have these model classes: public class TransactionInfo { public int request_id { get; set; } public DateTime request_date { get; set; } public double request_amount { get; set; } public int request_count { get; set;…
Ehsan Akbar
  • 6,977
  • 19
  • 96
  • 180
0
votes
0 answers

JsonTextWriter equivalent system.text from stream writer

I have newtonsoft json web api project that try to migrate system.text but I cannot figure out how to convert the code below. public static void SerializeJsonIntoStream(object value, Stream stream) { using (var sw = new…
datnet
  • 1
0
votes
2 answers

System.Text.Json Custom Deserialization for Dictionary>>

I need to have the System.Text.Json.JsonSerializer be able to deserialize this nested Dictionary type structure, but I'm getting the following error: Unable to cast object of type 'System.Text.Json.JsonElement' to type 'System.String'. I have the…
Charles
  • 127
  • 6
0
votes
0 answers

Unable to serialize custom attribute in System.Text.Json

There is a custom attribute I created that I prescribe to classes for the markup extension. I used to use the serialization mechanism through Newtonsoft.Json and I want to switch to Text.Json, but I ran into a problem that it throws exceptions when…
Magals
  • 23
  • 5
0
votes
3 answers

How to add property in existing json

I receive a JSON file like this: { "Name": "testeName", "type": "unique", "TestData": [{ "price": 2.3 }] } I want to add a new property ("type":"unique") to each object in TestData to be like this in the end: { "Name":…
Sacamoto
  • 107
  • 1
  • 8
0
votes
1 answer

JSON Deserializer issue in C#

I'm a newbie having my first go at importing data from a json file to a c# application. In this case, I'm making an app to organise and manage recipes for a crafting videogame I'm playing. I have a json file with my recipe info in it; { …
0
votes
1 answer

System.Text.Json serialize array of type to a json object with properties

Using System.Text.Json, how can I serialize (and deserialize) an array of n elements (classes), to an object of n children, where the element name is one of the properties of the object? For example, by default the following classes public class…
JobaDiniz
  • 862
  • 1
  • 14
  • 32
0
votes
0 answers

How to hide a property in a C# class from appearing in the JSON response body of a method that returns IActionResult?

I have defined a ProblemDetails class. I construct an object of this class and return from my Http triggered Azure function upon error. I have defined a convenience property HasDetail to tell if the object has any detail meaning if the object is…
blogs4t
  • 2,329
  • 5
  • 20
  • 33