美文网首页
Gson 解析 byte[] 出错

Gson 解析 byte[] 出错

作者: 郑小才 | 来源:发表于2021-03-26 20:42 被阅读0次

增加byte[]的adapter类

Gson mGson = new GsonBuilder().registerTypeHierarchyAdapter(byte[].class, new ByteArrayToBase64TypeAdapter()).create();

//增加byte[]的adapter类
 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));
        }
    }

相关文章

网友评论

      本文标题:Gson 解析 byte[] 出错

      本文链接:https://www.haomeiwen.com/subject/bpeshltx.html