Converting JSON between string and byte[] with GSON

后端 未结 3 1042
情歌与酒
情歌与酒 2020-12-10 05:16

I am using hibernate to map objects to the database. A client (an iOS app) sends me particular objects in JSON format which I convert to their true representation using the

相关标签:
3条回答
  • 2020-12-10 05:33

    From some blog for future reference, incase the link is not available, atleast users can refer here.

    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;
    
    import java.lang.reflect.Type;
    import java.util.Date;
    
    public class GsonHelper {
        public static final Gson customGson = new GsonBuilder()
                .registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
                    @Override
                    public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
                    return new Date(json.getAsLong());
                    }
                })
                .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));
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-10 05:35

    You can simply take the photo as String in POJO , and in Setter method convert String to byte[] and return byte[] in Getter method

    @Entity
    @Table(name = "PersonalCard")
    public class PersonalCard implements Card
    {
    
        @Id @GeneratedValue
        @Column(name = "id")
        private int id;
    
        @OneToOne
        @JoinColumn(name="userid")
        private int userid;
    
        @Column(name = "homephonenumber")
        protected String homeContactNumber;
    
        @Column(name = "mobilephonenumber")
        protected String mobileContactNumber;
    
        @Column(name = "photo")
        private byte[] optionalImage;
    
        @Column(name = "address")
        private String address;
    
        @Column
        byte[] optionalImage;
    
        public byte[] getOptionalImage()
        {
            return optionalImage;
        }
    
        public void setOptionalImage(String s)
        {
            this.optionalImage= s.getBytes();
        }
    }
    
    0 讨论(0)
  • 2020-12-10 05:45

    You can use this adapter to serialize and deserialize byte arrays in base64. Here's the content.

       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));
            }
        }
    

    Credit to the author Ori Peleg.

    0 讨论(0)
提交回复
热议问题