-
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.
added CardInfoType serializer/deserializer
- Loading branch information
Iulian Masar
committed
Feb 11, 2025
1 parent
1445978
commit e05aff8
Showing
4 changed files
with
79 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
20 changes: 20 additions & 0 deletions
20
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,20 @@ | ||
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 ("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.JsonElement; | ||
import com.google.gson.JsonPrimitive; | ||
import com.google.gson.JsonSerializationContext; | ||
import com.google.gson.JsonSerializer; | ||
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 == 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); | ||
} | ||
} |