tinder.core.helpers.GsonDeserializer Maven / Gradle / Ivy
package tinder.core.helpers;
import com.google.gson.Gson;
import io.jsonwebtoken.io.DeserializationException;
import io.jsonwebtoken.io.Deserializer;
import io.jsonwebtoken.lang.Assert;
import java.io.IOException;
// This class has been ported over from a PR that is currently not merged in jjwt: https://github.com/jwtk/jjwt/pull/414
public class GsonDeserializer implements Deserializer {
private final Class returnType;
private final Gson gson;
@SuppressWarnings("unused") //used via reflection by RuntimeClasspathDeserializerLocator
public GsonDeserializer() {
this(GsonSerializer.DEFAULT_GSON);
}
@SuppressWarnings({"unchecked", "WeakerAccess", "unused"}) // for end-users providing a custom gson
public GsonDeserializer(Gson gson) {
this(gson, (Class) Object.class);
}
private GsonDeserializer(Gson gson, Class returnType) {
Assert.notNull(gson, "gson cannot be null.");
Assert.notNull(returnType, "Return type cannot be null.");
this.gson = gson;
this.returnType = returnType;
}
@Override
public T deserialize(byte[] bytes) throws DeserializationException {
return readValue(bytes);
}
protected T readValue(byte[] bytes) {
return gson.fromJson(new String(bytes), returnType);
}
}