protobuf的简单应用,json和protocol Buffer的转换简单例子
Google Protocol Buffer 的使用和原理
Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,很适合做数据存储或 RPC 数据交换格式。它可用于通讯协议、数据存储等领域的语言无关、平台无关、可扩展的序列化结构数据格式。目前提供了 C++、Java、Python 三种语言的 API。
简介
什么是 Google Protocol Buffer? 假如您在网上搜索,应该会得到类似这样的文字介绍:
Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,目前已经正在使用的有超过 48,162 种报文格式定义和超过 12,183 个 .proto 文件。他们用于 RPC 系统和持续数据存储系统。
Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化。它很适合做数据存储或 RPC 数据交换格式。可用于通讯协议、数据存储等领域的语言无关、平台无关、可扩展的序列化结构数据格式。目前提供了 C++、Java、Python 三种语言的 API。
或许您和我一样,在第一次看完这些介绍后还是不明白 Protobuf 究竟是什么,那么我想一个简单的例子应该比较有助于理解它。
例子:
package org.protob; import org.protob.W.helloworld; import com.google.protobuf.InvalidProtocolBufferException; import com.googlecode.protobuf.format.JsonFormat; import com.googlecode.protobuf.format.JsonFormat.ParseException; /** * 下载 protoc-2.5.0-win32.zip * cmd: protoc.exe --java_out=./ w.proto * @author liangrui * */ public class Main { public static void main(String[] args) throws Exception { main2(null); main3(null); main4(null); } //序列化 /返序列化 public static void main2(String[] args) throws InvalidProtocolBufferException { //序列化 helloworld.Builder builder=helloworld.newBuilder(); builder.setId(10); builder.setStr("fx"); builder.setOpt(20); helloworld info=builder.build(); byte[] result=info.toByteArray() ; //返序列化 helloworld msg = helloworld.parseFrom(result); System.out.println(msg); } //protobuf转json public static void main3(String[] args) throws InvalidProtocolBufferException { //序列化 helloworld.Builder builder=helloworld.newBuilder(); builder.setId(10); builder.setStr("fx"); builder.setOpt(20); helloworld info=builder.build(); byte[] result=info.toByteArray() ; //返序列化 helloworld hello = helloworld.parseFrom(result); System.out.println(hello); String jsonFormat =JsonFormat.printToString(hello); System.out.println(jsonFormat); } //josn转protobuf public static void main4(String[] args) throws ParseException { helloworld.Builder builder =helloworld.newBuilder(); String jsonFormat = "{id:11,str:'xxx',opt:50}"; JsonFormat.merge(jsonFormat, builder); System.out.println(builder.build()); } } /** output: id: 10 str: "fx" opt: 20 id: 10 str: "fx" opt: 20 {"id": 10,"str": "fx","opt": 20} id: 11 str: "xxx" opt: 50 */
proto文件 w.proto
package org.protob; message helloworld { required int32 id = 1; // ID required string str = 2; // str optional int32 opt = 3; //optional field }
生成后的java类
// Generated by the protocol buffer compiler. DO NOT EDIT! // source: w.proto package org.protob; public final class W { private W() {} public static void registerAllExtensions( com.google.protobuf.ExtensionRegistry registry) { } public interface helloworldOrBuilder extends com.google.protobuf.MessageOrBuilder { // required int32 id = 1; /** * <code>required int32 id = 1;</code> * * <pre> * ID * </pre> */ boolean hasId(); /** * <code>required int32 id = 1;</code> * * <pre> * ID * </pre> */ int getId(); // required string str = 2; /** * <code>required string str = 2;</code> * * <pre> * str * </pre> */ boolean hasStr(); /** * <code>required string str = 2;</code> * * <pre> * str * </pre> */ java.lang.String getStr(); /** * <code>required string str = 2;</code> * * <pre> * str * </pre> */ com.google.protobuf.ByteString getStrBytes(); // optional int32 opt = 3; /** * <code>optional int32 opt = 3;</code> * * <pre> *optional field * </pre> */ boolean hasOpt(); /** * <code>optional int32 opt = 3;</code> * * <pre> *optional field * </pre> */ int getOpt(); } /** * Protobuf type {@code org.protob.helloworld} */ public static final class helloworld extends com.google.protobuf.GeneratedMessage implements helloworldOrBuilder { // Use helloworld.newBuilder() to construct. private helloworld(com.google.protobuf.GeneratedMessage.Builder<?> builder) { super(builder); this.unknownFields = builder.getUnknownFields(); } private helloworld(boolean noInit) { this.unknownFields = com.google.protobuf.UnknownFieldSet.getDefaultInstance(); } private static final helloworld defaultInstance; public static helloworld getDefaultInstance() { return defaultInstance; } public helloworld getDefaultInstanceForType() { return defaultInstance; } private final com.google.protobuf.UnknownFieldSet unknownFields; @java.lang.Override public final com.google.protobuf.UnknownFieldSet getUnknownFields() { return this.unknownFields; } private helloworld( com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException { initFields(); int mutable_bitField0_ = 0; com.google.protobuf.UnknownFieldSet.Builder unknownFields = com.google.protobuf.UnknownFieldSet.newBuilder(); try { boolean done = false; while (!done) { int tag = input.readTag(); switch (tag) { case 0: done = true; break; default: { if (!parseUnknownField(input, unknownFields, extensionRegistry, tag)) { done = true; } break; } case 8: { bitField0_ |= 0x00000001; id_ = input.readInt32(); break; } case 18: { bitField0_ |= 0x00000002; str_ = input.readBytes(); break; } case 24: { bitField0_ |= 0x00000004; opt_ = input.readInt32(); break; } } } } catch (com.google.protobuf.InvalidProtocolBufferException e) { throw e.setUnfinishedMessage(this); } catch (java.io.IOException e) { throw new com.google.protobuf.InvalidProtocolBufferException( e.getMessage()).setUnfinishedMessage(this); } finally { this.unknownFields = unknownFields.build(); makeExtensionsImmutable(); } } public static final com.google.protobuf.Descriptors.Descriptor getDescriptor() { return org.protob.W.internal_static_org_protob_helloworld_descriptor; } protected com.google.protobuf.GeneratedMessage.FieldAccessorTable internalGetFieldAccessorTable() { return org.protob.W.internal_static_org_protob_helloworld_fieldAccessorTable .ensureFieldAccessorsInitialized( org.protob.W.helloworld.class, org.protob.W.helloworld.Builder.class); } public static com.google.protobuf.Parser<helloworld> PARSER = new com.google.protobuf.AbstractParser<helloworld>() { public helloworld parsePartialFrom( com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException { return new helloworld(input, extensionRegistry); } }; @java.lang.Override public com.google.protobuf.Parser<helloworld> getParserForType() { return PARSER; } private int bitField0_; // required int32 id = 1; public static final int ID_FIELD_NUMBER = 1; private int id_; /** * <code>required int32 id = 1;</code> * * <pre> * ID * </pre> */ public boolean hasId() { return ((bitField0_ & 0x00000001) == 0x00000001); } /** * <code>required int32 id = 1;</code> * * <pre> * ID * </pre> */ public int getId() { return id_; } // required string str = 2; public static final int STR_FIELD_NUMBER = 2; private java.lang.Object str_; /** * <code>required string str = 2;</code> * * <pre> * str * </pre> */ public boolean hasStr() { return ((bitField0_ & 0x00000002) == 0x00000002); } /** * <code>required string str = 2;</code> * * <pre> * str * </pre> */ public java.lang.String getStr() { java.lang.Object ref = str_; if (ref instanceof java.lang.String) { return (java.lang.String) ref; } else { com.google.protobuf.ByteString bs = (com.google.protobuf.ByteString) ref; java.lang.String s = bs.toStringUtf8(); if (bs.isValidUtf8()) { str_ = s; } return s; } } /** * <code>required string str = 2;</code> * * <pre> * str * </pre> */ public com.google.protobuf.ByteString getStrBytes() { java.lang.Object ref = str_; if (ref instanceof java.lang.String) { com.google.protobuf.ByteString b = com.google.protobuf.ByteString.copyFromUtf8( (java.lang.String) ref); str_ = b; return b; } else { return (com.google.protobuf.ByteString) ref; } } // optional int32 opt = 3; public static final int OPT_FIELD_NUMBER = 3; private int opt_; /** * <code>optional int32 opt = 3;</code> * * <pre> *optional field * </pre> */ public boolean hasOpt() { return ((bitField0_ & 0x00000004) == 0x00000004); } /** * <code>optional int32 opt = 3;</code> * * <pre> *optional field * </pre> */ public int getOpt() { return opt_; } private void initFields() { id_ = 0; str_ = ""; opt_ = 0; } private byte memoizedIsInitialized = -1; public final boolean isInitialized() { byte isInitialized = memoizedIsInitialized; if (isInitialized != -1) return isInitialized == 1; if (!hasId()) { memoizedIsInitialized = 0; return false; } if (!hasStr()) { memoizedIsInitialized = 0; return false; } memoizedIsInitialized = 1; return true; } public void writeTo(com.google.protobuf.CodedOutputStream output) throws java.io.IOException { getSerializedSize(); if (((bitField0_ & 0x00000001) == 0x00000001)) { output.writeInt32(1, id_); } if (((bitField0_ & 0x00000002) == 0x00000002)) { output.writeBytes(2, getStrBytes()); } if (((bitField0_ & 0x00000004) == 0x00000004)) { output.writeInt32(3, opt_); } getUnknownFields().writeTo(output); } private int memoizedSerializedSize = -1; public int getSerializedSize() { int size = memoizedSerializedSize; if (size != -1) return size; size = 0; if (((bitField0_ & 0x00000001) == 0x00000001)) { size += com.google.protobuf.CodedOutputStream .computeInt32Size(1, id_); } if (((bitField0_ & 0x00000002) == 0x00000002)) { size += com.google.protobuf.CodedOutputStream .computeBytesSize(2, getStrBytes()); } if (((bitField0_ & 0x00000004) == 0x00000004)) { size += com.google.protobuf.CodedOutputStream .computeInt32Size(3, opt_); } size += getUnknownFields().getSerializedSize(); memoizedSerializedSize = size; return size; } private static final long serialVersionUID = 0L; @java.lang.Override protected java.lang.Object writeReplace() throws java.io.ObjectStreamException { return super.writeReplace(); } public static org.protob.W.helloworld parseFrom( com.google.protobuf.ByteString data) throws com.google.protobuf.InvalidProtocolBufferException { return PARSER.parseFrom(data); } public static org.protob.W.helloworld parseFrom( com.google.protobuf.ByteString data, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException { return PARSER.parseFrom(data, extensionRegistry); } public static org.protob.W.helloworld parseFrom(byte[] data) throws com.google.protobuf.InvalidProtocolBufferException { return PARSER.parseFrom(data); } public static org.protob.W.helloworld parseFrom( byte[] data, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException { return PARSER.parseFrom(data, extensionRegistry); } public static org.protob.W.helloworld parseFrom(java.io.InputStream input) throws java.io.IOException { return PARSER.parseFrom(input); } public static org.protob.W.helloworld parseFrom( java.io.InputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException { return PARSER.parseFrom(input, extensionRegistry); } public static org.protob.W.helloworld parseDelimitedFrom(java.io.InputStream input) throws java.io.IOException { return PARSER.parseDelimitedFrom(input); } public static org.protob.W.helloworld parseDelimitedFrom( java.io.InputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException { return PARSER.parseDelimitedFrom(input, extensionRegistry); } public static org.protob.W.helloworld parseFrom( com.google.protobuf.CodedInputStream input) throws java.io.IOException { return PARSER.parseFrom(input); } public static org.protob.W.helloworld parseFrom( com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException { return PARSER.parseFrom(input, extensionRegistry); } public static Builder newBuilder() { return Builder.create(); } public Builder newBuilderForType() { return newBuilder(); } public static Builder newBuilder(org.protob.W.helloworld prototype) { return newBuilder().mergeFrom(prototype); } public Builder toBuilder() { return newBuilder(this); } @java.lang.Override protected Builder newBuilderForType( com.google.protobuf.GeneratedMessage.BuilderParent parent) { Builder builder = new Builder(parent); return builder; } /** * Protobuf type {@code org.protob.helloworld} */ public static final class Builder extends com.google.protobuf.GeneratedMessage.Builder<Builder> implements org.protob.W.helloworldOrBuilder { public static final com.google.protobuf.Descriptors.Descriptor getDescriptor() { return org.protob.W.internal_static_org_protob_helloworld_descriptor; } protected com.google.protobuf.GeneratedMessage.FieldAccessorTable internalGetFieldAccessorTable() { return org.protob.W.internal_static_org_protob_helloworld_fieldAccessorTable .ensureFieldAccessorsInitialized( org.protob.W.helloworld.class, org.protob.W.helloworld.Builder.class); } // Construct using org.protob.W.helloworld.newBuilder() private Builder() { maybeForceBuilderInitialization(); } private Builder( com.google.protobuf.GeneratedMessage.BuilderParent parent) { super(parent); maybeForceBuilderInitialization(); } private void maybeForceBuilderInitialization() { if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) { } } private static Builder create() { return new Builder(); } public Builder clear() { super.clear(); id_ = 0; bitField0_ = (bitField0_ & ~0x00000001); str_ = ""; bitField0_ = (bitField0_ & ~0x00000002); opt_ = 0; bitField0_ = (bitField0_ & ~0x00000004); return this; } public Builder clone() { return create().mergeFrom(buildPartial()); } public com.google.protobuf.Descriptors.Descriptor getDescriptorForType() { return org.protob.W.internal_static_org_protob_helloworld_descriptor; } public org.protob.W.helloworld getDefaultInstanceForType() { return org.protob.W.helloworld.getDefaultInstance(); } public org.protob.W.helloworld build() { org.protob.W.helloworld result = buildPartial(); if (!result.isInitialized()) { throw newUninitializedMessageException(result); } return result; } public org.protob.W.helloworld buildPartial() { org.protob.W.helloworld result = new org.protob.W.helloworld(this); int from_bitField0_ = bitField0_; int to_bitField0_ = 0; if (((from_bitField0_ & 0x00000001) == 0x00000001)) { to_bitField0_ |= 0x00000001; } result.id_ = id_; if (((from_bitField0_ & 0x00000002) == 0x00000002)) { to_bitField0_ |= 0x00000002; } result.str_ = str_; if (((from_bitField0_ & 0x00000004) == 0x00000004)) { to_bitField0_ |= 0x00000004; } result.opt_ = opt_; result.bitField0_ = to_bitField0_; onBuilt(); return result; } public Builder mergeFrom(com.google.protobuf.Message other) { if (other instanceof org.protob.W.helloworld) { return mergeFrom((org.protob.W.helloworld)other); } else { super.mergeFrom(other); return this; } } public Builder mergeFrom(org.protob.W.helloworld other) { if (other == org.protob.W.helloworld.getDefaultInstance()) return this; if (other.hasId()) { setId(other.getId()); } if (other.hasStr()) { bitField0_ |= 0x00000002; str_ = other.str_; onChanged(); } if (other.hasOpt()) { setOpt(other.getOpt()); } this.mergeUnknownFields(other.getUnknownFields()); return this; } public final boolean isInitialized() { if (!hasId()) { return false; } if (!hasStr()) { return false; } return true; } public Builder mergeFrom( com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException { org.protob.W.helloworld parsedMessage = null; try { parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); } catch (com.google.protobuf.InvalidProtocolBufferException e) { parsedMessage = (org.protob.W.helloworld) e.getUnfinishedMessage(); throw e; } finally { if (parsedMessage != null) { mergeFrom(parsedMessage); } } return this; } private int bitField0_; // required int32 id = 1; private int id_ ; /** * <code>required int32 id = 1;</code> * * <pre> * ID * </pre> */ public boolean hasId() { return ((bitField0_ & 0x00000001) == 0x00000001); } /** * <code>required int32 id = 1;</code> * * <pre> * ID * </pre> */ public int getId() { return id_; } /** * <code>required int32 id = 1;</code> * * <pre> * ID * </pre> */ public Builder setId(int value) { bitField0_ |= 0x00000001; id_ = value; onChanged(); return this; } /** * <code>required int32 id = 1;</code> * * <pre> * ID * </pre> */ public Builder clearId() { bitField0_ = (bitField0_ & ~0x00000001); id_ = 0; onChanged(); return this; } // required string str = 2; private java.lang.Object str_ = ""; /** * <code>required string str = 2;</code> * * <pre> * str * </pre> */ public boolean hasStr() { return ((bitField0_ & 0x00000002) == 0x00000002); } /** * <code>required string str = 2;</code> * * <pre> * str * </pre> */ public java.lang.String getStr() { java.lang.Object ref = str_; if (!(ref instanceof java.lang.String)) { java.lang.String s = ((com.google.protobuf.ByteString) ref) .toStringUtf8(); str_ = s; return s; } else { return (java.lang.String) ref; } } /** * <code>required string str = 2;</code> * * <pre> * str * </pre> */ public com.google.protobuf.ByteString getStrBytes() { java.lang.Object ref = str_; if (ref instanceof String) { com.google.protobuf.ByteString b = com.google.protobuf.ByteString.copyFromUtf8( (java.lang.String) ref); str_ = b; return b; } else { return (com.google.protobuf.ByteString) ref; } } /** * <code>required string str = 2;</code> * * <pre> * str * </pre> */ public Builder setStr( java.lang.String value) { if (value == null) { throw new NullPointerException(); } bitField0_ |= 0x00000002; str_ = value; onChanged(); return this; } /** * <code>required string str = 2;</code> * * <pre> * str * </pre> */ public Builder clearStr() { bitField0_ = (bitField0_ & ~0x00000002); str_ = getDefaultInstance().getStr(); onChanged(); return this; } /** * <code>required string str = 2;</code> * * <pre> * str * </pre> */ public Builder setStrBytes( com.google.protobuf.ByteString value) { if (value == null) { throw new NullPointerException(); } bitField0_ |= 0x00000002; str_ = value; onChanged(); return this; } // optional int32 opt = 3; private int opt_ ; /** * <code>optional int32 opt = 3;</code> * * <pre> *optional field * </pre> */ public boolean hasOpt() { return ((bitField0_ & 0x00000004) == 0x00000004); } /** * <code>optional int32 opt = 3;</code> * * <pre> *optional field * </pre> */ public int getOpt() { return opt_; } /** * <code>optional int32 opt = 3;</code> * * <pre> *optional field * </pre> */ public Builder setOpt(int value) { bitField0_ |= 0x00000004; opt_ = value; onChanged(); return this; } /** * <code>optional int32 opt = 3;</code> * * <pre> *optional field * </pre> */ public Builder clearOpt() { bitField0_ = (bitField0_ & ~0x00000004); opt_ = 0; onChanged(); return this; } // @@protoc_insertion_point(builder_scope:org.protob.helloworld) } static { defaultInstance = new helloworld(true); defaultInstance.initFields(); } // @@protoc_insertion_point(class_scope:org.protob.helloworld) } private static com.google.protobuf.Descriptors.Descriptor internal_static_org_protob_helloworld_descriptor; private static com.google.protobuf.GeneratedMessage.FieldAccessorTable internal_static_org_protob_helloworld_fieldAccessorTable; public static com.google.protobuf.Descriptors.FileDescriptor getDescriptor() { return descriptor; } private static com.google.protobuf.Descriptors.FileDescriptor descriptor; static { java.lang.String[] descriptorData = { "\n\007w.proto\022\norg.protob\"2\n\nhelloworld\022\n\n\002i" + "d\030\001 \002(\005\022\013\n\003str\030\002 \002(\t\022\013\n\003opt\030\003 \001(\005" }; com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner = new com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner() { public com.google.protobuf.ExtensionRegistry assignDescriptors( com.google.protobuf.Descriptors.FileDescriptor root) { descriptor = root; internal_static_org_protob_helloworld_descriptor = getDescriptor().getMessageTypes().get(0); internal_static_org_protob_helloworld_fieldAccessorTable = new com.google.protobuf.GeneratedMessage.FieldAccessorTable( internal_static_org_protob_helloworld_descriptor, new java.lang.String[] { "Id", "Str", "Opt", }); return null; } }; com.google.protobuf.Descriptors.FileDescriptor .internalBuildGeneratedFileFrom(descriptorData, new com.google.protobuf.Descriptors.FileDescriptor[] { }, assigner); } // @@protoc_insertion_point(outer_class_scope) }
一个很好的例子:不过是c++
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。