Reliably convert any object to String and then back again

后端 未结 7 755
名媛妹妹
名媛妹妹 2020-12-02 13:35

Is there a reliable way to convert any object to a String and then back again to the same Object? I\'ve seen some examples of people converting them using toString()

相关标签:
7条回答
  • 2020-12-02 13:43

    One way is to use JSON. For implementation specific in Java, the answer might be given in this post:

    java code corresponding to Newtonsoft.Json.JsonConvert.SerializeObject(Object source,Newtonsoft.Json.JsonSerializerSettings()) in .net?

    Using JSON is reliable enough that it's used for web application development (Ajax).

    0 讨论(0)
  • 2020-12-02 13:51

    This is the code:

    try {
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        ObjectOutputStream so = new ObjectOutputStream(bo);
        so.writeObject(stringList);
        so.flush();
        redisString = new String(Base64.encode(bo.toByteArray()));       
    } 
    catch (Exception e) {
        e.printStackTrace();
    }
    
    try {
        byte b[] = Base64.decode(redisString.getBytes()); 
        ByteArrayInputStream bi = new ByteArrayInputStream(b);
        ObjectInputStream si = new ObjectInputStream(bi);
        List<String> stringList2 = (List<String>)si.readObject();
        System.out.println(stringList2.get(1));          
    } 
    catch (Exception e) {
        e.printStackTrace();         
    }
    
    0 讨论(0)
  • 2020-12-02 13:52

    You can use SerializationUtils from org.apache.commons.
    It provides the methods serialize and deserialize

    0 讨论(0)
  • 2020-12-02 13:57

    Yes, it is Serialization You can use, ObjectInputStream.readObject and ObjectOutputStream.writeObject. Please see below example:

    MyClass myClass = new MyClass();
    FileOutputStream fileStream = new FileOutputStream("myObjectFile.txt");
    ObjectOutputStream os = new ObjectOutputStream(fileStream);
    os.writeObject(os);
    os.close();
    
    FileInputStream fileInStream = new FileInputStream("myObjectFile.txt");
    ObjectInputStream ois = new ObjectInputStream(fileInStream);
    MyClass myClass2 = ois.readObject();
    ois.close();
    
    0 讨论(0)
  • 2020-12-02 14:02

    Yes, it is called serialization!

     String serializedObject = "";
    
     // serialize the object
     try {
         ByteArrayOutputStream bo = new ByteArrayOutputStream();
         ObjectOutputStream so = new ObjectOutputStream(bo);
         so.writeObject(myObject);
         so.flush();
         serializedObject = bo.toString();
     } catch (Exception e) {
         System.out.println(e);
     }
    
     // deserialize the object
     try {
         byte b[] = serializedObject.getBytes(); 
         ByteArrayInputStream bi = new ByteArrayInputStream(b);
         ObjectInputStream si = new ObjectInputStream(bi);
         MyObject obj = (MyObject) si.readObject();
     } catch (Exception e) {
         System.out.println(e);
     }
    
    0 讨论(0)
  • 2020-12-02 14:03

    Serialize to byte array, convert to Base64. Then decode Base64 back to byte array and deserialize.

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