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

System.Text.Json.JsonSerializer.Serialize ignores global config

In a Blazor WASM app under .NET 7 I configure standard Json serializer options like so in Program.cs builder.Services.Configure(options => { options.TypeInfoResolver = new PolymorphicAgentaDtoResolver(); …
0
votes
1 answer

How to return JSON string as an sub-object in a .NET 6 WebApi controller?

I have the following data structure which I want to return as result from a controller: public class DataModel { public string Id { get; private set; } public string Name { get; private set; } public string Description { get; private…
Vlad Radu
  • 337
  • 1
  • 13
0
votes
1 answer

.NET MAUI "A possible object cycle was detected”" json serialize

I've been reading about this error here but I can't solve it since I can't find any cycle in my classes. I have two classes: public partial class Project: ObservableObject { [ObservableProperty] private string name; …
Joao Lima
  • 87
  • 6
0
votes
0 answers

Controlling how Text.Json.JsonSerializer serializes an object that implements a generic collection interface

I have encountered a problem serialising an object to JSON. The object is set up with serialisable fields and properties, but it also implements a generic collection interface: In a simplified form, it essentially looks like this: class MyClass :…
Loophole
  • 333
  • 4
  • 7
0
votes
3 answers

How to assign a list to a read-only/init-only property using initializer syntax without hard-coding it, e.g. in JsonPolymorphismOptions?

I have the following code: public abstract class Base { } public class Foo : Base { } public class Bar : Base { } public static void TestA() { JsonPolymorphismOptions options = new JsonPolymorphismOptions { DerivedTypes = …
Joerg
  • 790
  • 2
  • 10
  • 23
0
votes
4 answers

Make client agnostic to API naming case and format

There is a client app sending requests to some service, maybe even several ones. The service is a 3rd party, not very reliable, and can make changes in the field names often. Client model. class Demo { string SomeName { get; set; } double…
Anonymous
  • 1,823
  • 2
  • 35
  • 74
0
votes
0 answers

Default order JSON.Net

System.Text.Json does not guarantee the serialization order of properties. JSON.Net has the same behavior ? I want to know json.net default serialization order
0
votes
1 answer

Return object with JsonPropertyName annotation from Azure Function

we have the following structure. public class Foo { [JsonPropertyName("x")] public string Prop1 { get; set; } [JsonPropertyName("y")] public string Prop2 { get; set; } [JsonPropertyName("z")] public string Prop3 { get;…
mburm
  • 1,417
  • 2
  • 17
  • 37
0
votes
2 answers

Controller Only Returning Derived Class Fields When Using JsonSerializer.Serialize

I have a controller that returns a list of derived objects , a List of ICollectedEditionToGet to be precise, however when I try to serialize said objects it's only returning the properties that exist on the derived lass. public interface…
user1708468
  • 195
  • 10
0
votes
1 answer

RuntimeBinder exception when using System.Text.Json.Serialize with dynamic type

public string PrettifyJson(string json) { dynamic? parsedJson = JsonSerializer.Deserialize(json); var optionPrettyPrint = new JsonSerializerOptions { WriteIndented = true }; string formattedJson =…
Hottemax
  • 305
  • 1
  • 12
0
votes
0 answers

Convert S3 response file to object using System.Text.Json

I'm trying to deserialize a response stream from AWS S3 (a JSON file) to an object using System.Text.Json. using var file = await _s3.GetObjectAsync(new GetObjectRequest { BucketName = bucketName, Key = key }); var memoryStream = new…
user2382627
0
votes
1 answer

Why is RequiresUnreferencedCodeAttribute making a method unavailable?

I'm trying to call the JsonSerializer.Deserialize override that takes a Utf8JsonReader and JsonSerializerOptions. I'm doing this because it's most convenient for my JsonConverter implementation. To my surprise, even though I'm targeting a runtime…
Jacob
  • 77,566
  • 24
  • 149
  • 228
0
votes
1 answer

How to ignore a property with JsonIgnore attribute in parent class

I use the JsonIgnore feature in the parent class, but the serialized subclass still has this property. public class Parent { [JsonIgnore] public virtual string ParentProperty { get; set; } } public class Child : Parent { public…
Hobo
  • 41
  • 1
  • 5
0
votes
0 answers

Polymorphic type discriminator is not converted to enum during deserialization

I'm using enum field as type discriminator for polymorphic serialization/deserialization and faced strange behavior. A string value that is a type discriminator is not converted to enum and sets the default value of this enum. Here's my C#…
0
votes
2 answers

Deserialize json with dynamic number of objects and keys

I have a json string that I need to deserialize (in a WPF app, using System.Net.Json). A subobject (node) in the tree contains a variable number of propertiers/nodes with variable names. A "minimal" sample looks like this: "intervals":…
Erik Thysell
  • 1,160
  • 1
  • 14
  • 31