All Questions

Tagged with
Filter by
Sorted by
Tagged with
122 votes
6 answers
168k views

How to set json serializer settings in asp.net core 3?

json serializer settings for legacy asp.net core applications were set by adding AddMvc().AddJsonOptions(), but I don't use AddMvc() in asp.net core 3. So how can I set global json serialization ...
Alex Zaitsev's user avatar
  • 2,626
99 votes
4 answers
57k views

dotnet core System.Text.Json unescape unicode string

Using JsonSerializer.Serialize(obj) will produce an escaped string, but I want the unescaped version. For example: using System; using System.Text.Json; public class Program { public static void ...
Joey's user avatar
  • 1,399
94 votes
15 answers
72k views

.NET Core: Remove null fields from API JSON response

On a global level in .NET Core 1.0 (all API responses), how can I configure Startup.cs so that null fields are removed/ignored in JSON responses? Using Newtonsoft.Json, you can apply the following ...
dotNetkow's user avatar
  • 5,233
85 votes
12 answers
69k views

How to globally set default options for System.Text.Json.JsonSerializer?

Instead of this: JsonSerializerOptions options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase // etc. }; var so = JsonSerializer.Deserialize<SomeObject>(...
Trevor Reid's user avatar
  • 3,464
73 votes
17 answers
127k views

ASP.NET Core appsettings.json update in code

I am currently working on project using asp.net core v1.1, and in my appsettings.json I have: "AppSettings": { "AzureConnectionKey": "***", "AzureContainerName": "**", "NumberOfTicks": ...
user avatar
50 votes
1 answer
27k views

What is difference between ObjectResult and JsonResult

There are two classes in Microsoft.AspNetCore.Mvc namespace: ObjectResult and JsonResult. Both convert the returned object in the JSON format. What is difference between them and what is the ...
hcp's user avatar
  • 3,338
47 votes
1 answer
37k views

Bind an IConfigurationSection to a complex object without ASP.NET Core

I have a .NET Core console application and want to read the appsettings.json and parse a section as List<Param> (without Dependency Injection or ASP.NET Core). I already tried How do I bind a ...
Maximilian Ast's user avatar
46 votes
2 answers
53k views

JsonConverter equivalent in using System.Text.Json

I'm starting to migrate some code I have from Newtonsoft.Json to System.Text.Json in a .net Core 3.0 app. I migrated the properties from [JsonProperty("id")] to [JsonPropertyName("id")] but I ...
Fernando Urkijo's user avatar
39 votes
1 answer
26k views

System.Text.Json.JsonSerializer.Serialize returns empty Json object "{}" [duplicate]

Environment: Visual Studio 2019 16.3.8, .NET 3.0.100, .NET Core 3.0 unit test. All 3 calls below to System.Text.Json.JsonSerializer.Serialize return empty objects: "{}" I must be doing something ...
MikeZ's user avatar
  • 1,225
39 votes
4 answers
46k views

Using .gitignore file to hide appsettings.json does not actually hide it

I have a C# MVC .Net Core application I'm building, the connection string is in a file called appsettings.json so what I want to do is simply exclude this from my git repository. I have added the ...
CrazyLegs's user avatar
  • 568
34 votes
5 answers
22k views

Loading startup configuration from JSON located in memory

I am researching about how to load the startup config in .NET Core. I notice there are different ways to do that, I have seen XML, JSON, init file, and from a Dictionary located in memory (I will go ...
mra's user avatar
  • 359
33 votes
4 answers
38k views

System.Text.Json - Deserialize nested object as string

I'm trying to use the System.Text.Json.JsonSerializer to deserialize the model partially, so one of the properties is read as string that contains the original JSON. public class SomeModel { ...
kyrylomyr's user avatar
  • 12.4k
30 votes
3 answers
20k views

Deserialize anonymous type with System.Text.Json

I am updating some apps for .NET Core 3.x, and as part of that I'm trying to move from Json.NET to the new System.Text.Json classes. With Json.NET, I could deserialize an anonymous type like so: var ...
superstator's user avatar
  • 3,124
26 votes
1 answer
16k views

How does reloadOnChange of Microsoft.Extensions.Configuration work for appsettings.json

In two projects (a .NET Core Web API and a .NET Core WindowsService) I am using appsettings.json for the configuration. var configuration = new ConfigurationBuilder() .SetBasePath(System.IO....
monty's user avatar
  • 8,265
23 votes
0 answers
65k views

Convert JToken To JObject [duplicate]

I'm trying to create an OAuth Handler in .Net Core, The api I'm using wraps the user data response in an property called data, But the OAuthTicket Context Expects a JObject and Not A JToken var ...
johnny 5's user avatar
  • 20.4k
21 votes
2 answers
26k views

How to deserialize stream to object using System.Text.Json APIs

I'm receiving a response from a web api call as a stream and need to deserialize it to a model. This is a generic method, so I can't say which parts of code will use this and what's the response ...
Mike's user avatar
  • 621
20 votes
5 answers
14k views

How to exclude a property from being serialized in System.Text.Json.JsonSerializer.Serialize() using a JsonConverter

I want to be able to exclude a property when serializing using System.Text.Json.JsonSerializer. I don't want to use a JsonIgnore attribute everywhere I want to do this. I would like to be able to ...
Tareq Imbasher's user avatar
19 votes
5 answers
28k views

How to serialize a dynamic object to a JSON string in dotnet core?

I am passing a JSON payload to an API Controller, and one of the fields is dynamic because the field needs to be passed again as a JSON string to another API. The dotnet core 3.1 middle layer shouldn'...
Todd Worden's user avatar
18 votes
5 answers
21k views

Validate if string is valid json (fastest way possible) in .NET Core 3.0

I know i can use an external library(newtonsoft) with a try/catch to check if a string is valid json structure. I do not want to deserialize to an object (since the json can be one or many properties)...
Zoinky's user avatar
  • 4,679
17 votes
6 answers
16k views

Invalid value for key 'authentication'

I have a .NET Core 3.0 app where am trying to connect to a Azure SQL database using EF Core and Active directory integrated authentication. I have verified that I have access to this database from my ...
Fike Rehman's user avatar
16 votes
7 answers
27k views

Reading a JSON object from appsettings.json

TL;DR: How do I read a complex JSON object from appsettings.json? I have a .NET Core 2.x application with multiple types of configuration values. appsettings.json looks like the snippet below and I ...
Babak Naffas's user avatar
  • 12.5k
16 votes
5 answers
11k views

How to force System.Text.Json serializer throw exception when property is missing?

Json.NET behaviour could be defined by attributes: either use default or just throw an exception if json payload does not contain required property. Yet System.Text.Json serializer silently does ...
Pavel Voronin's user avatar
14 votes
3 answers
11k views

System Text JsonSerializer Deserialization of TimeSpan

In researching how to deserialize a TimeSpan using Newtonsoft's JSON.net I came across code in my current project that did not use Json.net. It used System.Text.Json.JsonSerializer and appeared to not ...
ΩmegaMan's user avatar
  • 30.4k
14 votes
1 answer
14k views

Custom JSON serializer for optional property with System.Text.Json

I'm trying to implement a JSON serialization mechanism which handles both null and missing JSON values, to be able to perform partial updates when needed (so that it does not touch the field in the ...
Maxime Rossini's user avatar
14 votes
3 answers
15k views

appsettings.json in ASP.net Core 2.0 Preview configuration GetSection null

I was trying to call GetSection from injected configuration in the Startup.cs. The Value was null, while indexer to a concrete section value returns non-null value. It seems to me a bug behind the ...
rychlmoj's user avatar
  • 445
13 votes
6 answers
26k views

Post Multiple Parameters Without DTO Class

I have an action on my web project which calls to an API [HttpPost] public async Task<IActionResult> ExpireSurvey(int id) { var token = await HttpContext.GetTokenAsync("...
egmfrs's user avatar
  • 1,327
13 votes
3 answers
39k views

How to manually parse a JSON string in net-core 2.0

I have a json string with the following structure { "resource": "user", "method": "create", "fields": { "name": "John", "surname: "Smith", "email": "[email protected]"...
opensas's user avatar
  • 61.7k
13 votes
1 answer
14k views

How to ignore false values with System.Text.Json

I'm migrating from Newtonsoft.Json to System.Text.Json in my .NET Core 3.0 application. I'm trying to ignore false values. In System.Text.Json I found the option to ignore null values: ...
stevo's user avatar
  • 2,244
13 votes
1 answer
4k views

How can I hide response code 200 with Swashbuckle.AspNetCore?

Ciao, I'm working on a asp.net web api core (target framework .NET Core 2.1). I'm documenting my API using Swagger specifications. I chose to use Swashbuckle.AspNetCore library. I have one simple ...
ilMattion's user avatar
  • 1,921
13 votes
0 answers
4k views

Dynamically ignore property on sealed class when serializing JSON with System.Text.Json [duplicate]

Question Can I dynamically ignore a property from a sealed class using System.Text.Json.JsonSerializer? Example Code Example class from another library: public sealed class FrozenClass { // [...
KyleMit's user avatar
  • 34.2k
12 votes
2 answers
3k views

ASP.NET Core 5 returning JSON adding $id and $values properties

I'm using ASP.NET Core 5. As below, I'm using System.Text.Json: public IActionResult Index() { var result = GetAllMenuItems(); return Ok(result); } The expected shape of my JSON is: [ { ...
Uttam Ughareja's user avatar
12 votes
1 answer
10k views

Set Asp.Net Core MVC Json options

One of the classes that I have in my project called, say, AC, has a property Address that is of type IPEndPoint. This type, as well as IPAddress, are notorious for not being serializable to JSON by ...
stelioslogothetis's user avatar
12 votes
3 answers
6k views

System.Text.Json.JsonReaderException isn't found in namespace

I'm converting a .NET Framework 4.5 project to .NET Core 3.1. My tests used to use Newtonsoft.Json to check if json is valid and now I would want to implement the same with the built-in System.Text....
Namiskuukkwl's user avatar
11 votes
2 answers
6k views

Migrate one method to System.Text.Json

I added NewtonsoftJson as middleware in .net core 3.1. I would love to move over to new System.Text.Json serializer. I can´t just jump over but is there way to use the one from System.Text.Json in ...
user23802's user avatar
  • 111
11 votes
1 answer
14k views

JObject missing in .NET core, in NewtonSoft package

I'm trying to load a json file in .NET Core. To do this I'm using the NewtonSoft package. I successfully installed it, and using NewtonSoft.Json; produces no compiler errors. However when I try to ...
WreckFish's user avatar
  • 376
11 votes
3 answers
13k views

How do I bind a multi level configuration object using IConfiguration in a .net Core application?

I'm trying to bind to a custom configuration object which should be populated by an appsettings.json file. My appsettings looks a bit like: { "Logging": { "IncludeScopes": true, "LogLevel":...
Necoras's user avatar
  • 7,093
11 votes
2 answers
11k views

Insert JSON into PostgreSQL database with Dapper ORM (.NET Core)

Title pretty much explains it. Everything otherwise works fine and I can INSERT/UPDATE/DELETE/SELECT from my application using Dapper with no problem. The specific issue I am having is when attempting ...
Fernando Rodriguez's user avatar
10 votes
3 answers
6k views

aws cli lambda-Could not parse request body into json

I have created aws lambda function in .net core and deployed. I have tried executing function in aws console with test case and its working. but i am not able achieve the same with cli command aws ...
sp_m's user avatar
  • 2,677
10 votes
1 answer
11k views

hosting.json available options

Where can I find some documentation regarding which options are available on the hosting.json file? Right now I'm using the server.ulrs but I'm wondering if I can add the https certificate path/...
Tiago's user avatar
  • 3,061
10 votes
2 answers
11k views

How to serialize JSON to string without escape characters in .NET Core?

I would like to serialize a C# object to JSON in a string from .NET Core. I have tried with this code, but it results in a string with escape characters for the quotes: string s = JsonConvert....
OlavT's user avatar
  • 2,586
10 votes
2 answers
9k views

How to properly set up snake case JSON for dotnet core api?

I've managed to serialize the api responses to the snake_case naming convention by using the following in my Startup.cs. This way it returns my DTO's as snake cased JSON. services .AddMvc() ...
Tom Aalbers's user avatar
  • 4,764
10 votes
4 answers
14k views

System.Text.Json Deserialize nested object from API call - Data is wrapped in parent JSON property

I have an API JSON response that wraps the data content in a data property, which looks like this: { "data":{ "email":"[email protected]", "mobile":"+1555555123", "id":4, ...
DogEatDog's user avatar
  • 2,927
10 votes
1 answer
7k views

JsonSerializer.Deserialize expected to throw exception when deserializing different class

In .NET Core 3.1 I use System.Text.Json.JsonSerializer to handle my Json objects. When I tried to write an error case when the JsonSerializer.Deserialize<T>() gets a Json string that is of a ...
SzeliTom's user avatar
  • 103
10 votes
0 answers
3k views

Setting ContentRoot and WebRoot in ASP.NET Core at runtime (e.g. using a config file)

I have an ASP.NET Core Website self hosted within a Windows Service that gets installed on multiple machines. I don't want to hardcode the WebRoot directory or depend on its relative path inside ...
Bahaa's user avatar
  • 1,667
9 votes
1 answer
18k views

Deserialize json with C# .NET Core

Im trying to deserialize data that Ive got over POST in JSON format but having some problem. The error message is: SerializationException: Expecting state 'Element'.. Encountered 'Text' with name '...
Michal Takáč's user avatar
9 votes
1 answer
9k views

Deploying Stage/Production appsettings Asp.net Core

No the guide seems quite clear (to me at least) on how production settings can be put in place. They just talk about how some of this works, but there is no step-by-step guide. I have done the ...
christopher clark's user avatar
9 votes
1 answer
4k views

How to decode websocket connect as a json stream?

My client is connecting to a websocket server and the communications are all in json format. I use the ClientWebSocket and its ReceiveAsync method as documented. My read loop is something like this: ...
fluter's user avatar
  • 13.5k
9 votes
1 answer
16k views

Property 'JsonResult.SerializerSettings' must be an instance of type 'System.Text.Json.JsonSerializerOptions'

I am developing a page that shows the webgrid of all values of Leave Type Option (which is a model containing id, LeaveType and status). In my controller, I have written something below. After I run ...
George's user avatar
  • 179
9 votes
4 answers
5k views

What is the equivalent of Newtonsoft.Json DefaultValueHandling = DefaultValueHandling.Ignore option in System.Text.Json

I'm migrating from Newtonsoft.Json to System.Text.Json in my .NET Core 3.0 application. How do I get have the same behaviour with System.Text.Json as I have in my .NET Core 2.2 app with Newtonsoft....
stevo's user avatar
  • 2,244
8 votes
1 answer
7k views

net core 2.0 appsettings.json save on bin directory

I am new in net core 2.0. I am connecting to datbase. I am used to use an App.Config or Web.Config to set the connection string. But in net core 2.0 uses appsettings.json file instead. When I ...
Diego's user avatar
  • 2,298

1
2 3 4 5
14