c# - Issue deserializing (protocolBuffer) serialized data using protobuf-net -
i serialized data using protobuf-net, , able deser same in c#.
putting c# dummy w#
var file = file.create("animal.bin"); //creating msg - fill data animal.id = "1"; animal.name = "rat"; animal.host = "cheetha"; protobuf.serializer.serializewithlengthprefix(file, animal, prefixstyle.base128, 1); animal.id = "2"; animal.name = "cat"; animal.host = "cheetha"; protobuf.serializer.serializewithlengthprefix(file, animal, prefixstyle.base128, 1); .... animal.id = "4"; animal.name = "cheetha"; animal.host = "cheetha"; protobuf.serializer.serializewithlengthprefix(file, animal, prefixstyle.base128, 1); //done creating msg file.close();
so far good... no issues here. when try deserialize same in c++ using protocol-buffer, unable correct data
cpp code...
google_protobuf_verify_version; string fpath = "animal.bin"; fstream input(fpath, ios::in | ios::binary); if (!input) { cerr << "failed open " << fpath << endl; return false; } zerocopyinputstream *raw_in = new istreaminputstream(&input); codedinputstream *coded_in = new codedinputstream(raw_in); google::protobuf::uint32 n; std::string tmpstr; animal::animalinfo animallist; coded_in->readvarint32(&n); cout << "# " << n << endl; //output: #10 coded_in->readraw(&tmpstr,n); //tmpstr shows data >>1..rat..ch animallist.parsefromarray(&tmpstr,1);//not sure if correct usage?
i sure making mistake not able understand whats wrong.... have read , reread lots of post on dont see whats still wrong
using protocol buffer2.5, protobuf-netr622, visual studio 2010
i think you're mismatching headers; length-prefix and field-header (of field 1) two "varint"s; first "varint" decimal 10 (10 means: field 1, length-prefixed). second "varint" tells length of next data. - if want decode manually call readvarint32
second time. i'm not familiar readraw
, if second parameter number of bytes read, it goes there, i.e.
coded_in->readvarint32(&n); // field header // assert: n === 10 coded_in->readvarint32(&n); // length coded_in->readraw(&tmpstr,n);
alternatively, use wrapper object - i.e.
message animals { repeated animal items = 1; }
and deserialize as instance of animals
- uses exact same layout. difference here load all items in 1 go - might problematic if reading long streams.
another alternative be: don't add field-header:
serializer.serializewithlengthprefix(file, animal, prefixstyle.base128, 0);
then read 1 "varint":
coded_in->readvarint32(&n); // length coded_in->readraw(&tmpstr,n);
Comments
Post a Comment