Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting a NamingStrategy when using a JsonConverter on a class property

I've got a c# class that I am trying to correctly serialise using Newtonsoft.Json. The property is an enumeration type and I wish the value to be serialised as the "lowercase version of the enumeration name". There is a JsonConverterAttribute available for specifying this on the property and also a prewritten StringEnumConverter but I need to specify the CamelCaseNamingStrategy on that converter but I can't work out the syntax.

I've tried to assign it on the property itself:

public class C
{
    [JsonConverter(typeof(StringEnumConverter),NamingStrategy=typeof(CamelCaseNamingStrategy))]
    public ChartType ChartType { get; set; }
}

and I've also tried adding it similarly onto the enumeration type itself:

[JsonConverter(typeof(StringEnumConverter),NamingStrategy=typeof(CamelCaseNamingStrategy))]
public enum ChartType { Pie, Bar }

But the syntax is wrong. I can't find any examples of this in the Newtonsoft documentation.

The desired serialision would be: "ChartType":"pie" or "ChartType":"bar"

Any ideas? Thanks.

like image 985
Chris Walsh Avatar asked Feb 01 '19 17:02

Chris Walsh


People also ask

Which of the following attribute should be used to indicate the property must not be serialized while using .JSON serializer?

We still use [JsonIgnore] attribute, adding it to the property which we do not want to be serialized.

How to set JsonProperty name in c#?

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.

What is the use of JsonProperty attribute in C#?

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.

What is JSON property name?

The Jackson Annotation @JsonProperty is used on a property or method during serialization or deserialization of JSON. It takes an optional 'name' parameter which is useful in case the property name is different than 'key' name in JSON.


2 Answers

Okay, this appears to work:

[JsonProperty("type")] 
[JsonConverter(typeof(StringEnumConverter), 
     converterParameters:typeof(CamelCaseNamingStrategy))]
public ChartType ChartType { get; }  

As NamingStrategy is a property of the StringEnumConverter it's applied using the converterParameters parameter. This got my desired output. I think an example of this would be useful in Newtonsoft documentation.

like image 163
Chris Walsh Avatar answered Sep 17 '22 20:09

Chris Walsh


Another possible solution is using JsonSerializerSettings

var settings = new JsonSerializerSettings
{
    Converters = new List<JsonConverter> {
        new StringEnumConverter(new CamelCaseNamingStrategy())
    }
};
var result = JsonConvert.SerializeObject(obj, settings);
like image 35
Alexander Avatar answered Sep 18 '22 20:09

Alexander