package org.irmacard.api.common.util;

import com.google.gson.*;
import org.bouncycastle.util.encoders.Base64;

import java.lang.reflect.Type;
import java.math.BigInteger;

public class Base64BigIntegerSerializer implements JsonSerializer<BigInteger>, JsonDeserializer<BigInteger> {
	@Override
	public BigInteger deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
		try {
			return new BigInteger(1, Base64.decode(json.getAsJsonPrimitive().getAsString()));
		} catch (Exception e) {
			throw new JsonParseException("Failed parsing json into BigInteger", e);
		}
	}

	@Override
	public JsonElement serialize(BigInteger src, Type typeOfSrc, JsonSerializationContext context) {
		if (src.signum() == -1)
			throw new JsonSyntaxException("Cannot serialize negative bigint " + src.toString());
		return context.serialize(Base64.toBase64String(src.toByteArray()));
	}
}

@@ -422,16 +422,86 @@
 %09%09try %7B%0A
+%09%09%09JsonPrimitive p = json.getAsJsonPrimitive();%0A%09%09%09if (p.isString())%0A%09
 %09%09%09retur
@@ -577,16 +577,56 @@
 ng()));%0A
+%09%09%09else%0A%09%09%09%09return p.getAsBigInteger();%0A
 %09%09%7D catc

Add base10 deserializing to Base64BigIntegerSerializer for backwards compatibility