title: How to Deserialize String to Java Object
body: <p>I try to deserialize String to Java Object from a Redis server which is used to store Spring sessions, and I want to deserialize it outside of the Spring framework. I think the Spring Redis serializer is probably using the default charset, UTF-8, to serialize Java Object to String.</p>
  
  <p>The String in Redis:</p>
  
  <blockquote>
    <p>\xAC\xED\x00\x05sr\x00\x0Ejava.lang.Long;\x8B\xE4\x90\xCC\x8F#\xDF\x02\x00\x01J\x00\x05valuexr\x00\x10java.lang.Number\x86\xAC\x95\x1D\x0B\x94\xE0\x8B\x02\x00\x00xp\x00\x00\x00\x00\x00\x00\xD7</p>
  </blockquote>
  
  <p>The method I use:</p>
  
  <pre><code>private static void scanKeys(Jedis jedis, String prefix, String hashKey)
          throws IOException, ClassNotFoundException {
      Set&lt;String&gt; keys = jedis.keys(prefix + "*");
  
      for (String key : keys) {
          if (!key.contains("expir")) {
              Map&lt;String, String&gt; sessionMap = jedis.hgetAll(key);
  
              for (Entry&lt;String, String&gt; entry : sessionMap.entrySet()) {
  
                  if (entry.getValue() != null &amp;&amp; entry.getKey().equals(hashKey)) {
                      System.out.println(entry.getKey());
                      System.out.println(entry.getValue());
  
                      byte[] output = entry.getValue().getBytes(charset);
                      System.out.println(Arrays.toString(output));    
                      try {
                          Long id = (Long) deserialize(output);
                          System.out.println(id);
                      } catch (EOFException e) {
                          break;
                      }
                  }
              }
  
          }
  
      }
  }
  
  public static Object deserialize(byte[] data) throws IOException, ClassNotFoundException {
      ByteArrayInputStream in = new ByteArrayInputStream(data);
      ObjectInputStream is = new ObjectInputStream(in);
      return is.readObject();
  }
  </code></pre>
  
  <p>The error message:</p>
  
  <pre><code>java.io.StreamCorruptedException: invalid stream header: EFBFBDEF
      at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:862)
      at java.io.ObjectInputStream.&lt;init&gt;(ObjectInputStream.java:354)
      at practice.redis.jedis.App.deserialize(App.java:191)
      at practice.redis.jedis.App.scanKeys(App.java:168)
      at practice.redis.jedis.App.main(App.java:77)
  </code></pre>
  
  <p>I know the conversion between byte[] and String with UTF-8 is probably where the problem is, but I still want to ask if anyone knows how to solve this problem without modifying the serialization part.</p>