Json2CSharp / Json2CSharpCodeGenerator

Microsoft Reciprocal License
305 stars 83 forks source link

Can't add both JsonProperty and JsonPropertyName #88

Closed Digital-Composition closed 1 year ago

Digital-Composition commented 2 years ago

Despite the having the option to check both attributes, only one (JsonPropertyName) is selected. It would be so handy if I could add both attributes.

Json2CSharp commented 1 year ago

Why would you need to check both options, these should be radio buttons instead but you need to choose only one option

Digital-Composition commented 1 year ago

@Json2CSharp the tool is very useful as it is. Yet, I package model libraries for references across a big system. Because the system I use is massive, some times JsonNewtonsoft and .Net libraries are used in different parts of the system. For instance, if I use Dependency Injection and I add HttpClient services (Default), the ReadFromJsonBody method that is offered uses .Net library so I'm stuck with JsonPropertyName attributes if I want to deserialize a Json object. So it would be beneficial for some developers to have that duality of model attributes just in cae it is necessary.

Json2CSharp commented 1 year ago

can you please provide an example output of what you want?

Digital-Composition commented 1 year ago

@Json2CSharp, in this example a developer has used Newtonsoft library to convert an XML formatted string into JSON string to be deserialized and used as an object. The developer uses a reference to a packaged library that has the company's most common models. Because the JSON is the result of an XML transformation, some properties have "@" symbol attached to the property name which requires a property name declaration to be deserialized correctly (JsonProperty).

Newtonsoft

XmlDocument doc = new XmlDocument();
doc.LoadXml(message);
string jsonText = JsonConvert.SerializeXmlNode(doc);
var payload = JsonConvert.DeserializeObject<Data>(jsonText);

Now the same model library is used in another project and that uses .NET library to handle JSON objects such as case of the default HttpClient class. So it would be beneficial to have the possibility to check both options "JsonProperty" and "JsonProperyName" when converting a JSON payload into C# data model.

System.Text.Json (.NET)

var content = await httpResponse.Content.ReadFromJsonAsync<Data>();

MODEL

public class Data
{
    [JsonProperty("@updateTime")]
    [JsonPropertyName("@updateTime")]
    public string UpdateTime { get; set; }

    [JsonProperty("Content")]
    [JsonPropertyName("Content")]
    public string Content { get; set; }
}
Json2CSharp commented 1 year ago

Ok I got it, so we will allow the two options for JsonProperty as well as for JsonPropertyName

Digital-Composition commented 1 year ago

@Json2CSharp a million thanks. I asked everybody in my team to disable Ad-blocker in your website to help the cause 😄

Json2CSharp commented 1 year ago

Thanks a lot ❤️ !!!

Json2CSharp commented 1 year ago

This is done, thanks for letting me know 👍