
package org.microworld.mangopay.implementation.serialization;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.microworld.mangopay.exceptions.MangopayUnauthorizedException;

import java.lang.reflect.Type;
import java.util.Optional;

public class MangopayUnauthorizedExceptionDeserializer implements JsonDeserializer<MangopayUnauthorizedException> {
    @Override
    public MangopayUnauthorizedException deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) {
        final JsonObject object = json.getAsJsonObject();
        return new MangopayUnauthorizedException(object.get("error").getAsString(), Optional.ofNullable(object.get("error_description")).map(JsonElement::getAsString).orElse("(no error description)"));
    }
}
