简体   繁体   中英

Deserialize Map<byte[], byte[]> in Java

I am using jedis to get data from redis .

jedis.hgetall returns Map<byte[], byte[]> which I want to deserialize.

For other redis operations(like hget) I am able to deserialize easily with SerializationUtils.deserialize(byte[] objectData) , as they return byte[] .

Any idea how do I deserialize Map of byte[] ?

You could find unserialize method you want within the below menthods:

 import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Set;

    public class SerializeUtil {

        /**
         * unserialize byte[]
         * @param bytes
         * @return Object
         */
        public static Object unserialize(final byte[] bytes) {
            ByteArrayInputStream bais = null;
            try {
                bais = new ByteArrayInputStream(bytes);
                ObjectInputStream ois = new ObjectInputStream(bais);
                return ois.readObject();
            } catch (Exception e) {

            } 
            return null;
        }

        /**
         * unserialize hash Map<byte[], byte[]>
         * @param hash
         * @return Map<Object, Object>
         */
        public static Map<Object, Object> unserializehmbb2moo(final Map<byte[], byte[]> hash) {
            Map<Object, Object> result = new HashMap<Object, Object>();
            try {
                Set<byte[]> keys = hash.keySet();
                if (keys != null && keys.size() > 0) {
                    for (byte[] key : keys) {
                        result.put(unserialize(key), unserialize(hash.get(key)));
                    }
                }
            } catch (Exception e) {
            }
            return result;
        }


        /**
         * unserialize hash Map<byte[], byte[]>
         * @param hash
         * @return Map<String, Object>
         */
        public static Map<String, Object> unserializehmbb2mso(final Map<byte[], byte[]> hash) {
            Map<String, Object> result = new HashMap<String, Object>();
            try {
                Set<byte[]> keys = hash.keySet();
                if (keys != null && keys.size() > 0) {
                    for (byte[] key : keys) {
                        result.put(unserialize(key).toString(), unserialize(hash.get(key)));
                    }
                }
            } catch (Exception e) {
            }
            return result;
        }
    }

You could do it much easier with Redis based framework - Redisson :

Config config = new Confing();
config.setCodec(new SerializationCodec());
RedissonClient redisson = Redisson.create(config);

Map<MyKey, MyObject> map = redisson.getMap("myMap");
// both key and value objects are serialized transparently
map.put(new MyKey(), new MyObject());

// value object is deserialized transparently
map.get(new MyKey());

It offers big variations of codecs like Jackson JSON , Avro , Amazon Ion , Smile , CBOR , MsgPack , Kryo , FST , LZ4 , Snappy and JDK Serialization and allows you to concentrate on business logic development rather than serialization and connection handling with Jedis...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM