-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #132 from mercadopago/release/1.9.1
Release 1.9.1
- Loading branch information
Showing
9 changed files
with
104 additions
and
32 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System; | ||
using System.Text; | ||
using Newtonsoft.Json; | ||
|
||
namespace MercadoPago.Common | ||
{ | ||
public class ShippingModeConverter : JsonConverter | ||
{ | ||
public override bool CanConvert(Type objectType) | ||
{ | ||
return objectType == typeof(string); | ||
} | ||
|
||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
string value = (string)reader.Value; | ||
if (string.IsNullOrEmpty(value)) | ||
{ | ||
return null; | ||
} | ||
return Enum.Parse(typeof(ShipmentMode), SnakeCaseToLowerCase(value), true); | ||
} | ||
|
||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
ShipmentMode? shipmentMode = (ShipmentMode?)value; | ||
if (shipmentMode.HasValue) | ||
{ | ||
writer.WriteValue(PascalCaseToSnakeCase(shipmentMode.ToString())); | ||
} | ||
} | ||
|
||
private static string PascalCaseToSnakeCase(string value) | ||
{ | ||
if (value == null || value.Trim().Length == 0) | ||
{ | ||
return value; | ||
} | ||
|
||
value = value.Trim(); | ||
StringBuilder sb = new StringBuilder(); | ||
sb.Append(char.ToLowerInvariant(value[0])); | ||
for (int i = 1; i < value.Length; i++) | ||
{ | ||
char c = value[i]; | ||
if (char.IsUpper(c)) | ||
{ | ||
sb.Append('_'); | ||
sb.Append(char.ToLowerInvariant(c)); | ||
} | ||
else | ||
{ | ||
sb.Append(c); | ||
} | ||
} | ||
|
||
return sb.ToString(); | ||
} | ||
|
||
private static string SnakeCaseToLowerCase(string value) | ||
{ | ||
if (value == null || value.Trim().Length == 0) | ||
{ | ||
return value; | ||
} | ||
|
||
StringBuilder sb = new StringBuilder(); | ||
string[] tokens = value.Trim().Split('_'); | ||
for (int i = 0; i < tokens.Length; i++) | ||
{ | ||
if (!string.IsNullOrEmpty(tokens[i]) && tokens[i].Length > 0) | ||
{ | ||
sb.Append(tokens[i]); | ||
} | ||
} | ||
|
||
return sb.ToString(); | ||
} | ||
} | ||
} |
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
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