-
Notifications
You must be signed in to change notification settings - Fork 29
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 #353 from Mangopay/bugfix/cardinfo-type
[bugfix] added CardInfoType serializer/deserializer
- Loading branch information
Showing
4 changed files
with
82 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
23 changes: 23 additions & 0 deletions
23
src/main/java/com/mangopay/core/deserializer/CardInfoTypeDeserializer.java
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,23 @@ | ||
package com.mangopay.core.deserializer; | ||
|
||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonDeserializer; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonParseException; | ||
import com.mangopay.core.enumerations.CardInfoType; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
public class CardInfoTypeDeserializer implements JsonDeserializer<CardInfoType> { | ||
@Override | ||
public CardInfoType deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { | ||
String value = json.getAsString(); | ||
if ("null".equals(value)) { | ||
return null; | ||
} | ||
if ("CHARGE CARD".equals(value)) { | ||
return CardInfoType.CHARGE_CARD; | ||
} | ||
return CardInfoType.valueOf(value); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/mangopay/core/serializer/CardInfoTypeSerializer.java
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,19 @@ | ||
package com.mangopay.core.serializer; | ||
|
||
import com.google.gson.*; | ||
import com.mangopay.core.enumerations.CardInfoType; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
public class CardInfoTypeSerializer implements JsonSerializer<CardInfoType> { | ||
@Override | ||
public JsonElement serialize(CardInfoType src, Type typeOfSrc, JsonSerializationContext context) { | ||
if (src == null) { | ||
return JsonNull.INSTANCE; | ||
} | ||
if (src == CardInfoType.CHARGE_CARD) { | ||
return new JsonPrimitive("CHARGE CARD"); | ||
} | ||
return new JsonPrimitive(src.name()); | ||
} | ||
} |
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,32 @@ | ||
package com.mangopay.entities; | ||
|
||
import com.mangopay.core.BaseTest; | ||
import com.mangopay.core.enumerations.CardInfoType; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNull; | ||
|
||
public class GsonTest extends BaseTest { | ||
@Test | ||
public void testSerializeCardInfoType() { | ||
CardInfoType type = CardInfoType.CHARGE_CARD; | ||
String json = getApi().getGson().toJson(type); | ||
assertEquals("\"CHARGE CARD\"", json); | ||
|
||
type = null; | ||
json = getApi().getGson().toJson(type); | ||
assertEquals("null", json); | ||
} | ||
|
||
@Test | ||
public void testDeserializeCardInfoType() { | ||
String jsonInput = "\"CHARGE CARD\""; | ||
CardInfoType deserialized = getApi().getGson().fromJson(jsonInput, CardInfoType.class); | ||
assertEquals(CardInfoType.CHARGE_CARD, deserialized); | ||
|
||
jsonInput = "null"; | ||
deserialized = getApi().getGson().fromJson(jsonInput, CardInfoType.class); | ||
assertNull(deserialized); | ||
} | ||
} |