Serializable java object to a string

Discussion in 'Java' started by bob smith, Oct 10, 2013.

  1. bob smith

    bob smith Guest

    What's the easiest way to convert a Serializable java object to a string?

    Maybe a hex string or base64?

    Thanks.
     
    bob smith, Oct 10, 2013
    #1
    1. Advertising

  2. bob smith

    Jeff Higgins Guest

    On 10/09/2013 11:09 PM, bob smith wrote:
    > What's the easiest way to convert a Serializable java object to a string?
    >
    > Maybe a hex string or base64?
    >

    <http://docs.oracle.com/javase/7/docs/platform/serialization/spec/serialTOC.html>or
    or write a check
     
    Jeff Higgins, Oct 10, 2013
    #2
    1. Advertising

  3. bob smith

    markspace Guest

    On 10/9/2013 8:09 PM, bob smith wrote:
    > What's the easiest way to convert a Serializable java object to a string?
    >
    > Maybe a hex string or base64?
    >
    > Thanks.
    >



    javax.xml.bind.DatatypeConverter contains both hex and base 64
    conversion methods to help you out.
     
    markspace, Oct 10, 2013
    #3
  4. bob smith

    Arne Vajhøj Guest

    On 10/9/2013 11:09 PM, bob smith wrote:
    > What's the easiest way to convert a Serializable java object to a string?
    >
    > Maybe a hex string or base64?


    Write the object to an ObjectOutputStream wrapped around
    a ByteArrayOutputStream, retrieve the byte array and
    convert it to hex or base64.

    Arne
     
    Arne Vajhøj, Oct 11, 2013
    #4
  5. bob smith

    Arne Vajhøj Guest

    On 10/10/2013 7:52 PM, Arne Vajhøj wrote:
    > On 10/9/2013 11:09 PM, bob smith wrote:
    >> What's the easiest way to convert a Serializable java object to a string?
    >>
    >> Maybe a hex string or base64?

    >
    > Write the object to an ObjectOutputStream wrapped around
    > a ByteArrayOutputStream, retrieve the byte array and
    > convert it to hex or base64.


    Example:

    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io_ObjectInputStream;
    import java.io_ObjectOutputStream;
    import java.util.ArrayList;
    import java.util.List;

    import javax.xml.bind.DatatypeConverter;

    public class SerFun {
    public static String anySerialize(Object o) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(o);
    oos.close();
    return DatatypeConverter.printBase64Binary(baos.toByteArray());
    }
    public static Object anyDeserialize(String s) throws IOException,
    ClassNotFoundException {
    ByteArrayInputStream bais = new
    ByteArrayInputStream(DatatypeConverter.parseBase64Binary(s));
    ObjectInputStream ois = new ObjectInputStream(bais);
    Object o = ois.readObject();
    ois.close();
    return o;
    }
    public static void main(String[] args) throws Exception {
    List<Data> lst = new ArrayList<Data>();
    lst.add(new Data(1, "A"));
    lst.add(new Data(2, "BB"));
    lst.add(new Data(3, "CCC"));
    System.out.println(lst);
    String s = anySerialize(lst);
    @SuppressWarnings("unchecked")
    List<Data> lst2 = (List<Data>)anyDeserialize(s);
    System.out.println(lst2);
    }
    }

    Arne
     
    Arne Vajhøj, Oct 11, 2013
    #5
  6. bob smith

    Arne Vajhøj Guest

    On 10/10/2013 7:52 PM, Arne Vajhøj wrote:
    > On 10/9/2013 11:09 PM, bob smith wrote:
    >> What's the easiest way to convert a Serializable java object to a string?
    >>
    >> Maybe a hex string or base64?

    >
    > Write the object to an ObjectOutputStream wrapped around
    > a ByteArrayOutputStream, retrieve the byte array and
    > convert it to hex or base64.


    I will recommend XML serialization over binary serialization if
    the serialized version wil be persisted!

    Arne
     
    Arne Vajhøj, Oct 11, 2013
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Line
    Replies:
    8
    Views:
    681
    Tony Morris
    Jan 14, 2004
  2. Christian Schuhegger
    Replies:
    4
    Views:
    806
    Christian Schuhegger
    Aug 11, 2004
  3. James Yong
    Replies:
    3
    Views:
    1,842
    John C. Bollinger
    Aug 24, 2005
  4. Jimmy
    Replies:
    4
    Views:
    652
    Thomas Hawtin
    Aug 8, 2007
  5. Replies:
    8
    Views:
    3,132
Loading...

Share This Page