0

I have a class in my project which needs to be serialized for two different use cases. As I can't add two diffrent attributes to on property I would like to serialize the objects of the class one time with the [JsonProperty("attributname")] decleration and one time with the property name it self.

For ex:

public class Contact
{
    [JsonProperty("name")]
    public string Lastname { get; set; }
}

public class Program
{
    public void SerializeByJsonPropertyName()
    {
        var contact = new Contact()
        {
            Lastname = "Harber"
        }
        var requestJson = JsonConvert.SerializeObject(contact);
        // Serialized Object requestJson:
        // {
        //     "name" = "Harber"
        // }
    }

    public void SerializeByPropertyName()
    {
        var contact = new Contact()
        {
            Lastname = "Harber"
        }
        var requestJson = JsonConvert.SerializeObject(contact /*, ? Setting ?*/);
        // Serialized Object requestJson:
        // {
        //     "Lastname" = "Harber"
        // }
    }
}

The first szenario works totaly fine, but for the second szenario I could´t find any solution. Except creating two classes or duplicate the properties in my class.. IS there any setting in Newtonsofts JsonConverter for doing this?

Thanks for your help!

marco
  • 3
  • 2

1 Answers1

0

You can create different naming strategies, then create different settings, each setting has a Contract resolver, each contract resolver has a naming strategy, then you supply for each settings the naming strategy you want to use, something like this

public static class JsonSerializingSettings {

    public static JsonSerializerSettings JsonUnModified{ get; set; } = new JsonSerializerSettings
    {
        ContractResolver = new DefaultContractResolver() {
            NamingStrategy =  new UnmodifiedNamingStrategy()
        }
    };
    public static JsonSerializerSettings JsonDefault { get; set; } = new JsonSerializerSettings
    {
        ContractResolver = new DefaultContractResolver() {
            NamingStrategy =  new DefaultNamingStrategy()
        }
    };
 }
public class UnmodifiedNamingStrategy : NamingStrategy {


    protected override string ResolvePropertyName(string name) {
        return name;
    }

}

and when you want to use it

    var requestJson = JsonConvert.SerializeObject(contact,JsonSerializingSettings.JsonDefault);
    // Serialized Object requestJson:
    // {
    //     "name" = "Harber"
    // }

var requestJson = JsonConvert.SerializeObject(contact,JsonSerializingSettings.JsonUnModified);
    // Serialized Object requestJson:
    // {
    //     "Lastname" = "Harber"
    // }
Munzer
  • 2,216
  • 2
  • 18
  • 25
  • Thanks for your answers! I implemented a combination of your answers, which solved my problem. Perfect! – marco Mar 26 '20 at 10:31
  • [Good further Read](https://dotnetcoretutorials.com/2018/05/05/setting-json-serialization-configuration-at-runtime-on-a-net-core-api/), please mark my answer as correct if it was correct, thank you and I am glad I could help – Munzer Mar 26 '20 at 10:33
  • @Munzer I tried it and worked on the first level object , but the nested objects are still the same – Hana Jul 19 '20 at 17:37