Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JsonPropertyAttribute not working with . in name

I'm creating an Azure DevOps webhook endpoint for a work item creation trigger using ASP.NET Core 3.1. The payload produced by DevOps has . characters in some property names on the Fields array

"System.AreaPath": "FabrikamCloud",
"System.TeamProject": "FabrikamCloud",
"System.IterationPath": "FabrikamCloud\\Release 1\\Sprint 1",
"System.WorkItemType": "Bug",
"System.State": "New",
"System.Reason": "New defect reported",
"System.CreatedDate": "2014-07-15T17:42:44.663Z",
"System.CreatedBy": {

I've created models to represent the various levels of the object graph and the parents are serialising nicely but even if I annotate these properties appropriately they don't de-serialise and all values are set to defaults

public class Fields
{
    [JsonProperty("System.AreaPath")]
    public string SystemAreaPath { get; set; }

    [JsonProperty("System.TeamProject")]
    public string SystemTeamProject { get; set; }

    [JsonProperty("System.IterationPath")]
    public string SystemIterationPath { get; set; }

    [JsonProperty("System.WorkItemType")]
    public string SystemWorkItemType { get; set; }

    [JsonProperty("System.State")]
    public string SystemState { get; set; }

    [JsonProperty("System.Reason")]
    public string SystemReason { get; set; }

    [JsonProperty("System.CreatedDate")]
    public DateTime SystemCreatedDate { get; set; }

    [JsonProperty("System.CreatedBy")]
    public UserDetails SystemCreatedBy { get; set; }

    [JsonProperty("System.ChangedDate")]
    public DateTime SystemChangedDate { get; set; }

    [JsonProperty("System.ChangedBy")]
    public UserDetails SystemChangedBy { get; set; }

    [JsonProperty("System.Title")]
    public string SystemTitle { get; set; }

Anyone know how to deal with property names containing the decimal point?

like image 739
Stephen York Avatar asked Sep 15 '25 19:09

Stephen York


2 Answers

You can use

[JsonPropertyName("System.AreaPath")]

instead of

[JsonProperty("System.AreaPath")] 

from System.Text.Json, which is used by default in .net core 3 as mentioned by @Hintee

like image 118
Roman Marusyk Avatar answered Sep 17 '25 09:09

Roman Marusyk


The JsonProperty attribute works just fine as long as you're using Newtonsoft Json to handle JSON serialization/deserialization. But AspNet Core 3+ uses System.Text.Json by default.

Here's an article to configure it to use Newtonsoft:

https://dotnetcoretutorials.com/2019/12/19/using-newtonsoft-json-in-net-core-3-projects/

like image 23
Hintee Avatar answered Sep 17 '25 10:09

Hintee