1,278 questions
Advice
0
votes
2
replies
66
views
System.Text.Json Polymorphic Deserialization not binding Type Discriminator property
I'm trying to do a polymorphic deserialization using System.Text.Json in net-8.0. Here is my schema.
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
[JsonDerivedType(typeof(Manager)...
-3
votes
3
answers
157
views
How to fix "Could not load file or assembly 'System.Threading.Tasks.Extensions`" exception when using System.Text.Json in .NET Framework?
I am trying to deserialize a JSON string in C#:
JsonObject json = JsonSerializer.Deserialize<JsonObject>
("{\"columnsperrow\":\"1\",\"columnwidths\":[400.0]...
2
votes
1
answer
73
views
Ignore boolean property = true in JSON serialization
I have a record with a bool property that has default value = true:
private record MyRecord()
{
public bool BoolProperty { get; init; } = true;
}
I want to ignore the "BoolProperty":...
0
votes
2
answers
108
views
Is it possible to use generic argument of a type for its attribute parameter (JsonConverter)?
Use case: I want to write a JsonConverter for a generic type like this, but I cannot apply it to the type itself:
public class EditingModelConverter<T> : JsonConverter<EditingModel<T>&...
6
votes
1
answer
295
views
System.Text.Json is not handling Quaternions - is there some Json settings we need to set/support?
We are having some issues deserialising classes with Quaternion properties.
We are seeing very odd behaviour.
The Quaternions deserialise correctly (we can do a custom JsonConverter to confirm the ...
1
vote
0
answers
69
views
System.Text.Json deserialization not deserializing the Json string [duplicate]
We have a class as shown below:
public class KeyVal<Key, Val>
{
public Key Id { get; set; }
public Val Text { get; set; }
public KeyVal() { }
public KeyVal(Key key, Val ...
1
vote
1
answer
101
views
Polymorphic deserialize JSON using DI container to create instances
Consider these model classes:
[JsonPolymorphic(TypeDiscriminatorPropertyName = "$type")]
[JsonDerivedType(typeof(A), typeDiscriminator: nameof(A))]
[JsonDerivedType(typeof(B), ...
1
vote
1
answer
236
views
Adding modifiers to System.Text.Json serialization when using source generation
I'm currently using the following code to set up a System.Text.Json JsonSerializerOptions object that alphabetizes properties upon serialization:
public static readonly JsonSerializerOptions ...
0
votes
0
answers
57
views
How to globally reject null for non-nullable reference types in ASP.NET Core 9 Web API with System.Text.Json?
Background
I have an ASP.NET Core 9 Web API project with [nullable reference types] enabled (<Nullable>enable</Nullable>). In my request DTOs, I declare certain properties as non‑nullable, ...
0
votes
0
answers
70
views
ASP.NET Web API - Model binder validation property name
I have a basic ASP.NET Web API project. When I send a request, the serialization is done using System.Text.Json. However, when I send a non-valid json, for example text instead of number, it returns a ...
2
votes
0
answers
49
views
How do you limit indentation depth when serializing with System.Text.Json?
I am duplicating this question, except applying it to System.Text.Json: How do you limit indentation depth when serializing with Newtonsoft.Json.
When serializing an object using System.Text.Json, is ...
0
votes
1
answer
151
views
System.Text.Json.JsonSerializer.Deserialize is not deserializing object correctly [duplicate]
I am trying to use System.Text.Json library for performance improvement. When I am trying to convert it, System.Text.Json is not able to deserialize the object but Newtonsoft.Json able to do so.
How ...
-5
votes
1
answer
150
views
How to enumerate a JsonElement array backwards?
I have a System.Text.JsonElement with about 100 elements inside. I know of EnumerateArray(), but is there a way to do this backwards, meaning: Start at the last element of the list and enumerate until ...
1
vote
1
answer
178
views
The converter handles Int32 but is being asked to convert Nullable<Int32>
I'm implementing a custom JsonConverter that handles nullable types. Specifically, it turns empty strings and empty arrays into null.
I've managed to piece together this: (with help from here)
public ...
2
votes
3
answers
162
views
Why does an inherited class not get JSON serialized correctly?
I want to create a JsonSerializable class, that I can inherit from. See this code:
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
public interface IJson
{
string ...
0
votes
1
answer
89
views
How to get JsonPropertyName string and concatenate with others in dot.notation? (C# and Salesforce related)
I have built a C# library to make queries on Salesforce using Web Calls.
This is an example of implementation:
var myQuery = new SalesForceQueryBuilder<MyCoustomUnicornEntity>()
...
0
votes
1
answer
70
views
How to make Deserialization work with a private field?
I'm wanting to only expose a list as ReadOnly but it's, but while trying to accomodate that for Deserialization, it's not currently working.
Firstly, to confirm Serialization:
{
"StartTime"...
1
vote
1
answer
91
views
Deserializing JSON text to an object in C#
I have the following class:
public class DeviceInfo
{
public string? ip { get; set; }
public string? deviceKey { get; set; }
public long time { get; set; }
public string? version { get;...
1
vote
1
answer
1k
views
I can't bypass the System.ArgumentException: '.NET number values such as positive and negative infinity cannot be written as valid JSON."
I am getting the following exception when I am trying to serialize a double that has the value NaN, via JsonConverter
The exception is:
System.ArgumentException: '.NET number values such as positive ...
4
votes
2
answers
491
views
With System.Text.Json, is there a way to EXCLUDE properties from a BASE class from serialization?
For example, I have a class that derives from ObservableValidator and my resulting serialization then includes HasErrors. I want to only include my derived class's properties and don't have access to ...
1
vote
1
answer
265
views
Exception about missing TypeInfoResolver while using BenchmarkDotNet to perform a JSON source generator serialization
I am getting an exception:
> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.InvalidOperationException: TypeInfoResolver '...
0
votes
1
answer
54
views
Serialising/deserialising class with a abstract base loses value with Newtonsoft and System.Text.Json
I'm trying to serialise and deserialise a servicebus event class, my event inherits from an abstract base with a generic parameter for the payload, and my base class implements an interface.
No matter ...
0
votes
1
answer
77
views
.NET 8 HttpClient-Return Nullable Int with GetFromJsonAsync
Is is possible to return a nullable int with GetFromJsonAsync? I'm getting a System.Text.Json.JsonException error:
The input does not contain any JSON tokens
I have a simple API endpoint that ...
0
votes
0
answers
132
views
JsonDerivedType - rename type discriminator
I have classes with polymorphism active e.g.
[JsonDerivedType(typeof(DerivedClass1), "derivedClass1")]
[JsonDerivedType(typeof(Foo), "foo")]
public abstract class BaseClass
{
}
...
0
votes
0
answers
48
views
System.InvalidOperationsException when attempting deserialization of JSON to C# List [duplicate]
I have a class
public class Record
{
public string Name { get; set; } // Flat owner
public int Size { get; set; } // Flat size
public int Enterance { get; set; } /...