0

I have my Field and a byte array or a String (I can return either from Jedis) What would be a good way of either Serializing/Deserializing to and from this field into a human readable value?

I know there's a way... I'm just not seeing it. The other thing I need to mention is that this needs to work for both Serializable Java Objects and Primitives

My current attempt:

protected void loadField(Jedis jedis, String key, Field field) {
    String value = jedis.get(key);
    Class clazz = field.getClass();
    try {
        if (Serializable.class.isAssignableFrom(clazz)) {
            ByteArrayInputStream bis = new ByteArrayInputStream(value.getBytes());
            ObjectInput in = new ObjectInputStream(bis);
            field.set(this, in.readObject());
        } else if (clazz.isAssignableFrom(Integer.TYPE)) {
            field.set(this, Integer.parseInt(value));
        } else if (clazz.isAssignableFrom(Double.TYPE)) {
            field.set(this, Double.parseDouble(value));
        } else if (clazz.isAssignableFrom(Long.TYPE)) {
            field.set(this, Long.parseLong(value));
        } else if (clazz.isAssignableFrom(Short.TYPE)) {
            field.set(this, Short.parseShort(value));
        } else if (clazz.isAssignableFrom(Float.TYPE)) {
            field.set(this, Float.parseFloat(value));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

I have yet to set data into the Jedis KVS and as such, this can completely change. Any ideas?

Thomas Nairn
  • 1,186
  • 8
  • 34

0 Answers0