My problem is to serialize protobuf data in C++ and deserialize the
data in Java probably. In fact I get **InvalidProtobufferException**
in Java.
Here is my minimal example:
My protobuf file looks like this:
option java_outer_classname = "NameProtos";
message Name {
required string name = 1;
}
Which I compile with:
protoc -I=. --cpp_out=../cpp/ name.proto
and
protoc -I=. --java_out=../java/ name.proto
In C++ I create the name object this way:
Name name;
name.set_name("platzhirsch");
socket.send(name.SerializeAsString);
In Java I read from the socket, until the socket is closed
(socket.send closes the connection, after it entirely wrote the string
passed, so I guess here is no when stop reading issue, isn't there?).
In Java I make the following call in order to deserialize:
`NameProtos.Name name =
NameProtos.Name.parseFrom(ByteString.copyFromUtf8(received))`;
However I always get **InvalidProtocolBufferException**
I have no idea, the received strings are not empty though, they look
like this:
[SPACE]platzhirsch[A Character which cannot be displayed probably]
These character which I cannot read, I also get, when using other
fields like int32. I guess there are just encoded into the string.
--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To post to this group, send email to prot...@googlegroups.com.
To unsubscribe from this group, send email to protobuf+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.
Right! I think I refactored the whole writing and reading, still I am
stuck. However by this I get this Exception:
com.google.protobuf.UninitializedMessageException: Message missing
required fields: name
In C++:
boost::asio::streambuf b;
Name name;
name.set_name("platzhirsch");
std::ostream os(&b);
ZeroCopyOutputStream *raw_output = new OstreamOutputStream(&os);
CodedOutputStream *coded_output = new CodedOutputStream(raw_output);
coded_output->WriteLittleEndian32(name.ByteSize());
name.SerializeToCodedStream(coded_output);
socket.send(b);
In Java:
NameProtos.Name name =
NameProtos.Name.parseDelimitedFrom(socket.getInputStream());
System.out.println(name.newBuilder().build().toString());
---
I can't find out where the problem might be.
--
--
--