How to support multiple names for a property using System.Text.Json Deserialization?

PFNS-6254 0 Reputation points
2023-03-27T01:08:56.12+00:00

Hello together,

right now I'm reading following learn article:

https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/custom-contracts

It is written

Support multiple names for a single property (for example, if a previous library version used a different name).

Since JsonPropertyNameAttribute is only allowed once, does this mean only 2 names are possible like that?

public class Test {
    [JsonPropertyName("Value2")]
    public string Value1 { get; set; };
}

How could I handle deserializiing two json objects, differing only in the name of the Value, into my class?

public class Test {
    [JsonPropertyName("Value2")]
    [JsonPropertyName("Value1")]
    public string Value { get; set; }

    public string Desc { get; set}
}

I dont want to inherit from the Test class and override those propertys to have each a JsonPropertyNameAttribute assigned to.

Best regards

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,337 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Jack J Jun 24,276 Reputation points Microsoft Vendor
    2023-03-27T02:21:48.6466667+00:00

    @PFJ, Welcome to Microsoft Q&A, you could try to add a second property to your class.

    Here is a code example you could refer to.

       internal class Program
        {
            static void Main(string[] args)
            {
                string json1 = @"{""Value1"":""test1"",""Desc"":""This is d1""}";
                string json2 = @"{""Value2"":""test2"",""Desc"":""This is d2""}";
    
                var obj1 = JsonSerializer.Deserialize<Test>(json1); 
                var obj2= JsonSerializer.Deserialize<Test>(json2);
                Console.WriteLine(obj1.Value1);
                Console.WriteLine(obj2.Value1);
    
            }
        }
        public class Test
        {
           
            [JsonPropertyName("Value1")]
            public string Value1 { get; set; }
    
            [JsonPropertyName("Value2")]
            public string Value2 { set { Value1 = value; } }
    
            public string Desc { get; set; }
        }
    
    

    As the result is shown, we also could output the Value2's value:

    User's image

    Hope my solution could be helpful for you.

    Best Regards,

    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Bruce (SqlWork.com) 54,711 Reputation points
    2023-03-27T15:14:40.19+00:00

    Only one attribute is allowed. But with a custom contract you could write code that determines the property name. One of the use cases is multiple names for a single property.