怎样把json转为protocol buffer
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎样把json转为protocol buffer相关的知识,希望对你有一定的参考价值。
例子: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;
参考技术A :浏览器和JS并不直接处理数据--尤其是遇到内部服务时。我的观点是,结构化格式,例如谷歌的Protocol Buffers,是一个比JSON在编码方面更好的选择。如果你从来没有使用过Protocol Buffers,你可以参看 这里。
Protocol Buffer 去除 json omitempty
TODO
以上是关于怎样把json转为protocol buffer的主要内容,如果未能解决你的问题,请参考以下文章
C#中怎样将List<自己定义>转为Json格式 及相关函数-DataContractJsonSerializer