java protobuf c,Protobuf Java到C ++的序列化[二进制]

I have a program that serializes data with Protobuf in Java, by writing binary data in a byte[] Array and then saving it in ".txt" file. I am receiving that data on the C++ side in a stringstream. Now I want to parse that binary data with C++, but the Protobuf-Parsing-Method "parseFromString()" doesn't work! The fields from my Test Message are not set. I wrote a little test for that and I can show you some code:

Java Serialization

byte[] s = test.build().toByteArray(); //This is serialized to "C:\test.txt" as binary

C++ Parsing:

Test t1; // My Protobuf Message

std::ifstream myFile("C:\\test.txt");

std::string s;

myFile >> s;

t1.ParseFromString(s);

std::cout << "Decoded: " << t2.s() << std::endl; // Check if parsing was correct

But it just returns: " Decoded: ", as if t2 was empty, but it shouldn't be! How can you parse binary data in C++?

解决方案

Your problem is probably here:

myFile >> s;

The >> operator reads a text string, delimited by whitespace. An encoded protobuf is not text. Probably, ParseFromString() is returning false to indicate that it couldn't parse the data, because it is incomplete.

What you want to do is read the whole file. The easiest way to do this in your case is to use ParseFromIstream(&myFile). (And make sure to check that it returns true!)

(Another option would be to check the file size, create an array of that size, use myFile.read(array, size), and then ParseFromArray(array, size), but that's a lot more work that will do the same thing.)

Note that you probably should not use .txt as a file extension for a protobuf as the file does not contain text.

Also note that, confusingly, in C++ you can put binary (non-text) data in an std::string -- it's just a byte container -- but in Java you cannot put binary data in a String. So the C++ ParseFromString() and SerializeAsString() deal in binary data, whereas the Java toString() actually returns a textual representation of the message (meant for debugging purposes) which is not what you want to transmit.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中有很多种序列化序列化的方式,其中性能最好的之一就是Google开源的ProtobufProtocol Buffers)。 下面是使用Protobuf进行序列化和反序列化的步骤(基于Maven项目): 1. 在pom.xml文件中添加Protobuf依赖: ``` <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>3.17.3</version> </dependency> ``` 2. 定义.proto文件,例如: ``` syntax = "proto3"; package com.example.protobuf; option java_package = "com.example.protobuf"; option java_outer_classname = "PersonProto"; message Person { string name = 1; int32 age = 2; repeated string phoneNumbers = 3; } ``` 3. 使用protoc命令将.proto文件编译成Java类,例如: ``` protoc --java_out=src/main/java src/main/resources/person.proto ``` 4. 在Java代码中使用生成的Person类进行序列化和反序列化,例如: ``` import com.example.protobuf.PersonProto.Person; // 序列化 Person person = Person.newBuilder() .setName("John") .setAge(30) .addPhoneNumbers("123456789") .addPhoneNumbers("987654321") .build(); byte[] data = person.toByteArray(); // 反序列化 Person person2 = Person.parseFrom(data); System.out.println(person2.getName()); System.out.println(person2.getAge()); System.out.println(person2.getPhoneNumbersList()); ``` 注意,在实际使用中,需要根据具体的业务需求来定义.proto文件,并使用生成的对应Java类进行序列化和反序列化。同时,由于Protobuf二进制序列化方式,使用时需要注意数据的兼容性和版本控制。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值