serializing java.sql.Timestamp returns wrong value at deserialization.

539 views
Skip to first unread message

prateek sharma

unread,
Jan 29, 2014, 7:45:46 AM1/29/14
to proto...@googlegroups.com
Hi all,
Serializing java.sql.Timestamp is returning wrong value at the time of deserialization.
My use case is very simple. I have one pojo which has few properties and one property of type java.sql.Timestamp. I am setting today's date(i.e. 29-Jan-2014) in this field.
I am using RuntimeSchema to serialize object and I am using GraphIOUtil.writeTo(...) method to serialize this object.
Problem is that when I deserialize it, the resultant object has that timestamp field populated with a date of 3-Jan-1970. :(

Please help.

Thanks,
Pratz

David Yu

unread,
Jan 29, 2014, 8:35:12 AM1/29/14
to protostuff
Note sure why that's happending.  Perhaps the internal field is transient.
You can instead use delegates (more efficient for these scalar types).

Here's the code:
    public static final Delegate<Timestamp> TIMESTAMP_DELEGATE = new Delegate<Timestamp>()
    {

        public FieldType getFieldType()
        {
            return FieldType.FIXED64;
        }

        public Class<?> typeClass()
        {
            return Timestamp.class;
        }
        
        public Timestamp readFrom(Input input) throws IOException
        {
            return new Timestamp(input.readFixed64());
        }

        public void writeTo(Output output, int number, Timestamp value, boolean repeated)
                throws IOException
        {
            output.writeFixed64(number, value.getTime(), repeated);
        }

        public void transfer(Pipe pipe, Input input, Output output, int number, 
                boolean repeated) throws IOException
        {
            output.writeFixed64(number, input.readFixed64(), repeated);
        }
    };

 

Thanks,
Pratz

--
You received this message because you are subscribed to the Google Groups "protostuff" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protostuff+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
When the cat is away, the mouse is alone.
- David Yu

prateek sharma

unread,
Jan 31, 2014, 7:31:34 AM1/31/14
to proto...@googlegroups.com
Bingo... It worked. :) But why not to make it by default configuration in protostuff itself. I am using protostuff 1.0.7.

Thanks,
Pratz

David Yu

unread,
Jan 31, 2014, 7:55:02 AM1/31/14
to protostuff



On Fri, Jan 31, 2014 at 8:31 PM, prateek sharma <meprat...@gmail.com> wrote:
Bingo... It worked. :)
Great. 
But why not to make it by default configuration in protostuff itself.
There are too many jdk classes (java.util.*, java.sql.*, javax.*, etc) for them to be included in the core.
A separated module (delegates) is the best place for them.

As a reference, the built-in scalar types, other than String/byte[]/primitive types, are java.util.Date, BigDecimal and BigInteger.

I am using protostuff 1.0.7.

Thanks,
Pratz

On Wednesday, 29 January 2014 18:15:46 UTC+5:30, prateek sharma wrote:
Hi all,
Serializing java.sql.Timestamp is returning wrong value at the time of deserialization.
My use case is very simple. I have one pojo which has few properties and one property of type java.sql.Timestamp. I am setting today's date(i.e. 29-Jan-2014) in this field.
I am using RuntimeSchema to serialize object and I am using GraphIOUtil.writeTo(...) method to serialize this object.
Problem is that when I deserialize it, the resultant object has that timestamp field populated with a date of 3-Jan-1970. :(

Please help.

Thanks,
Pratz

--
You received this message because you are subscribed to the Google Groups "protostuff" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protostuff+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages