SamuGG
SamuGG

Reputation: 479

Deserialize protobuf java array

Supose I have a WCF service which sends List serialized with protobuf-net. That function returns byte[], application/octet-stream.

Now, in my java application I have compiled MyClass.proto into MyClass.java and get from http the data sent before.

To deserialize 1 object I shall use

MyClass MyObject = MyClass.parseFrom(http_input_stream);

But what shall I use if an array comes...?

Upvotes: 2

Views: 13160

Answers (2)

SamuGG
SamuGG

Reputation: 479

Well I ended up by creating another proto message

message MyClassCollection {
    repeated MyClass = 1;
}

Then compile it into java class and deserialize the incoming array like

MyClassCollection MyObjects = MyClassCollection.parseFrom(http_input_stream);

Upvotes: 2

Marc Gravell
Marc Gravell

Reputation: 1062780

A List<MyClass> is actually serialized as a sequence of MyClass objects, each with a standard field-header of 1. I don't know the java API backwards: if it has a "read a sequence of items" API then: use that. However, a trusty fallback is the following, entirely compatible ".proto" fragment:

message Foo {
    repeated MyClass items = 1;
}

Load the data as a Foo (you might want to rename that...) and: job done.

Upvotes: 2

Related Questions