Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #26270 > unrolled thread
| Started by | bob smith <bob@coolfone.net> |
|---|---|
| First post | 2013-10-09 20:09 -0700 |
| Last post | 2013-10-10 20:08 -0400 |
| Articles | 6 — 4 participants |
Back to article view | Back to comp.lang.java.programmer
Serializable java object to a string bob smith <bob@coolfone.net> - 2013-10-09 20:09 -0700
Re: Serializable java object to a string Jeff Higgins <jeff@invalid.invalid> - 2013-10-10 02:11 -0400
Re: Serializable java object to a string markspace <markspace@nospam.nospam> - 2013-10-09 23:27 -0700
Re: Serializable java object to a string Arne Vajhøj <arne@vajhoej.dk> - 2013-10-10 19:52 -0400
Re: Serializable java object to a string Arne Vajhøj <arne@vajhoej.dk> - 2013-10-10 20:07 -0400
Re: Serializable java object to a string Arne Vajhøj <arne@vajhoej.dk> - 2013-10-10 20:08 -0400
| From | bob smith <bob@coolfone.net> |
|---|---|
| Date | 2013-10-09 20:09 -0700 |
| Subject | Serializable java object to a string |
| Message-ID | <80380e82-2493-479b-ab13-3fa8bd60aa6b@googlegroups.com> |
What's the easiest way to convert a Serializable java object to a string? Maybe a hex string or base64? Thanks.
[toc] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2013-10-10 02:11 -0400 |
| Message-ID | <l35gd5$2lt$1@dont-email.me> |
| In reply to | #26270 |
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
[toc] | [prev] | [next] | [standalone]
| From | markspace <markspace@nospam.nospam> |
|---|---|
| Date | 2013-10-09 23:27 -0700 |
| Message-ID | <l35hcl$6kh$1@dont-email.me> |
| In reply to | #26270 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2013-10-10 19:52 -0400 |
| Message-ID | <52573dbd$0$292$14726298@news.sunsite.dk> |
| In reply to | #26270 |
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
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2013-10-10 20:07 -0400 |
| Message-ID | <5257414c$0$303$14726298@news.sunsite.dk> |
| In reply to | #26297 |
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
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2013-10-10 20:08 -0400 |
| Message-ID | <52574175$0$303$14726298@news.sunsite.dk> |
| In reply to | #26297 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web