kshchepanovskyi / protostuff-googlecode-exported

Automatically exported from code.google.com/p/protostuff
Apache License 2.0
0 stars 1 forks source link

Timestamp cannot be serialized/deserialized correctly #123

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create a simple object which has timestamp Values
2. When serializing the object the timestamp field is written wrongly 
3. Then when trying to deserialize an exception is produced

What is the expected output? What do you see instead?

Serialized object:
{"logDate":{"nanos":917000000},"msgTxt":"1","timezone":"Europe/Moscow"}

When trying to deserialize the following error is produced:
Expected token: [ but was START_OBJECT on message: com.test.MyLog
...
Caused by: com.dyuproject.protostuff.JsonInputException: Expected token: [ but 
was START_OBJECT on message: com.test.MyLog
    at com.dyuproject.protostuff.JsonIOUtil.parseListFrom(JsonIOUtil.java:639)

What version of the product are you using? On what operating system?
1.0.5 on win7

Please provide any additional information below.

I dynamically create a schema:
        Schema schema = RuntimeSchema.getSchema(clazz);

parse with:
        JsonIOUtil.toByteArray(obj, schema, false, buf));        

deserialize with: 

        objectList = JsonIOUtil.parseListFrom(in, schema, false, buffer);

I think the problem is that the timestamp logdate i have is not serialized 
correctly

Please advice 

Thank you

Original issue reported on code.google.com by thomas.b...@gmail.com on 21 May 2012 at 9:00

GoogleCodeExporter commented 9 years ago
Shouldn't you be using JsonIOUtil.mergeFrom instead of JsonIOUtil.parseListFrom?
Can you post the fully qualified class name of the timestamp class?

If you can post a simple example that would be great.

Original comment by david.yu...@gmail.com on 21 May 2012 at 11:49

GoogleCodeExporter commented 9 years ago
Hi, 

thank you for your reply. I think the issue is when serializing because when i 
see the generated file the logDate is as described above.

The object I use is fairly simple:

package com.test;

import java.io.Serializable;
import java.sql.Timestamp;

public class MyLog {
    private static final long serialVersionUID = 3802847070993248978L;

    private Timestamp logDate;

    private String msgTxt;

    private String timezone;

    public MyLog() {
    }

    public Timestamp getLogDate() {
        return logDate;
    }

    public void setLogDate(Timestamp theLogDate) {
        logDate = theLogDate;
    }

    public String getMsgTxt() {
        return msgTxt;
    }

    public void setMsgTxt(String theMsgTxt) {
        msgTxt = theMsgTxt;
    }
    public String getTimezone() {
        return timezone;
    }

    public void setTimezone(String theTimezone) {
        timezone = theTimezone;
    }

}

Original comment by thomas.b...@gmail.com on 21 May 2012 at 12:00

GoogleCodeExporter commented 9 years ago
The problem was that you are using JsonIOUtil.parseListFrom, when you should've 
been using JsonIOUtil.mergeFrom.

Also, there are no built-in serializers for java.sql.Timestamp (its a scalar 
value none-the-less).

I've attached a sample which basically solves your problem (using 1.0.7 
delegate)

Original comment by david.yu...@gmail.com on 21 May 2012 at 12:20

Attachments:

GoogleCodeExporter commented 9 years ago
Thank you for your reply,

I tried your solution and worked fine :) 
I was using parseListFrom because it gives me back the object list instead of 
having a for (each line) loop. with mergeFrom. Or there is an alternative way 
of using it to do something like:

objectList = JsonIOUtil.parseListFrom(in, schema, true, buffer); 

JsonIOUtil.mergeFrom(in, objectList, schema, true, buffer); 
but i do not serialize list i serialize my MyLog object and append it to the 
file one by one.

Thank you again you are helping me a lot :)

Original comment by thomas.b...@gmail.com on 21 May 2012 at 1:32

GoogleCodeExporter commented 9 years ago
If you want to make use of protostuff pipes (transcoding), then you'll want to 
do:
class Wrapper
{
    List<MyLog> logs;
}

And use JsonIOUtil.writeTo and JsonIOUtil.mergeFrom.

If not then:
// serialize
List<MyLog> logs = getLogs();
JsonIOUtil.writeListTo(out, logs, schema, false, buffer);

// produces:
// [{"logDate":1337609415306,"msgTxt":"1","timezone":"Europe/Moscow"}]

// deserialize
List<MyLog> parsedLogs = JsonIOUtil.parseListFrom(in, schema, false, buffer);

Original comment by david.yu...@gmail.com on 21 May 2012 at 2:12