I have the following JSON for patch request:
{
"idfa": "28A427FE-770B-4FA3-AA8E-123",
"idfv": "11B3343C-ECBB-4CC8123B5BA-DDD9CA5768FD",
"app_build_number": 1,
"app_version": "1.0.0",
"screen_height": 820,
"screen_width": 300,
"locale": "ru",
"app_id": "com.hello",
"app_platform": "iOS",
"manufacturer": "Apple",
"model": "iPhone10,6",
"os_version": "12.3.1",
"sdk_version": "0.3"
}
And the following model for mapping:
public class CustomerChangeViewModel
{
[JsonProperty("idfa")]
[Required(ErrorMessage = "Required idfa")]
public string Idfa { get; set; }
[JsonProperty("idfv")]
[Required(ErrorMessage = "Required idfv")]
public string Idfv { get; set; }
[Required(ErrorMessage = "Required app_build_number")]
[JsonProperty("app_build_number")]
public string AppBuildNumber { get; set; }
[JsonProperty("app_version")]
[Required(ErrorMessage = "Required app_version")]
public string AppVersion { get; set; }
[JsonProperty("screen_height")]
[Required(ErrorMessage = "Required screen_height")]
public string ScreenHeight { get; set; }
[JsonProperty("screen_width")]
[Required(ErrorMessage = "Required width")]
public string ScreenWidth { get; set; }
[JsonProperty("locale")]
[Required(ErrorMessage = "Required locale")]
public string Locale { get; set; }
[JsonProperty("app_id")]
[Required(ErrorMessage = "Required app_id")]
public string AppId { get; set; }
[JsonProperty("app_platform")]
[Required(ErrorMessage = "Required app_platform")]
public string AppPlatform { get; set; }
[JsonProperty("manufacturer")]
[Required(ErrorMessage = "Required manufacturer")]
public string Manufacturer { get; set; }
[JsonProperty("model")]
[Required(ErrorMessage = "Required model")]
public string Model { get; set; }
[JsonProperty("os_version")]
[Required(ErrorMessage = "Required os_version")]
public string OsVersion { get; set; }
[JsonProperty("sdk_version")]
[Required(ErrorMessage = "Required sdk_version")]
public string SdkVersion { get; set; }
}
And controller:
[Route("/api/v1.0/startup")]
[HttpPatch]
public async Task<IActionResult> Update([FromBody] CustomerChangeViewModel viewModel)
{
...
}
After sending this request I have the following:
As you can see not all fields were mapped. I think there is a problem with fields with "_" symbol. Any ideas why it's happening? I use .NET Core 3.1 and Insomnia as HTTP client.
P.S I'm not sure is it necessary here, but my routing settings is:
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
To set the name of individual properties, use the [JsonPropertyName] attribute. The property name set by this attribute: Applies in both directions, for serialization and deserialization.
JsonPropertyAttribute indicates that a property should be serialized when member serialization is set to opt-in. It includes non-public properties in serialization and deserialization. It can be used to customize type name, reference, null, and default value handling for the property value.
.NET Core 3.* is using System.Text.Json by default and it doesn't work with JsonPropertyAttribute class. You need to install Microsoft.AspNetCore.Mvc.NewtonsoftJson. And call AddNewtonsoftJson extension method to set ASP.NET Core project to use Newtonsoft.Json packages.
By default, JSON is minified. You can pretty-print the JSON. By default, casing of JSON names matches the .NET names. You can customize JSON name casing. By default, circular references are detected and exceptions thrown. You can preserve references and handle circular references.
If the JSON contains a value for a read-only property, the value is ignored and no exception is thrown. Non-public constructors are ignored by the serializer. Deserialization to immutable objects or properties that don't have public set accessors is supported.
By default, if a property in JSON is null, the corresponding property in the target object is set to null. In some scenarios, the target property might have a default value, and you don't want a null value to override the default. For example, suppose the following code represents your target object: C#.
Also you can use [JsonPropertyName("model")]
attribute instead of [JsonPropertyAttribute("model")]
if you want to use native System.Text.Json
for .net core 3.1
.NET Core 3.* is using System.Text.Json
by default and it doesn't work with JsonPropertyAttribute
class.
You need to install Microsoft.AspNetCore.Mvc.NewtonsoftJson
.
Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson
And call AddNewtonsoftJson
extension method to set ASP.NET Core project to use Newtonsoft.Json
packages.
services.AddControllers().AddNewtonsoftJson();
Related links
Using Newtonsoft.Json In .NET Core 3+ Projects
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With