Skip to content

Instantly share code, notes, and snippets.

@orip
Created September 5, 2012 11:22
Show Gist options
  • Star 37 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save orip/3635246 to your computer and use it in GitHub Desktop.
Save orip/3635246 to your computer and use it in GitHub Desktop.
Gson type adapter to serialize and deserialize byte arrays in base64
import java.lang.reflect.Type;
import android.util.Base64;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
public class GsonHelper {
public static final Gson customGson = new GsonBuilder().registerTypeHierarchyAdapter(byte[].class,
new ByteArrayToBase64TypeAdapter()).create();
// Using Android's base64 libraries. This can be replaced with any base64 library.
private static class ByteArrayToBase64TypeAdapter implements JsonSerializer<byte[]>, JsonDeserializer<byte[]> {
public byte[] deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return Base64.decode(json.getAsString(), Base64.NO_WRAP);
}
public JsonElement serialize(byte[] src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(Base64.encodeToString(src, Base64.NO_WRAP));
}
}
}
@DamianoPantani
Copy link

DamianoPantani commented Feb 14, 2017

Thank you. Using org.apache.commons.codec.binary.Base64 this methods seems to be working:

@Override
public byte[] deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
	return Base64.decodeBase64(json.getAsString());
}

@Override
public JsonElement serialize(byte[] src, Type typeOfSrc, JsonSerializationContext context) {
	return new JsonPrimitive(Base64.encodeBase64String(src));
}

@pupeno
Copy link

pupeno commented Aug 29, 2017

Using Java 1.8, with Lambdas, I have serialization a deserialization of byte arrays to Base64 with this code:

GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(byte[].class, (JsonSerializer<byte[]>) (src, typeOfSrc, context) -> new JsonPrimitive(Base64.getEncoder().encodeToString(src)));
builder.registerTypeAdapter(byte[].class, (JsonDeserializer<byte[]>) (json, typeOfT, context) -> Base64.getDecoder().decode(json.getAsString()));
Gson gson = builder.create();

@bahcetepe
Copy link

thank you so much.

@deois
Copy link

deois commented Apr 12, 2019

thank you!!!

@zerocen
Copy link

zerocen commented Feb 3, 2021

Thanks!!!

@CrazyBunQnQ
Copy link

Using Java 1.8, with Lambdas, I have serialization a deserialization of byte arrays to Base64 with this code:

GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(byte[].class, (JsonSerializer<byte[]>) (src, typeOfSrc, context) -> new JsonPrimitive(Base64.getEncoder().encodeToString(src)));
builder.registerTypeAdapter(byte[].class, (JsonDeserializer<byte[]>) (json, typeOfT, context) -> Base64.getDecoder().decode(json.getAsString()));
Gson gson = builder.create();

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment