Created
July 9, 2020 09:02
-
-
Save gsscoder/d5ca419a7ed0d5fb6e200f81a7aab9ac to your computer and use it in GitHub Desktop.
C# Json.NET naming strategy to serialize enum values as lowercase strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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