0

I need to serialize an Object in java. Currently I'm doing it by using this code, which does:

  1. ObjectOutputStream.writeObject(obj);
  2. Base64 encode the obj

And reversing the process.

It works fine for primitive types inside the class, such as long and int.

However, all String objects inside that object become null. I'd need them to be included as well. Is there any way to do that?

Edit: code I'm using

// Modified from source: https://stackoverflow.com/questions/134492/how-to-serialize-an-object-into-a-string
public static Object FromString( String s )
{
    Object o = null;
    try {
        byte[] data = Base64.getDecoder().decode(s);
        ObjectInputStream ois = new ObjectInputStream(
                new ByteArrayInputStream(data));
        o = ois.readObject();
        ois.close();
    }
    catch(Exception e)
    {
        System.out.println(e);
    }
    return o;
}

// Modified from source: https://stackoverflow.com/questions/134492/how-to-serialize-an-object-into-a-string
public static String ToString( Serializable o )
{
    ByteArrayOutputStream baos = null;
    try {
        baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(o);
        oos.close();
    }
    catch(Exception e)
    {
        System.out.println(e);
    }

    return Base64.getEncoder().encodeToString(baos.toByteArray());
}

EDIT: Pojo

public class SignedTimestamp implements Serializable {

    private Long obj;
    private byte[] signature;
    private String signatureAsAString;
}

Output on the other application (receiving the data and deserializing):

obj = 1494609033621;

signature = null;

signatureAsAString = null;

Community
  • 1
  • 1
Kevin Van Ryckegem
  • 1,915
  • 3
  • 28
  • 55

2 Answers2

1

I dont see any problem in that code. Please post your code so that we will dig further.

tried from my end:

Encoded serialized version rO0ABXNyAAhFbXBsb3llZTLR4JLRYAw9AgAESQAGbnVtYmVyTAAHYWRkcmVzc3QAEkxqYXZhL2xhbmcvU3RyaW5nO0wADWFkZHJlc3NPYmplY3R0AAlMQWRkcmVzcztMAARuYW1lcQB+AAF4cAAAAGV0ABlQaG9ra2EgS3VhbiwgQW1iZWh0YSBQZWVyc3IAB0FkZHJlc3MkcEtPHXHTqQIAAUwACGFkZHJMaW5lcQB+AAF4cHEAfgAEdAAJUmV5YW4gQWxp

Reconstituted object Employee [name=Reyan Ali, address=Phokka Kuan, Ambehta Peer, number=101, addressObject=Address [addrLine=Phokka Kuan, Ambehta Peer]]

Baskar Felix
  • 141
  • 2
  • 4
1

I have just tested your code, and it's working okay.

I've created a source named SignedTimeStamp.java:

import java.io.*;

public class SignedTimestamp implements Serializable {
    private Long obj;
    private byte[] signature;
    private String signatureAsAString;

    public SignedTimestamp(Long obj, byte[] signature, String signatureAsAString) {
        this.obj = obj;
        this.signature = signature;
        this.signatureAsAString = signatureAsAString;
    }

    public Long getObj() {
        return this.obj;
    }

    public byte[] getSignature() {
        return this.signature;
    }

    public String getSignatureAsAString() {
        return this.signatureAsAString;
    }

}

And declared another called Serializables.java, containing your code: import java.io.; import java.util.;

public class Serializables {
    public static Object FromString(String s) {
        Object o = null;
        try {
            byte[] data = Base64.getDecoder().decode(s);
            ObjectInputStream ois = new ObjectInputStream(
                    new ByteArrayInputStream(data));
            o = ois.readObject();
            ois.close();
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
        return o;
    }

    // Modified from source: http://stackoverflow.com/questions/134492/how-to-serialize-an-object-into-a-string
    public static String ToString( Serializable o ) {
        ByteArrayOutputStream baos = null;
        try {
            baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(o);
            oos.close();
        }
        catch(Exception e)
        {
            System.out.println(e);
        }

        return Base64.getEncoder().encodeToString(baos.toByteArray());
    }
}

Then, I created one main class in Serialize.java:

public class Serialize {

    public static void main(String[] args) throws Exception {
        SignedTimestamp o = new SignedTimestamp(100L, new byte[]{ (byte) 128 }, "Hello, world!");
        System.out.println(Serializables.ToString(o));
    }
}

Which returned me this result:

sh-4.3$ java Serialize
rO0ABXNyAA9TaWduZWRUaW1lc3RhbXCGTHiJ+JenzgIAA0wAA29ianQAEExqYXZhL2xhbmcvTG9uZztbAAlzaWduYXR1cmV0AAJbQkwAEnNpZ25hdHVyZUFzQVN0cmluZ3QAEkxqYXZhL2xhbmcvU3RyaW5nO3hwc3IADmphdmEubGFuZy
5Mb25nO4vkkMyPI98CAAFKAAV2YWx1ZXhyABBqYXZhLmxhbmcuTnVtYmVyhqyVHQuU4IsCAAB4cAAAAAAAAABkdXIAAltCrPMX+AYIVOACAAB4cAAAAAGAdAANSGVsbG8sIHdvcmxkIQ==                                    
sh-4.3$ 

Then, I created another main class in Deserialize.java:

public class Deserialize {

    public static void main(String[] args) throws Exception {
        String serialized = 
            "rO0ABXNyAA9TaWduZWRUaW1lc3RhbXCGTHiJ+JenzgIAA0wAA29ianQAEExqYXZhL2xhbmcvTG9uZztbAAlzaWduYXR1cmV0AAJbQkwAEnNpZ25hdHVyZUFzQVN0cmluZ3QAEkxqYXZhL2xhbmcvU3RyaW5nO3hwc3IADmphdmEubGFuZy" +
            "5Mb25nO4vkkMyPI98CAAFKAAV2YWx1ZXhyABBqYXZhLmxhbmcuTnVtYmVyhqyVHQuU4IsCAAB4cAAAAAAAAABkdXIAAltCrPMX+AYIVOACAAB4cAAAAAGAdAANSGVsbG8sIHdvcmxkIQ==";
        SignedTimestamp o = (SignedTimestamp) Serializables.FromString(serialized);

        System.out.println(o.getObj());
        System.out.println(o.getSignature());
        System.out.println(o.getSignatureAsAString());
    }

}

And it returns this:

sh-4.3$ java Deserialize                                                                                                                                                          
100                                                                                                                                                                               
[B@6bc7c054                                                                                                                                                                       
Hello, world!                                                                                                                                                                     
sh-4.3$ 

So, all in all, the code seems to be working perfectly. How did you test your code? Perhaps the mistake is there.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Haroldo_OK
  • 6,612
  • 3
  • 43
  • 80
  • 1
    I was certain the mistake happened due to the serialization.. However, turns out I forgot to check whether the value is null before the serialization, which it was! Thanks for pointing out all the steps + the fiddle helped! – Kevin Van Ryckegem May 12 '17 at 21:14