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

.Net6 Enum value to JSON value mapping

I've troubles deserializing JSON values to specific enum values with .NET6 standard JSON serializer. I hoped that EnumMember attribute will work, but it doesn't. Here's some sample code: using System; using System.Runtime.Serialization; using…
grasbueschel
  • 879
  • 2
  • 8
  • 24
0
votes
0 answers

Serializing and deserializing property type of "object" using json. Receiving invalid cast exception

I am switching over my object serialization in my app because it said it was depreciated (https://docs.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide). I am now attempting to use System.Text.Json API's as described…
Willy
  • 137
  • 13
0
votes
1 answer

JsonSerializerOptions config to not encode + in a string

Referring to this answer 2, how could I create an encoder to allow/ not encode the + character in a string? For background, this is for a phone number field of type string where numbers can be specified in an international format. This phone number…
imsan
  • 359
  • 4
  • 17
0
votes
0 answers

Is it possible to deserialise either Json.Number or Json.String to a string without using a custom Converter?

I am writing a service to consume JSON that I have no control over. One property in this JSON can either be a number or a string, so input can either look like: {"event_type":"status","value":"ok"} or {"event_type":"status","value":-253} I have a…
Dutts
  • 5,781
  • 3
  • 39
  • 61
0
votes
2 answers

C# custom json serialization converter to set all default value of an object to null

I have a nested object that looks like: public record Options { public BatterySettings BatterySettings { get; init; } = new(); public LogSettings LogSettings { get; init; } = new(); } public record LogSettings { public string…
K. Vu
  • 95
  • 1
  • 8
0
votes
1 answer

Item[String] on System.Text.Json.Nodes.JsonNode: The node must be of type 'JsonObject'

Experimenting with System.Text.Json, and not able to cleanly express chain of properties with optional early temination. I want something like this ((JsonNode)node)?["a"]?["b"]?["c"] Note, I'm not looking for workarounds (I can write some wrappers,…
Alexey Yeltsov
  • 333
  • 2
  • 13
0
votes
2 answers

JsonSerializer.Deserialize: Deserialize object to its actual type

I'm deserializing a JSON list to object[] and expectedly get an array of object. I'd like however to deserialize to more specific types. Is there a way to do that, possibly with supplying the exact type on serialization? Unfortunatly I'm not able to…
0
votes
3 answers

How to store Multiple derived class in same list?

I have these classes public class SubMenuItem : SubMenuVariant { public string SubMenuTitle { get; set; } public LinkFieldType Link { get; set; } public List SubItems { get; set; } } public class…
I am not Fat
  • 283
  • 11
  • 36
0
votes
4 answers

Deserialize complex json into an object

I have a strange json, which: I can not change it, it is from a third party It can have up to 75 properties A simple example of the json: { "bookingcode":["ABC","DEF", "GHJ", "TEST"], "referencenumber":[123, 456] …
0
votes
1 answer

Include only properties that are instantiated when serializing to Json

I have a class that I referenced from an internal nuget package public class Person { public string Name { get; set;} public int Age{ get; set;} } and I am using System.Text.Json to serialize the instantiated message. When I initialized…
Joseph
  • 502
  • 4
  • 15
0
votes
1 answer

Azure Durable Functions & Json dynamic array

I am working with an Azure Durable Function and I am having some trouble with System.Text.Json In a real scenario I will have a Activity Function which will call an API Endpoint and will get some Json result, which will have an Array with multiple…
0
votes
1 answer

Why is Utf8JsonReader.TokenType required with JsonConvert, but not JsonConvert

In order to be able to deserialize the following JSON: [ { "name": "Luke Skywalker", "height": "172", "mass": "77", "birth_year": "19BBY", "gender": "male" } ] into the following record: record class…
Alexandre Bell
  • 3,141
  • 3
  • 30
  • 43
0
votes
1 answer

How to create a JSON stream of UTF8 bytes in C#

I´m trying to write Unit Tests for an extension method of stream which internally uses return await JsonSerializer .DeserializeAsync(@this, type, options ?? JsonFlatOptions) .ConfigureAwait(false); where @this would be my…
0
votes
0 answers

C# RestSharp API returns data with no Headers

I am struggling with converting JSON (obtained from a Rest API) into a C# Class. The reason that I am having difficulty is because the JSON data being returned does not appear to have any headers, which directly plays at odds with the generated C#…
mherr
  • 348
  • 1
  • 7
  • 25
0
votes
1 answer

Design Pattern to combine runtime data with data loaded from a JSON

I have a data class: public Data { Foo MyFoo Bar MyBar } Foo is something that needs to a lot of changes and cleaning up to be useful. So I have an interface public IFooTransformation { Foo Transform(Foo foo) } Users create lists of…