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
1 answer

How to use JsonConstructor Attribute in System.Text.Json

I am using the Json namespace provided by .NET, not the Newtonsoft one. I have a piece of code: string text; text = File.ReadAllText(EntityDirectory + @"\Json\AbilityTemplates.json"); foreach (AbilityTemplate…
martinrhan
  • 362
  • 1
  • 18
0
votes
1 answer

Converting inconsistent json values using System.Text.Json

The service I'm working with returns an empty array instead of null for objects. Which causes errors during deserialization. System.Text.Json.JsonException : The JSON value could not be converted to Models.Error. Path: $.errors | LineNumber: 8 |…
Shleemypants
  • 2,081
  • 2
  • 14
  • 21
0
votes
3 answers

serialization of enum results in int

When i do a GET request to retrieve my list of Persons the enums(Title) get converted into integers: class Person { public Title PersonTitle{ get; set;} public string Name { get; set;} } enum Title { STUDENT, TEACHER, DIRECTOR } Let's…
schweppes0x
  • 182
  • 1
  • 13
0
votes
1 answer

Json deserialize object array with dynamic property names

I want to deserialize a json object in c# using JsonSerializer.Deserialize of System.Text.Json. The json looks like this: { "id":10, "authorization_ids":[ ], "karma_user_ids":[ 2 ], "group_ids":{ "2":[ …
B4DschK4Pp
  • 165
  • 1
  • 10
0
votes
0 answers

HttpClient and System.Text.Json.JsonSerializer and PHP different character size when serializing special characters

I have a hard time trying to customize the output of HttpClient and JsonSerializer when I send (or serialize) object with special characters. I get different size characters in the output. Test case if very simple: var test = new Test { A =…
Misiu
  • 4,738
  • 21
  • 94
  • 198
0
votes
3 answers

How do I deserialize a nested JSON object which is a string in System.Text.Json?

I'm deserializing JSON in a ASP.NET 5 MVC application using: var tulemus = JsonSerializer.Deserialize(apiResponse, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); public class EstoJarelMaksTulemnus { …
Andrus
  • 26,339
  • 60
  • 204
  • 378
0
votes
3 answers

.NET custom Json converter for list or single item

my goal is to use System.text.Json to parse a response from the rest api. The response returns one or more items - but if only one item is returned, there are no brackets [] surrounding it (which makes it hard to parse). The deserialization: // i…
Manuel
  • 188
  • 1
  • 12
0
votes
0 answers

Json, Intellisense and Anonymous types

I have this json object that I can access the elements/nodes using intellisense from within the method but I can't access them outside of it. How do you still make the intellisense work when typing it outside the method? I'm using net core…
Sol
  • 53
  • 7
0
votes
2 answers

Custom deserialization with System.Text.Json - Grouping fields into object

Using System.Text.Json and .NET Core 3.1, how can I deserialize the following JSON for a membership: { "id": 123, "firstName": "James", "lastName": "Smith", "group": "Premium" "state": "Active" } Classes: public class…
alhazen
  • 1,907
  • 3
  • 22
  • 43
0
votes
0 answers

The type ReadOnlyMemory<> and ReadOnlySequence<> is defined in an assembly that is not referenced

I have a C# Desktop application I am developing which is meant to read text from a file and convert it into a JsonDocument. I have installed and referenced the System.Text.Json library developed by Microsoft in order to read from the JSON file. I am…
Manny265
  • 1,709
  • 4
  • 23
  • 42
0
votes
2 answers

TryGetInt32 throws System.InvalidOperationException

Call me crazy, but I was under the impression that there was a convention of try which meant give it a go, but if you can't then get back to me and let me know that it was a "no-go". I have recently started a new project and I have decided to use…
spovelec
  • 369
  • 1
  • 4
  • 20
0
votes
1 answer

How to deserialize asynchronously using System.Text.Json library?

I have a json that I am deserializing by using NewtonSoftJson Json library as shown below: public async Task InvokeAsync(HttpContext httpContext, ISchema schema) { ... var request = Deserialize(httpContext.Request.Body); …
AndyP
  • 527
  • 1
  • 14
  • 36
0
votes
0 answers

C# accept null or string via REST

I have an API that accepts a simple class public class MyPayload { public string Id {get;set;} public JsonElement Payload {get;set;} } The Id of the payload tells the backend what class to create and the json Payload is the data to pass along. We…
Jeff Patton
  • 551
  • 4
  • 15
0
votes
1 answer

How to Serialize an Object to Json in F# excluding defaults and Keeping Enum Names

I thought this would have been easy but I am having issues ticking all the boxes that I need in this. I need to Serialize an object to Json Ignore any properties not set Use the ENum names instead of integer values I have generated all the models…
Martin Thompson
  • 3,415
  • 10
  • 38
  • 62
0
votes
0 answers

Attribute is not showing on swagger request after moving from Json.Net to System.Text.Json

I am trying to move from Json.Net to system.Text.Json library in .Net core 3.1. I notice an odd behavior, that swagger doesn't show the attribute that have [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] on the request…