Skip to content

Commit

Permalink
update to use naming strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
feinoujc committed Dec 9, 2018
1 parent b490a29 commit 60b6363
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/Mandrill.net/Model/WebHook/MandrillMessageEventType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public enum MandrillMessageEventType
{
Send,
Deferral,
[EnumMember(Value = "hard_bounce")] HardBounce,
[EnumMember(Value = "soft_bounce")] SoftBounce,
HardBounce,
SoftBounce,
Open,
Click,
Spam,
Expand Down
2 changes: 0 additions & 2 deletions src/Mandrill.net/Model/WebHook/MandrillWebHookEventType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ namespace Mandrill.Model
public enum MandrillWebHookEventType
{
Send,
[EnumMember(Value = "hard_bounce")]
HardBounce,
[EnumMember(Value = "soft_bounce")]
SoftBounce,
Open,
Click,
Expand Down
31 changes: 4 additions & 27 deletions src/Mandrill.net/Serialization/MandrillJsonContractResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,11 @@ namespace Mandrill.Serialization
{
internal class MandrillJsonContractResolver : DefaultContractResolver
{
private static readonly Regex CamelCaseRegex = new Regex("^[A-Z][a-z]+(?:[A-Z][a-z]+)*$", RegexOptions.Compiled);

protected static string ConvertCamelCasePropertyNamesToLowerCaseUnderscoreStyle(string propertyName)
public MandrillJsonContractResolver()
{
if (CamelCaseRegex.IsMatch(propertyName))
{
return Regex.Replace(
Regex.Replace(
Regex.Replace(propertyName, @"([A-Z]+)([A-Z][a-z])", "$1_$2"), @"([a-z\d])([A-Z])",
"$1_$2"), @"[-\s]", "_").ToLower();

}
return (propertyName).ToLower();
NamingStrategy = new SnakeCaseNamingStrategy(false, false);
}


protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var jsonProperty = base.CreateProperty(member, memberSerialization);
Expand All @@ -37,7 +26,7 @@ protected override JsonProperty CreateProperty(MemberInfo member, MemberSerializ
if (propertyType.GetTypeInfo().IsGenericType && propertyType.GenericTypeArguments.Length == 1)
{
var t = propertyType.GenericTypeArguments[0];
if (typeof (IList<>).MakeGenericType(t).GetTypeInfo().IsAssignableFrom(propertyType.GetTypeInfo()))
if (typeof(IList<>).MakeGenericType(t).GetTypeInfo().IsAssignableFrom(propertyType.GetTypeInfo()))
{
var prop = jsonProperty.DeclaringType.GetTypeInfo().GetDeclaredProperty(member.Name);
jsonProperty.ShouldSerialize = instance =>
Expand All @@ -48,7 +37,7 @@ protected override JsonProperty CreateProperty(MemberInfo member, MemberSerializ
}
}

if (typeof (IDictionary<string, string>).GetTypeInfo().IsAssignableFrom(propertyType.GetTypeInfo()))
if (typeof(IDictionary<string, string>).GetTypeInfo().IsAssignableFrom(propertyType.GetTypeInfo()))
{
var prop = jsonProperty.DeclaringType.GetTypeInfo().GetDeclaredProperty(member.Name);

Expand All @@ -72,19 +61,7 @@ protected override JsonProperty CreateProperty(MemberInfo member, MemberSerializ
}
}

//leave the keys of a dictionary alone, otherwise convert to lowercase underscore
if (!PropertyIsInDictionary(jsonProperty))
{
jsonProperty.PropertyName = ConvertCamelCasePropertyNamesToLowerCaseUnderscoreStyle(jsonProperty.PropertyName);
}

return jsonProperty;
}

private static bool PropertyIsInDictionary(JsonProperty jsonProperty)
{
return typeof (IDictionary<string, string>).GetTypeInfo().IsAssignableFrom(jsonProperty.DeclaringType.GetTypeInfo())
|| typeof (IDictionary<string, object>).GetTypeInfo().IsAssignableFrom(jsonProperty.DeclaringType.GetTypeInfo());
}
}
}
2 changes: 1 addition & 1 deletion src/Mandrill.net/Serialization/MandrillSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ private static JsonSerializer CreateSerializer()
var settings = new JsonSerializerSettings { ContractResolver = new MandrillJsonContractResolver() };

settings.Converters.Add(new UnixDateTimeConverter());
settings.Converters.Add(new StringEnumConverter { NamingStrategy = new CamelCaseNamingStrategy(), AllowIntegerValues = false });
settings.Converters.Add(new StringEnumConverter { NamingStrategy = new SnakeCaseNamingStrategy(), AllowIntegerValues = false });
settings.NullValueHandling = NullValueHandling.Ignore;
settings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
return JsonSerializer.Create(settings);
Expand Down
2 changes: 1 addition & 1 deletion tests/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ public TestModel()
private enum TestEnum
{
Reject,
[EnumMember(Value = "soft_bounce")] SoftBounce
SoftBounce
}

private class TestSubModel
Expand Down

0 comments on commit 60b6363

Please sign in to comment.