如何通过使用协议缓冲区反序列化Java中的文件吗? [英] How to de-serialize the file in Java by using Protocol-buffer?
问题描述
要创建在C#中序列化的文件,然后想反序列化Java中的相同。对于这一点,我使用协议缓冲库。在这里,我想这样做:
在C#中,我添加的protobuf-net.dll并要序列重新psented为$ P $类:
Person.cs
[ProtoBuf.ProtoContract]
公共类Person
{
[ProtoBuf.ProtoMember(1)]
公众诠释标识{获取;设置;}
[ProtoBuf.ProtoMember(2)]
公共字符串名称{;组; }
}
和的主要方法,序列化这样的:
变种人=新的Person
{
ID = 12345,
NAME =弗雷德,
};
使用(var文件= File.Create(somepath \\\\ person.bin))
{
Serializer.Serialize(文件的人);
}
我复制并在ecilpse拉filesystem-的Android / SD卡和该bin文件试图反序列化它
在eclipse -Java,
补充protobuf.jar,外部库和创建person.proto文件,其中包含:
消息人士{
INT32 ID = 1要求的;
必需的字符串名称= 2;
}
任何人都可以请建议如何反序列化这是在C#创建的对象?
的常用方法Protocol Buffers的工作如何为:
- 您定义在一个小
.proto
文本文件的协议。 - 您在运行该文件 的protobuf的编译器
- 原编译器生成你需要序列化和功放的所有语言源$ C $ C;解析对象。
在C#中使用该库是特殊的。它并不需要一个 .proto
文件开始。相反,你可以通过注解类(或任何在C#中的术语)创建code范围内的协议。
为了用你的IN-code产生的协议,从Java您需要的 .proto
文件,因为Java不支持这种类型的定义在code。
您既可以手工编写的 .proto
文件或让C#库生成(见马克Gravell的答案) - 我建议你生成的文件,这样就可以'T犯错误的定义。
一旦该文件在运行的Protobuf编译器( protoc
)的(下载中心)在 .proto
文件,并将其将生成一个的.java
文件给你。这个文件有序列化和分析所需的人
类,应有尽有。
现在,你既包括的protobuf [版]的.jar
,并在项目生成的的.java
文件。在 .proto
文件本身并不需要在项目中。
一旦你这样做,仅仅使用生成的code解析文件:
人员的人= Person.parseFrom(新的FileInputStream(文件));
code,你写的唯一的线是上面的。
有关Java集成的更多细节在官方教程 发现p>
Want to create a serialized file in C# and then want to de-serialize the same in Java. For this , i am using Protocol buffer library. Here want i have done:
In C#, I added protobuf-net.dll and the class which is to be serialized is represented as:
Person.cs
[ProtoBuf.ProtoContract]
public class Person
{
[ProtoBuf.ProtoMember(1)]
public int Id {get;set;}
[ProtoBuf.ProtoMember(2)]
public string Name { get; set; }
}
and in the main method, serialized it like this:
var person = new Person
{
Id = 12345,
Name = "Fred",
};
using (var file = File.Create("somepath\\person.bin"))
{
Serializer.Serialize(file, person);
}
This bin file i copied and pulled in the ecilpse filesystem- android /sdcard and tried to de-serialize it
In eclipse -JAVA,
Added protobuf.jar, external library and created person.proto file which contains:
message Person{
required int32 Id=1;
required string Name=2;
}
Can anyone please suggest how to de-serialize the object which was created in the C#?
The usual way how Protocol Buffers work is:
- You define your protocol in a small
.proto
text file. - You run the protobuf compiler on that file
- proto compiler generates source code in all the languages you need to serialize & parse objects.
The library you use in C# is special. It does not need that .proto
file to start with. Instead you can create the protocol within your code by annotating classes (or whatever the terminology in C# is).
In order to use your in-code-generated protocol from Java you need the .proto
file since Java does not support that type of definition in code.
You can either write the .proto
file by hand or let the C# library generate it (see Marc Gravell's answer) - I would suggest you generate the file so you can't make mistakes in the definition.
Once you have that file you run the protobuf compiler (protoc
) (dowload) on the .proto
file and it will generate a .java
file for you. That file has the Person
class and everything required to serialize and parse.
Now you include both protobuf[version].jar
and the generated .java
file in your project. The .proto
file itself is not required in the project.
Once you have done that simply use the generated code to parse the file:
Person person = Person.parseFrom(new FileInputStream(file));
The only line of code that you write is that one above.
More details about the Java integration is found in the official tutorial
这篇关于如何通过使用协议缓冲区反序列化Java中的文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!