All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jp.gopay.sdk.adapters.JsonAdapters Maven / Gradle / Ivy

There is a newer version: 0.11.17
Show newest version
package jp.gopay.sdk.adapters;

import com.google.gson.*;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import jp.gopay.sdk.models.common.*;
import jp.gopay.sdk.models.common.auth.LoginJWTStrategy;
import jp.gopay.sdk.models.request.subscription.RemoveInstallmentsPlan;
import jp.gopay.sdk.models.response.PaymentsPlan;
import jp.gopay.sdk.models.response.gateway.GoPayGateway;
import jp.gopay.sdk.models.response.subscription.SimulatedPayment;
import jp.gopay.sdk.models.response.transactiontoken.TokenAliasKey;
import jp.gopay.sdk.types.*;
import org.joda.time.LocalDate;
import org.joda.time.Period;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import org.threeten.bp.Duration;
import org.threeten.bp.ZoneId;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

public class JsonAdapters {

    public final static DateTimeFormatter dateTimeParser = ISODateTimeFormat.dateTimeParser();
    public final static DateTimeFormatter dateTimePrinter = ISODateTimeFormat.dateTime();

    public static class JsonDateAdapter implements JsonSerializer, JsonDeserializer {
        public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(dateTimePrinter.print(src.getTime()));
        }
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            String s = json.getAsJsonPrimitive().getAsString();
            return dateTimeParser.parseDateTime(s).toDate();
        }
    }

    public static class JsonPeriodAdapter implements JsonSerializer, JsonDeserializer {
        public JsonElement serialize(Period src, Type typeOfSrc, JsonSerializationContext context){
            return new JsonPrimitive(src.toString());
        }


        public Period deserialize(JsonElement json, Type typeOfP, JsonDeserializationContext context) {
            String s = json.getAsJsonPrimitive().getAsString();
            return Period.parse(s);
        }
    }

    public static class JsonDurationAdapter implements JsonSerializer, JsonDeserializer{
        @Override
        public JsonElement serialize(Duration duration, Type type, JsonSerializationContext jsonSerializationContext) {
            return new JsonPrimitive(duration.toString());
        }

        @Override
        public Duration deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
            String s = jsonElement.getAsJsonPrimitive().getAsString();
            return Duration.parse(s);
        }
    }

    public static class JsonLocalDateAdapter implements JsonSerializer, JsonDeserializer{
        public JsonElement serialize(LocalDate src, Type typeOfSrc, JsonSerializationContext context){
            return new JsonPrimitive(src.toString());
        }
        public LocalDate deserialize(JsonElement json, Type typeOfP, JsonDeserializationContext context){
            String s = json.getAsJsonPrimitive().getAsString();
            return LocalDate.parse(s);
        }
    }

    public static class JsonRemoveInstallmentsPlanAdapter extends TypeAdapter{
        private final Gson gson;

        public JsonRemoveInstallmentsPlanAdapter(Gson nullableGson) {
            this.gson = nullableGson;
        }

        @Override
        public void write(JsonWriter out, RemoveInstallmentsPlan value) throws IOException {
            gson.toJson(JsonNull.INSTANCE, out);
        }

        @Override
        public RemoveInstallmentsPlan read(JsonReader in) throws IOException {
            return new RemoveInstallmentsPlan();
        }
    }

    public static class JsonJWTDeserializer implements JsonDeserializer{
        @Override
        public LoginJWTStrategy deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            return new LoginJWTStrategy(json.getAsJsonPrimitive().getAsString());
        }

    }

    public static class JsonDomainAdapter implements JsonSerializer, JsonDeserializer{
        @Override
        public Domain deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            return new Domain(json.getAsJsonPrimitive().getAsString());
        }

        @Override
        public JsonElement serialize(Domain src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src.asString());
        }
    }

    public static class JsonZoneIdAdapter implements JsonSerializer, JsonDeserializer {
        @Override
        public JsonElement serialize(ZoneId src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src.getId());
        }

        @Override
        public ZoneId deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            return ZoneId.of(json.getAsJsonPrimitive().getAsString());
        }
    }

    public static class JsonCountryAdapter implements JsonSerializer, JsonDeserializer {
        @Override
        public Country deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            return Country.getCountryByAlpha2(json.getAsJsonPrimitive().getAsString());
        }

        @Override
        public JsonElement serialize(Country src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src.getAlpha2());
        }
    }

    public static class JsonMetadataMapAdapter implements  JsonDeserializer {
        @Override
        public MetadataMap deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            MetadataMap metadataMap = new MetadataMap();
            for (Map.Entry entry : json.getAsJsonObject().entrySet()) {
                if (entry.getValue().isJsonPrimitive() && entry.getValue().getAsJsonPrimitive().isString()) {
                    metadataMap.put(entry.getKey(), entry.getValue().getAsString());
                } else {
                    metadataMap.put(entry.getKey(), new Gson().toJson(entry.getValue()));
                }
            }
            return metadataMap;
        }
    }

    public static class JsonCardBrandAdapter implements JsonDeserializer {
        @Override
        public CardBrand deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            return CardBrand.forBrandName(json.getAsJsonPrimitive().getAsString());
        }
    }

    public static class JsonPaymentsPlanAdapter implements JsonSerializer, JsonDeserializer{


        @Override
        public PaymentsPlan deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            final ArrayList payments = new ArrayList<>();
            Class clazz = SimulatedPayment.class;
            for(JsonElement elem : json.getAsJsonArray()){
                payments.add((SimulatedPayment) context.deserialize(elem, clazz));
            }
            return new PaymentsPlan(payments);
        }

        @Override
        public JsonElement serialize(PaymentsPlan src, Type typeOfSrc, JsonSerializationContext context) {
            JsonArray jsonPayments = new JsonArray();
            for(SimulatedPayment payment: src){
                jsonPayments.add(
                        context.serialize(payment)
                );
            }
            return jsonPayments;
        }
    }

    public static class JsonDayOfMonthAdapter implements JsonSerializer, JsonDeserializer{

        @Override
        public DayOfMonth deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            Integer jsonDayOfMonth = json.getAsNumber().intValue();
            return new DayOfMonth(jsonDayOfMonth);
        }

        @Override
        public JsonElement serialize(DayOfMonth src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src.getDay());
        }
    }

    public static class JsonTokenAliasKeyAdapter implements JsonSerializer, JsonDeserializer{

        @Override
        public TokenAliasKey deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            String keyStr = json.getAsString();
            return new TokenAliasKey(keyStr);
        }

        @Override
        public JsonElement serialize(TokenAliasKey src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src.getKey());
        }
    }

    public static class JsonPaidyTokenAdapter implements JsonSerializer, JsonDeserializer{

        @Override
        public PaidyToken deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            String str = json.getAsString();
            return new PaidyToken(str);
        }

        @Override
        public JsonElement serialize(PaidyToken src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src.getPaidyToken());
        }
    }

    public static class JsonGoPayGatewayAdapter implements JsonDeserializer{
        @Override
        public GoPayGateway deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            return new GoPayGateway(Gateway.valueOf(json.getAsString().toUpperCase()));
        }
    }

    public static class JsonEmailAddressAdapter implements JsonSerializer, JsonDeserializer {
        @Override
        public JsonElement serialize(GoPayEmailAddress src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src.serialize());
        }

        @Override
        public GoPayEmailAddress deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            String email = json.getAsString();
            if (email.isEmpty()) {
                return new EmptyEmailAddress();
            }
            return new EmailAddress(email);
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy