Skip to content

Instantly share code, notes, and snippets.

@gsscoder
Created July 9, 2020 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gsscoder/d5ca419a7ed0d5fb6e200f81a7aab9ac to your computer and use it in GitHub Desktop.
Save gsscoder/d5ca419a7ed0d5fb6e200f81a7aab9ac to your computer and use it in GitHub Desktop.
C# Json.NET naming strategy to serialize enum values as lowercase strings
/*
public class YourClass
{
[JsonProperty("type")]
[JsonConverter(typeof(StringEnumConverter), converterParameters: typeof(LowerCaseNamingStrategy))]
public YourEnum YourProperty { get; set; }
}
*/
using Newtonsoft.Json.Serialization;
/// <summary>A lower case naming strategy.</summary>
public class LowerCaseNamingStrategy : NamingStrategy
{
public LowerCaseNamingStrategy(bool processDictionaryKeys, bool overrideSpecifiedNames)
{
ProcessDictionaryKeys = processDictionaryKeys;
OverrideSpecifiedNames = overrideSpecifiedNames;
}
public LowerCaseNamingStrategy(bool processDictionaryKeys, bool overrideSpecifiedNames, bool processExtensionDataNames)
: this(processDictionaryKeys, overrideSpecifiedNames)
{
ProcessExtensionDataNames = processExtensionDataNames;
}
public LowerCaseNamingStrategy() { }
protected override string ResolvePropertyName(string name) => name.ToLower();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment