Why I encounter Exception when using RuntimeSchema?

110 views
Skip to first unread message

DarrenWang

unread,
Nov 1, 2010, 2:45:22 AM11/1/10
to protostuff
I defined a simple POJO:


public class ProtoVO {
private int id;
private String name;
private Date timestamp;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Date getTimestamp() {
return timestamp;
}

public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}

@Override
public String toString() {
return "ProtoVO [id=" + id + ", name=" + name + ", timestamp="
+ timestamp + "]";
}

}

and try to run a simple sample:
Schema<ProtoVO> schema = RuntimeSchema.getSchema(ProtoVO.class);
// Schema<ProtoVO> schema = new ProtoVOSchema();

LinkedBuffer buffer = LinkedBuffer.allocate(1024);

ProtoVO vo = new ProtoVO();
vo.setId(11);
vo.setName("darren");
vo.setTimestamp(new Date());

byte[] bytes = ProtostuffIOUtil.toByteArray(vo, schema,
buffer);

ProtoVO v = new ProtoVO();
ProtostuffIOUtil.mergeFrom(bytes, v, schema);

System.out.println(v);

But I got exception from the 1st line:

Exception in thread "main" java.lang.RuntimeException: All fields are
either transient/static. Note that Map fields are excluded. Two
dimensional array fields are excluded. Collection fields whose
generic type is a collection or another generic type, are excluded.
at
com.dyuproject.protostuff.runtime.RuntimeSchema.createFrom(RuntimeSchema.java:
138)
at
com.dyuproject.protostuff.runtime.RuntimeSchema.createFrom(RuntimeSchema.java:
85)
at com.dyuproject.protostuff.runtime.RuntimeSchema
$Lazy.getSchema(RuntimeSchema.java:283)
at
com.dyuproject.protostuff.runtime.RuntimeSchema.getSchema(RuntimeSchema.java:
77)
at com.dyuproject.protostuff.runtime.RuntimeFieldFactory
$14$1.<init>(RuntimeFieldFactory.java:1119)
at com.dyuproject.protostuff.runtime.RuntimeFieldFactory
$14.create(RuntimeFieldFactory.java:1117)
at
com.dyuproject.protostuff.runtime.RuntimeSchema.createFrom(RuntimeSchema.java:
127)
at
com.dyuproject.protostuff.runtime.RuntimeSchema.createFrom(RuntimeSchema.java:
85)
at com.dyuproject.protostuff.runtime.RuntimeSchema
$Lazy.getSchema(RuntimeSchema.java:283)
at
com.dyuproject.protostuff.runtime.RuntimeSchema.getSchema(RuntimeSchema.java:
77)
at
cn.spring21.sandbox.protostuff.ProtostuffMarshaller.main(ProtostuffMarshaller.java:
16)

Did I miss sth?

DarrenWang

unread,
Nov 1, 2010, 2:48:33 AM11/1/10
to protostuff
And when I try to write a custom schema for this pojo, some exceptions
on IO were raised too, can you someone explain the difference between
Int32, SInt32, Fixed32 types?

public class ProtoVOSchema implements Schema<ProtoVO> {
private static final Map<String, Integer> fieldMapping = new
HashMap<String, Integer>() {
private
static final long serialVersionUID = 1771916786472684230L;
{

put("id", 1);

put("name", 2);

put("timestamp", 3);
}
};

public String getFieldName(int arg0) {
for (Map.Entry<String, Integer> e : fieldMapping.entrySet()) {
if (e.getValue().intValue() == arg0) {
return e.getKey();
}
}
return null;
}

public int getFieldNumber(String arg0) {
return fieldMapping.get(arg0) == null ? 0 :
fieldMapping.get(arg0).intValue();
}

public boolean isInitialized(ProtoVO arg0) {
return arg0.getId() > 0;
}

public void mergeFrom(Input in, ProtoVO vo) throws IOException {
vo.setId(in.readSInt32());
vo.setName(in.readString());
vo.setTimestamp(new Date(in.readSInt64()));
}

public String messageFullName() {
return "protoVO marshalling";
}

public String messageName() {
return "protoVo";
}

public ProtoVO newMessage() {
return new ProtoVO();
}

public Class<ProtoVO> typeClass() {
return ProtoVO.class;
}

public void writeTo(Output out, ProtoVO vo) throws IOException {
out.writeSInt32(1, vo.getId(), false);
out.writeString(2, vo.getName(), false);
out.writeSInt64(3, vo.getTimestamp().getTime(), false);
> cn.spring21.sandbox.protostuff.ProtostuffMarshaller.main(ProtostuffMarshall er.java:

David Yu

unread,
Nov 1, 2010, 2:52:03 AM11/1/10
to proto...@googlegroups.com
There's no built-in date converter in the runtime schema.
You could use a long timestamp.

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

David Yu

unread,
Nov 1, 2010, 3:05:31 AM11/1/10
to proto...@googlegroups.com
On Mon, Nov 1, 2010 at 2:48 PM, DarrenWang <fujoh...@gmail.com> wrote:
And when I try to write a custom schema for this pojo, some exceptions
on IO were raised too, can you someone explain the difference between
Int32, SInt32, Fixed32 types?
In java-land, they are typically the same ... the difference is how they are serialized on the wire. 
For the date timestamp, use Fixed64.

DarrenWang

unread,
Nov 1, 2010, 3:14:25 AM11/1/10
to protostuff
Thanks David,

One more question, if I serialize the object this way:

public void writeTo(Output out, ProtoVO vo) throws IOException {
out.writeSInt32(1, vo.getId(), false);
out.writeString(2, vo.getName(), false);
out.writeSInt64(3, vo.getTimestamp().getTime(), false);
}

can I de-serialize it in the order as it's serialized?

public void mergeFrom(Input in, ProtoVO vo) throws IOException {
vo.setId(in.readSInt32());
vo.setName(in.readString());
vo.setTimestamp(new Date(in.readSInt64()));
}

On Nov 1, 3:05 pm, David Yu <david.yu....@gmail.com> wrote:
> On Mon, Nov 1, 2010 at 2:48 PM, DarrenWang <fujohnw...@gmail.com> wrote:
> > And when I try to write a custom schema for this pojo, some exceptions
> > on IO were raised too, can you someone explain the difference between
> > Int32, SInt32, Fixed32 types?
>
> Seehttp://code.google.com/apis/protocolbuffers/docs/proto.html#scalar

David Yu

unread,
Nov 1, 2010, 3:27:46 AM11/1/10
to proto...@googlegroups.com
On Mon, Nov 1, 2010 at 3:14 PM, DarrenWang <fujoh...@gmail.com> wrote:
Thanks David,

One more question,  if I serialize the object this way:

   public void writeTo(Output out, ProtoVO vo) throws IOException {
       out.writeSInt32(1, vo.getId(), false);
       out.writeString(2, vo.getName(), false);
       out.writeSInt64(3, vo.getTimestamp().getTime(), false);
   }

can I de-serialize it in the order as it's serialized?

   public void mergeFrom(Input in, ProtoVO vo) throws IOException {
       vo.setId(in.readSInt32());
       vo.setName(in.readString());
       vo.setTimestamp(new Date(in.readSInt64()));
   }
Deserialize it this way:

    public void mergeFrom(Input in, ProtoVO vo) throws IOException 
    {
        while(true)
        {
            int number = in.readFieldNumber(this);
            switch(number)
            {
                case 0:
                    return;
                case 1:
                    vo.id = in.readSInt32();
                    break;
                case 2:
                    vo.name = in.readString();
                    break;
                case 3:
                    vo.timestamp = new Date(in.readSInt64());
                    break;
                default:
                    in.handleUnknownField(number, this);
            }
        }
    }

You don't assume its order ... especially when you're deserializing from json where the fields aren't exactly ordered.

DarrenWang

unread,
Nov 1, 2010, 3:31:09 AM11/1/10
to protostuff
If I don't misunderstand, the method "in.handleUnknownField" will
break the loop or the upside caller will pass a 0 field number?

On Nov 1, 3:27 pm, David Yu <david.yu....@gmail.com> wrote:

David Yu

unread,
Nov 1, 2010, 3:35:34 AM11/1/10
to proto...@googlegroups.com
On Mon, Nov 1, 2010 at 3:31 PM, DarrenWang <fujoh...@gmail.com> wrote:
If I don't misunderstand,  the method "in.handleUnknownField" will
break the loop or the upside caller will pass a 0 field number?
The "while(true)" loop will still continue when it encounters an unknown field (it basically skips it).. and when there are no more fields to deserialize, you will get 0 from input.readFieldNumber(schema) ... 

DarrenWang

unread,
Nov 1, 2010, 3:37:23 AM11/1/10
to protostuff
thank you, man , I got it, it works now, wow~

On Nov 1, 3:35 pm, David Yu <david.yu....@gmail.com> wrote:

David Yu

unread,
Nov 1, 2010, 3:41:34 AM11/1/10
to proto...@googlegroups.com
On Mon, Nov 1, 2010 at 3:37 PM, DarrenWang <fujoh...@gmail.com> wrote:
thank you, man , I got it, it works now, wow~
Cool.  For schema customization, there's a template you can reference at http://code.google.com/p/protostuff/wiki/Schema

Cheers

DarrenWang

unread,
Nov 1, 2010, 8:42:19 AM11/1/10
to protostuff
with your explanation, I can understand the code very well now, :-)

On Nov 1, 3:41 pm, David Yu <david.yu....@gmail.com> wrote:
> On Mon, Nov 1, 2010 at 3:37 PM, DarrenWang <fujohnw...@gmail.com> wrote:
> > thank you, man , I got it, it works now, wow~
>
> Cool.  For schema customization, there's a template you can reference athttp://code.google.com/p/protostuff/wiki/Schema
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages