Questions tagged [system.text.json]

System.Text.Json is the built-in JSON facilities added in .NET Core 3.0.

Filter by
Sorted by
Tagged with
1 vote
1 answer
89 views

Endless loop during serialization

I need to return an object to the client that contains a field of the VMoney class public class VMoney { public Decimal Value { get; } public VMoney(Decimal value) { Value = value;...
Weazy's user avatar
  • 13
1 vote
2 answers
53 views

Why missing $type when serialize generic with JsonDerivedType?

Here are my codes: public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); Debug.WriteLine(JsonSerializer.Serialize<TestModel>(new ...
Melon NG's user avatar
  • 2,700
3 votes
2 answers
89 views

How can I deserialize generics by System.Text?

Here is the class: public class TestModel<T> { public string Name { get; set; } public T Parameter { get; set; } public DateTime Time { get; set; } } The generic may ...
Melon NG's user avatar
  • 2,700
0 votes
1 answer
43 views

Deserialize Json input to dynamic

I want to deserialize a Json document without knowing anything about its content (can't use a strongly typed .NET class). I was using the following code with Newtonsoft.Json to deserialize to dynamic: ...
Benjamin Talmard's user avatar
0 votes
0 answers
31 views

pythonnet - import .net code into python - Error: Could not load file or assembly - (for nested assembly)

the repo with all the code https://github.com/avivs100/pythonnetTest Environment: Pythonnet version: 3.0.3.0 Python version: 3.11.3 Operating System: win 10 .NET Runtime: 7 Details: I created a ...
aviv shichman's user avatar
1 vote
1 answer
53 views

Use a Blanket policy to serialize enums as strings with snake case

I'm moving some libraries to .net 8 and I'm trying to use the new JsonNamingPolicy.SnakeCaseLower for enums (I have a custom converter that I currently use that uses reflection but I want to drop it). ...
Juan Zamudio's user avatar
3 votes
1 answer
89 views

Setting System.Text.Json's PropertyNamingPolicy per web request

I'm upgrading our codebase from Newtonsoft.Json to System.Text.Json. One of the tricky requirements we have is that we need to set the casing in our Web Api to pascal case or camel case based on the ...
Justin's user avatar
  • 17.8k
0 votes
0 answers
29 views

Configurating STJ serializer options in ASP.NET Core Razor Pages

I have a ASP.NET Core 8 application that uses Razor pages. The endpoints internally use a HttpClient to get data from a downstream API. The downstream API is configured to serialize enums as strings ...
Kralizek's user avatar
  • 2,019
-1 votes
1 answer
77 views

When deserializing JSON, How can I configure the concrete type used for properties declared as IList<T> to be ObservableCollection<T>?

I have some classes that model the structure I expect from my JSON file. For values that are an array in the JSON, the corresponding C# properties are declared to have the type IList<T> for ...
Marc Miller's user avatar
0 votes
0 answers
72 views

Warnings with PublishAot and deserializing json System.Text.Json in .NET 8.0.100

I'm getting multiple warning about deserializing json with System.Text.Json .NET 8.0.100 How would I get rid of the warnings? I do want to note that the current code works, but I want to get rid of ...
weggooie man's user avatar
2 votes
2 answers
82 views

.NET 8 serialization/deserialization of IEnumerable interface

Having following classes: public class Person { public int Age { get; set; } } using System.Collections; using System.Text.Json.Serialization; namespace SerializeCustomEnumerable; public class ...
suchoss's user avatar
  • 3,054
0 votes
2 answers
73 views

Ignore json null value in System.Text.Json Deserialize

model: public class Demo { public int A { get; set; } public string B { get; set; } = "Default value"; } json strJson: { "A" : 1, "B" : null } When I ...
Shire's user avatar
  • 21
0 votes
1 answer
36 views

How to deserialize objects having different keys (names) but identical structure in C# .Net 8 using System.Text.Json.Serialization?

Consider following Json data below, I would like to deserialize object items in "cart" as array/Enumerable -lets say CartItem- since ojects "1345516108" and "1456460669" ...
Onur Buyukcaglar's user avatar
2 votes
1 answer
48 views

Exception when deserializing a file that I have previously serialized using System.Text.Json

In my C# (v9) .Net 6 app, I have a class with the following member variable: private readonly List<Dictionary<ConsoleColor, ConsoleColor>> colours = new () { new Dictionary <...
Plunder Bunny's user avatar
-1 votes
0 answers
43 views

Deserialize wrapper of generic Dto's

I have implemented a wrapper for dto's in the following manner. I am trying to deserialize this wrapper to get the correct Dto's back. I've tried applying everything I know about deserializing but I ...
Zenith's user avatar
  • 146
0 votes
0 answers
149 views

Use System.Text.Json with Unity

I was having trouble finding a JSON serialization/deserialization solution that was compatible with the latest LTS version of Unity (2022.3.9f1), with the issue being that this was specifically needed ...
Will McVay's user avatar
1 vote
1 answer
129 views

How to deserialize java duration in c#?

Assuming that I got a duration from Java: "PT-328H-46M-43.2082074S". How to deserialize it to NodaTime Duration in system.text.json?
NightCity10932's user avatar
-1 votes
1 answer
22 views

How to deserialize an `Instant` without the final Z with NodaTime and `System.Text.Json`?

I need to deserialize a JSON string with .NET's System.Text.Json containing a date time in UTC, but I'm unable to use the NodaTime converter for Instant since the string is missing the final Z (even ...
mcont's user avatar
  • 1,762
2 votes
1 answer
87 views

System.Text.Json: How can I enforce default camelCase JsonNamingPolicy when Serializing data?

I have this annoying problem where the moment I add a custom JSON converter the naming policy is not respected when Deserializing properties. It defaults to PascalCase always no matter what. I can ...
Kounavi's user avatar
  • 1,109
0 votes
0 answers
24 views

NetTopologySuite.IO.GeoJSON deserializing invalid GeoJSON

We are using NetTopologySuite.IO.GeoJSON4STJ to convert GeoJSON used in our front-end using Google Javascript API to data stored in a database. A mapper class is used to handle the incoming string ...
Valuator's user avatar
  • 3,322
0 votes
1 answer
99 views

C# Json Deserialize not working with parameterized constructor

How does JsonSerializer.Deserialize work with a parameterized constructor? In the below example, Foo1 works as expected, but when Foo2 uses a constructor instead of properties, it errors: ...
imMAW's user avatar
  • 173
0 votes
1 answer
78 views

Json deserialization onto readonly fields in C#

I have a readonly struct. The struct, as it is readonly,must have readonly fields. However, I cannot apparently deserialize onto these fields from json. I would much like to keep my struct readonly ...
xxxx's user avatar
  • 59
-1 votes
0 answers
61 views

JsonNode: How to parse NaN, Infinity, -Infinity?

I need to parse extended JSON (JavaScript). How can I do that in JsonNode ? [Fact] void JavaScript() { var str = "[NaN, Infinity, -Infinity]"; var array = JsonNode.Parse(str); } ...
malat's user avatar
  • 12.1k
1 vote
2 answers
84 views

How to ignore Property on JSON deserialization?

My Dto has Id property: public class ADto { public int Id { get; set; } public string Text { get; set; } } The Id is set by EF Core not by Frontend request, so I want to ignore it ...
Yoda's user avatar
  • 17.5k
1 vote
0 answers
54 views

JsonElement to Native C# Object

I have a function which takes in an argument as IDictionary<string, object> parameters. This argument value is passed from a request argument from API. ExecuteQueryRequest.cs public class ...
Hardik Dhuri's user avatar
0 votes
1 answer
156 views

System.Text.Json Serialize an Object

I am trying to send an object via SignalR. It looks like [Serializable] public class MyDataClass { //Some property correctly serialized and deserialized public Int32 Format { get; set; } ...
Foxhunt's user avatar
  • 802
0 votes
1 answer
59 views

Serialize a list of generic class objects using System.Text.Json not serializing generic properties correctly

I have an application that composes algorithms. Each algorithm consists of a sequence of steps. Steps have a very similar structure, only difference being a single property that can be one of many ...
Dido's user avatar
  • 522
1 vote
0 answers
33 views

System.Text.Json.JsonException: The JSON value could not be converted to Enum in .NET 6 [duplicate]

I am facing an issue that my third party API sends me response in string which I convert to enum but API adds space at the end of some values for example instead of "Some value 1" it comes ...
Hakuna's user avatar
  • 297
0 votes
0 answers
52 views

I need JsonElement.GetProperty() to be case insensitive

I have some JSONs in my app that are parsed from strings, using JsonDocument.Parse(). but their properties have inconsistencies in their naming. Some properties are camelCased while some others are ...
Sharif Rezvani's user avatar
0 votes
1 answer
28 views

How to get a proper error message in ASP.NET Core for a polymorphic deserialization when type discriminator is not specified?

I want to get an array of polymorphic classes in a request: { "notifications": [{"type":"Sms", ...}, {"type":"Email", ...} ] } For that I have ...
Ilya Chernomordik's user avatar
1 vote
1 answer
75 views

Serialize and Deserialize generic objects in ASP.NETCore .NET 6 C# App

This is my requirement: A Page object, it has a string property Name and a generic list of Field object. A Field object has the following public string field Name and a public generic field Value. The ...
StackUnderflow's user avatar
0 votes
0 answers
46 views

How to get JsonValueKind from JsonObject?

I have JsonObject that is created like this: JsonObject jsonObj = JsonNode.Parse(inputString).AsObject(); I then get property value from it like this if (jsonObj.TryGetPropertyValue("property1&...
ilya_i's user avatar
  • 333
3 votes
2 answers
69 views

Deserialize Json array in .NET7 project gives error

I'm feeling stupid, because I get an error and cannot figure out why. Error: System.Text.Json.JsonException: 'The JSON value could not be converted to System.Collections.Generic.List`1[Helpspot.CL....
John V's user avatar
  • 133
1 vote
1 answer
127 views

Using System.Text.Json DeserializeAsyncEnumerable to deserialize non-root items

I'd like to process a large JSON response (a large list of measurements) from a webserver in a streaming fashion using JsonSerializer.DeserializeAsyncEnumerable(). Problem is that the array of ...
Erwin van der Valk's user avatar
0 votes
1 answer
117 views

Why System.Text.Json Fails to Deserialize Json to Generic Class

I have two classes and enum: public abstract class Changed { public abstract object OldValue { get; set; } public abstract object NewValue { get; set; } public TrackedPropertyType Type { ...
Fallingsappy's user avatar
-2 votes
1 answer
78 views

Obtain a real value from a ValueKind

I have a function that build a predicate to filter a query from the database based on a Json that it is coming from the request of the APIs. I opened other question about but not I know better the ...
Enrico's user avatar
  • 3,900
1 vote
0 answers
29 views

Get System.Object from System.Text.Json.JsonElement [duplicate]

I am using the var myObject = await JsonSerializer.DeserializeAsync<MyObject>(stream); to deserialize a Json file into my object structure. public class MyObject { public string Name { get; ...
hwcverwe's user avatar
  • 5,307
3 votes
1 answer
152 views

How to serialize an Enum with a custom string name using System.Text.Json [duplicate]

I have some problems when customizing Enum values as a string. public sealed class SerializationTest { [JsonPropertyName("type")] [JsonConverter(typeof(JsonStringEnumConverter))] ...
Lucas Rafaell's user avatar
1 vote
1 answer
66 views

Enum string serialization not working as expected in .net 6.0

I have a production api returning the below json: [ { "StartUtc": "2023-09-02T13:52:00Z", "EndUtc": "2023-09-02T15:19:00Z", "...
user007's user avatar
  • 1,556
0 votes
1 answer
121 views

Azure Function: - System.Text.Json: The input does not contain any JSON tokens

I use Rider to create a new Azure Function by template, TimerTrigger based, almost everything by default, but got "System.Text.Json: The input does not contain any JSON tokens." The code is ...
YIZ's user avatar
  • 1
0 votes
0 answers
58 views

Apply JsonConverter attribute on Refit request method

I am developing a UWP app using the Refit library to make HTTP requests to an API that returns JSON data. I have a custom JsonConverter class NoteDeserializerNet : JsonConverter<List<Note>>...
Gabboxl's user avatar
  • 93
0 votes
1 answer
63 views

How to obtain stream in proper form - System.Text.Json;

I am trying to convert stream into class, but I keep receiving stream as HTML (not json). Cleaning browser cache does not help. While running using Microsoft.AspNetCore.Mvc; using Model; using System....
Maciej Ambroziak's user avatar
0 votes
0 answers
66 views

Is there a local System.Text.Json cache per assembly?

I am experiencing a strange problem with serialization using System.Text.Json in version 6.0.8: serialization time depends on my assembly name. I have a console app (net48) that only contains the ...
Morten Lemvigh's user avatar
0 votes
0 answers
115 views

Using JsonSerializer with MemoryMappedViewStream " '0x00' is invalid after a single JSON value."

I am passing data between processes using Memory Mapped Files. Both processes are .NET 6.0 applications which share a .dll containing the data class. I had been serializing the data using ...
Valuator's user avatar
  • 3,322
0 votes
0 answers
98 views

ENUM properties of Event's class failing to deserialize using latest Microsoft.Graph API version 5.*

I have a working version of application which uses Microsoft.Graph API version 4.37. Now upgrading it to use latest version 5.*. The main updated/refactored code parts are working using dedicated ...
Aidas's user avatar
  • 151
1 vote
0 answers
49 views

How to serialize a simple Enum as Integer instead of String By Default in .NET 6

I am using Refit Client to call APIs in .NET and here it is my refit settings var refitSettings = new RefitSettings( new SystemTextJsonContentSerializer( ...
Hakuna's user avatar
  • 297
-1 votes
1 answer
55 views

net 6.0: dynamically returning child object in response

I have a response class. public class Response { [JsonPropertyName("number")] public int? number { get; set; } [JsonIgnore] public string WithClientInfo { get; set; } ...
user3552264's user avatar
1 vote
1 answer
57 views

Is there a design pattern for creating type safe classes that contain arrays that can be serialised and deserialised with System.Text.JSON?

Consider a slightly contrived example: public class Polynomial { public Polynomial(int maxOrder) { MaxOrder = maxOrder; Coefficients = new PolynomialCoefficient[...
tmren's user avatar
  • 33
0 votes
1 answer
78 views

C# System.Text.Json Deserialize JSON-string with Cyrillic symbols - how can i prevent conversion to escape-string?

I'm trying to deserialize Json-string with cyrillic-symbols, and i get JsonObject with string-properties as escape string: Source JsonString: {"ERROR":"-","ERRORS":{"...
Виктор Кравченко's user avatar
0 votes
0 answers
77 views

Is there a way to create a generic model to write a Dictionary<string, object>

I am working on an application where users can store localizations. The issue I am facing is that the localizations that users can store can be Key, value But also Key, Object and there is no limit to ...
Emirhan Demirci's user avatar

1
2 3 4 5
21