-
Notifications
You must be signed in to change notification settings - Fork 24
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 #243 from Mangopay/bugfix/cardinfo-type-enum
[Bugfix] added custom converted for CardInfo.Type
- Loading branch information
Showing
3 changed files
with
90 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using MangoPay.SDK.Core.Enumerations; | ||
using Newtonsoft.Json; | ||
|
||
namespace MangoPay.SDK.Core.Serializers | ||
{ | ||
/// <summary> | ||
/// Serialize "CHARGE_CARD" as "CHARGE CARD" | ||
/// </summary> | ||
public class CardInfoTypeConverter : JsonConverter<CardInfoType?> | ||
{ | ||
public override void WriteJson(JsonWriter writer, CardInfoType? value, JsonSerializer serializer) | ||
{ | ||
if (value == null) | ||
{ | ||
writer.WriteNull(); | ||
return; | ||
} | ||
var stringValue = value == CardInfoType.CHARGE_CARD ? "CHARGE CARD" : value.ToString(); | ||
writer.WriteValue(stringValue); | ||
} | ||
|
||
public override CardInfoType? ReadJson(JsonReader reader, Type objectType, CardInfoType? existingValue, | ||
bool hasExistingValue, JsonSerializer serializer) | ||
{ | ||
if (reader.Value == null) | ||
{ | ||
return null; | ||
} | ||
var value = reader.Value.ToString(); | ||
var cardInfo = value == "CHARGE CARD" ? CardInfoType.CHARGE_CARD : (CardInfoType)Enum.Parse(typeof(CardInfoType), value); | ||
return cardInfo; | ||
} | ||
} | ||
} |
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