{"id":2166,"date":"2023-04-02T13:41:33","date_gmt":"2023-04-02T05:41:33","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2166"},"modified":"2023-04-05T13:13:35","modified_gmt":"2023-04-05T05:13:35","slug":"serialization-comparison-between-protobuf-and-json","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/02\/serialization-comparison-between-protobuf-and-json\/","title":{"rendered":"\u5e8f\u5217\u5316\uff1aProtoBuf \u4e0e JSON \u7684\u6bd4\u8f83\uff01"},"content":{"rendered":"<h2>\u4ecb\u7ecd<\/h2>\n<p>ProtoBuf \u662fgoogle\u56e2\u961f\u5f00\u53d1\u7684\u7528\u4e8e\u9ad8\u6548\u5b58\u50a8\u548c\u8bfb\u53d6\u7ed3\u6784\u5316\u6570\u636e\u7684\u5de5\u5177\u3002\u4ec0\u4e48\u662f\u7ed3\u6784\u5316\u6570\u636e\u5462\uff0c\u6b63\u5982\u5b57\u9762\u4e0a\u8868\u8fbe\u7684\uff0c\u5c31\u662f\u5e26\u6709\u4e00\u5b9a\u7ed3\u6784\u7684\u6570\u636e\u3002\u6bd4\u5982\u7535\u8bdd\u7c3f\u4e0a\u6709\u5f88\u591a\u8bb0\u5f55\u6570\u636e\uff0c\u6bcf\u6761\u8bb0\u5f55\u5305\u542b\u59d3\u540d\u3001ID\u3001\u90ae\u4ef6\u3001\u7535\u8bdd\u7b49\uff0c\u8fd9\u79cd\u7ed3\u6784\u91cd\u590d\u51fa\u73b0\u3002<\/p>\n<p><!-- more --><\/p>\n<p>XML\u3001JSON \u4e5f\u53ef\u4ee5\u7528\u6765\u5b58\u50a8\u6b64\u7c7b\u7ed3\u6784\u5316\u6570\u636e\uff0c\u4f46\u662f\u4f7f\u7528ProtoBuf\u8868\u793a\u7684\u6570\u636e\u80fd\u66f4\u52a0\u9ad8\u6548\uff0c\u5e76\u4e14\u5c06\u6570\u636e\u538b\u7f29\u5f97\u66f4\u5c0f\u3002<\/p>\n<h2>\u539f\u7406<\/h2>\n<p>ProtoBuf \u662f\u901a\u8fc7ProtoBuf\u7f16\u8bd1\u5668\u5c06\u4e0e\u7f16\u7a0b\u8bed\u8a00\u65e0\u5173\u7684\u7279\u6709\u7684<code>.proto<\/code>\u540e\u7f00\u7684\u6570\u636e\u7ed3\u6784\u6587\u4ef6\u7f16\u8bd1\u6210\u5404\u4e2a\u7f16\u7a0b\u8bed\u8a00(Java,C\/C++,Python)\u4e13\u7528\u7684\u7c7b\u6587\u4ef6\uff0c\u7136\u540e\u901a\u8fc7Google\u63d0\u4f9b\u7684\u5404\u4e2a\u7f16\u7a0b\u8bed\u8a00\u7684\u652f\u6301\u5e93lib\u5373\u53ef\u8c03\u7528API\u3002(\u5173\u4e8eproto\u7ed3\u6784\u4f53\u600e\u4e48\u7f16\u5199\uff0c\u53ef\u81ea\u884c\u67e5\u9605\u6587\u6863)<\/p>\n<h2>ProtoBuf\u7f16\u8bd1\u5668\u5b89\u88c5<\/h2>\n<pre><code>Mac: brew install protobuf<\/code><\/pre>\n<h2>ProtoBuf\u4f7f\u7528\u6848\u4f8b<\/h2>\n<h3>\u5148\u521b\u5efa\u4e00\u4e2aproto\u6587\u4ef6<\/h3>\n<p>message.proto<\/p>\n<pre><code class=\"language-java\">syntax = &quot;proto3&quot;;\n\nmessage Person {\n    int32 id = 1;\n    string name = 2;\n\n    repeated Phone phone = 4;\n\n    enum PhoneType {\n        MOBILE = 0;\n        HOME = 1;\n        WORK = 2;\n    }\n\n    message Phone {\n        string number = 1;\n        PhoneType type = 2;\n    }\n}<\/code><\/pre>\n<h3>\u521b\u5efa\u4e00\u4e2aJava\u9879\u76ee<\/h3>\n<p>\u5e76\u4e14\u5c06proto\u6587\u4ef6\u653e\u7f6e<code>src\/main\/proto<\/code>\u6587\u4ef6\u5939\u4e0b<\/p>\n<h3>\u7f16\u8bd1proto\u6587\u4ef6\u81f3Java\u7248\u672c<\/h3>\n<p>\u7528\u547d\u4ee4\u884c cd \u5230<code>src\/main<\/code>\u76ee\u5f55\u4e0b<\/p>\n<p>\u7ec8\u7aef\u6267\u884c\u547d\u4ee4: <code>protoc --java_out=.\/java .\/proto\/*.proto<\/code><\/p>\n<p>\u4f1a\u53d1\u73b0\uff0c\u5728<code>src\/main\/java<\/code>\u91cc\u5df2\u7ecf\u751f\u6210\u91cc\u5bf9\u5e94\u7684Java\u7c7b<\/p>\n<h3>\u4f9d\u8d56Java\u7248\u672c\u7684ProtoBuf\u652f\u6301\u5e93<\/h3>\n<p>\u8fd9\u91cc\u53ea\u4e3e\u4e00\u4e2a\u7528Gradle\u4f7f\u7528\u4f9d\u8d56\u7684\u6817\u5b50<\/p>\n<pre><code>implementation &#039;com.google.protobuf:protobuf-java:3.9.1&#039;<\/code><\/pre>\n<h3>\u5c06Java\u5bf9\u8c61\u8f6c\u4e3aProtoBuf\u6570\u636e<\/h3>\n<pre><code class=\"language-java\">Message.Person.Phone.Builder phoneBuilder = Message.Person.Phone.newBuilder();\nMessage.Person.Phone phone1 = phoneBuilder\n        .setNumber(&quot;100860&quot;)\n        .setType(Message.Person.PhoneType.HOME)\n        .build();\nMessage.Person.Phone phone2 = phoneBuilder\n        .setNumber(&quot;100100&quot;)\n        .setType(Message.Person.PhoneType.MOBILE)\n        .build();\nMessage.Person.Builder personBuilder = Message.Person.newBuilder();\npersonBuilder.setId(1994);\npersonBuilder.setName(&quot;XIAOLEI&quot;);\npersonBuilder.addPhone(phone1);\npersonBuilder.addPhone(phone2);\n\nMessage.Person person = personBuilder.build();\nlong old = System.currentTimeMillis();\nbyte[] buff = person.toByteArray();\nSystem.out.println(&quot;ProtoBuf \u7f16\u7801\u8017\u65f6\uff1a&quot; + (System.currentTimeMillis() - old));\nSystem.out.println(Arrays.toString(buff));\nSystem.out.println(&quot;ProtoBuf \u6570\u636e\u957f\u5ea6:&quot; + buff.length);<\/code><\/pre>\n<h3>\u5c06ProtoBuf\u6570\u636e\uff0c\u8f6c\u6362\u56deJava\u5bf9\u8c61<\/h3>\n<pre><code class=\"language-java\">System.out.println(&quot;-\u5f00\u59cb\u89e3\u7801-&quot;);\nold = System.currentTimeMillis();\nMessage.Person personOut = Message.Person.parseFrom(buff);\nSystem.out.println(&quot;ProtoBuf \u89e3\u7801\u8017\u65f6\uff1a&quot; + (System.currentTimeMillis() - old));\nSystem.out.printf(&quot;Id:%d, Name:%s\\n&quot;, personOut.getId(), personOut.getName());\nList&lt;Message.Person.Phone&gt; phoneList = personOut.getPhoneList();\nfor (Message.Person.Phone phone : phoneList) {\n    System.out.printf(&quot;\u624b\u673a\u53f7:%s (%s)\\n&quot;, phone.getNumber(), phone.getType());\n}<\/code><\/pre>\n<h2>\u6bd4\u8f83<\/h2>\n<p>\u4e3a\u4e86\u80fd\u4f53\u73b0ProtoBuf\u7684\u4f18\u52bf\uff0c\u6211\u5199\u4e86\u540c\u6837\u7ed3\u6784\u4f53\u7684Java\u7c7b\uff0c\u5e76\u4e14\u5c06Java\u5bf9\u8c61\u8f6c\u6362\u6210JSON\u6570\u636e\uff0c\u6765\u4e0eProtoBuf\u8fdb\u884c\u6bd4\u8f83\u3002JSON\u7f16\u8bd1\u5e93\u4f7f\u7528Google\u63d0\u4f9b\u7684GSON\u5e93\uff0cJSON\u7684\u90e8\u5206\u4ee3\u7801\u5c31\u4e0d\u8d34\u51fa\u6765\u4e86\uff0c\u76f4\u63a5\u5c55\u793a\u7ed3\u679c<\/p>\n<p>\u8fd0\u884c 1 \u6b21<\/p>\n<pre><code>\u3010 JSON \u5f00\u59cb\u7f16\u7801 \u3011\nJSON \u7f16\u78011\u6b21\uff0c\u8017\u65f6\uff1a22ms\nJSON \u6570\u636e\u957f\u5ea6:106\n-\u5f00\u59cb\u89e3\u7801-\nJSON \u89e3\u78011\u6b21\uff0c\u8017\u65f6\uff1a1ms\n\n\u3010 ProtoBuf \u5f00\u59cb\u7f16\u7801 \u3011\nProtoBuf \u7f16\u78011\u6b21,\u8017\u65f6\uff1a32ms\nProtoBuf \u6570\u636e\u957f\u5ea6:34\n-\u5f00\u59cb\u89e3\u7801-\nProtoBuf \u89e3\u78011\u6b21,\u8017\u65f6\uff1a3ms<\/code><\/pre>\n<p>\u8fd0\u884c 10 \u6b21<\/p>\n<pre><code>\u3010 JSON \u5f00\u59cb\u7f16\u7801 \u3011\nJSON \u7f16\u780110\u6b21\uff0c\u8017\u65f6\uff1a22ms\nJSON \u6570\u636e\u957f\u5ea6:106\n-\u5f00\u59cb\u89e3\u7801-\nJSON \u89e3\u780110\u6b21\uff0c\u8017\u65f6\uff1a4ms\n\n\u3010 ProtoBuf \u5f00\u59cb\u7f16\u7801 \u3011\nProtoBuf \u7f16\u780110\u6b21,\u8017\u65f6\uff1a29ms\nProtoBuf \u6570\u636e\u957f\u5ea6:34\n-\u5f00\u59cb\u89e3\u7801-\nProtoBuf \u89e3\u780110\u6b21,\u8017\u65f6\uff1a3ms<\/code><\/pre>\n<p>\u8fd0\u884c 100 \u6b21<\/p>\n<pre><code>\u3010 JSON \u5f00\u59cb\u7f16\u7801 \u3011\nJSON \u7f16\u7801100\u6b21\uff0c\u8017\u65f6\uff1a32ms\nJSON \u6570\u636e\u957f\u5ea6:106\n-\u5f00\u59cb\u89e3\u7801-\nJSON \u89e3\u7801100\u6b21\uff0c\u8017\u65f6\uff1a8ms\n\n\u3010 ProtoBuf \u5f00\u59cb\u7f16\u7801 \u3011\nProtoBuf \u7f16\u7801100\u6b21,\u8017\u65f6\uff1a31ms\nProtoBuf \u6570\u636e\u957f\u5ea6:34\n-\u5f00\u59cb\u89e3\u7801-\nProtoBuf \u89e3\u7801100\u6b21,\u8017\u65f6\uff1a4ms<\/code><\/pre>\n<p>\u8fd0\u884c 1000 \u6b21<\/p>\n<pre><code>\u3010 JSON \u5f00\u59cb\u7f16\u7801 \u3011\nJSON \u7f16\u78011000\u6b21\uff0c\u8017\u65f6\uff1a39ms\nJSON \u6570\u636e\u957f\u5ea6:106\n-\u5f00\u59cb\u89e3\u7801-\nJSON \u89e3\u78011000\u6b21\uff0c\u8017\u65f6\uff1a21ms\n\n\u3010 ProtoBuf \u5f00\u59cb\u7f16\u7801 \u3011\nProtoBuf \u7f16\u78011000\u6b21,\u8017\u65f6\uff1a37ms\nProtoBuf \u6570\u636e\u957f\u5ea6:34\n-\u5f00\u59cb\u89e3\u7801-\nProtoBuf \u89e3\u78011000\u6b21,\u8017\u65f6\uff1a8ms<\/code><\/pre>\n<p>\u8fd0\u884c 1\u4e07 \u6b21<\/p>\n<pre><code>\u3010 JSON \u5f00\u59cb\u7f16\u7801 \u3011\nJSON \u7f16\u780110000\u6b21\uff0c\u8017\u65f6\uff1a126ms\nJSON \u6570\u636e\u957f\u5ea6:106\n-\u5f00\u59cb\u89e3\u7801-\nJSON \u89e3\u780110000\u6b21\uff0c\u8017\u65f6\uff1a93ms\n\n\u3010 ProtoBuf \u5f00\u59cb\u7f16\u7801 \u3011\nProtoBuf \u7f16\u780110000\u6b21,\u8017\u65f6\uff1a49ms\nProtoBuf \u6570\u636e\u957f\u5ea6:34\n-\u5f00\u59cb\u89e3\u7801-\nProtoBuf \u89e3\u780110000\u6b21,\u8017\u65f6\uff1a23ms<\/code><\/pre>\n<p>\u8fd0\u884c 10\u4e07 \u6b21<\/p>\n<pre><code>\u3010 JSON \u5f00\u59cb\u7f16\u7801 \u3011\nJSON \u7f16\u7801100000\u6b21\uff0c\u8017\u65f6\uff1a248ms\nJSON \u6570\u636e\u957f\u5ea6:106\n-\u5f00\u59cb\u89e3\u7801-\nJSON \u89e3\u7801100000\u6b21\uff0c\u8017\u65f6\uff1a180ms\n\n\u3010 ProtoBuf \u5f00\u59cb\u7f16\u7801 \u3011\nProtoBuf \u7f16\u7801100000\u6b21,\u8017\u65f6\uff1a51ms\nProtoBuf \u6570\u636e\u957f\u5ea6:34\n-\u5f00\u59cb\u89e3\u7801-\nProtoBuf \u89e3\u7801100000\u6b21,\u8017\u65f6\uff1a58ms<\/code><\/pre>\n<h2>\u603b\u7ed3<\/h2>\n<h3>\u7f16\u89e3\u7801\u6027\u80fd<\/h3>\n<p>\u4e0a\u8ff0\u6817\u5b50\u53ea\u662f\u7b80\u5355\u7684\u91c7\u6837\uff0c\u5b9e\u9645\u4e0a\u636e\u6211\u7684\u5b9e\u9a8c\u53d1\u73b0<\/p>\n<ul>\n<li>\u6b21\u6570\u57281\u5343\u4ee5\u4e0b\uff0cProtoBuf\u7684\u7f16\u7801\u4e0e\u89e3\u7801\u6027\u80fd\uff0c\u90fd\u4e0eJSON\u4e0d\u76f8\u4e0a\u4e0b\uff0c\u751a\u81f3\u8fd8\u6709\u6bd4JSON\u5dee\u7684\u8d8b\u52bf<\/li>\n<li>\u6b21\u6570\u57282\u5343\u4ee5\u4e0a\uff0cProtoBuf\u7684\u7f16\u7801\u89e3\u7801\u6027\u80fd\uff0c\u90fd\u6bd4JSON\u9ad8\u51fa\u5f88\u591a<\/li>\n<li>\u6b21\u6570\u572810\u4e07\u4ee5\u4e0a\uff0cProtoBuf\u7684\u7f16\u89e3\u7801\u6027\u80fd\u5c31\u5f88\u660e\u663e\u4e86\uff0c\u8fdc\u8fdc\u9ad8\u51faJSON\u7684\u6027\u80fd<\/li>\n<\/ul>\n<h3>\u5185\u5b58\u5360\u7528<\/h3>\n<p>ProtoBuf\u7684\u5185\u5b5834\uff0c\u800cJSON\u5230\u8fbe106\uff0cProtoBuf\u7684\u5185\u5b58\u5360\u7528\u53ea\u6709JSON\u76841\/3.<\/p>\n<h3>\u7ed3\u5c3e<\/h3>\n<p>\u5176\u5b9e\u8fd9\u6b21\u5b9e\u9a8c\u6709\u5f88\u591a\u53ef\u5f85\u4f18\u5316\u7684\u5730\u65b9\uff0c\u5c31\u7b97\u662f\u8fd9\u79cd\u7c97\u7565\u7684\u6d4b\u8bd5\uff0c\u4e5f\u80fd\u770b\u51fa\u6765ProtoBuf\u7684\u4f18\u52bf\u3002<\/p>\n<h2>\u517c\u5bb9<\/h2>\n<h3>\u65b0\u589e\u5b57\u6bb5<\/h3>\n<ul>\n<li>\u5728proto\u6587\u4ef6\u4e2d\u65b0\u589e nickname \u5b57\u6bb5<\/li>\n<li>\u751f\u6210Java\u6587\u4ef6<\/li>\n<li>\u7528\u8001proto\u5b57\u8282\u6570\u7ec4\u6570\u636e\uff0c\u8f6c\u6362\u6210\u5bf9\u8c61<\/li>\n<\/ul>\n<pre><code>Id:1992, Name:XIAOLEI\n\u624b\u673a\u53f7:100860 (HOME)\n\u624b\u673a\u53f7:100100 (MOBILE)\ngetNickname=<\/code><\/pre>\n<p>\u7ed3\u679c\uff0c\u662f\u53ef\u4ee5\u8f6c\u6362\u6210\u529f\u3002<\/p>\n<h3>\u5220\u9664\u5b57\u6bb5<\/h3>\n<ul>\n<li>\u5728proto\u6587\u4ef6\u4e2d\u5220\u9664 name \u5b57\u6bb5<\/li>\n<li>\u751f\u6210Java\u6587\u4ef6<\/li>\n<li>\u7528\u8001proto\u5b57\u8282\u6570\u7ec4\u6570\u636e\uff0c\u8f6c\u6362\u6210\u5bf9\u8c61<\/li>\n<\/ul>\n<pre><code>Id:1992, Name:null\n\u624b\u673a\u53f7:100860 (HOME)\n\u624b\u673a\u53f7:100100 (MOBILE)<\/code><\/pre>\n<p>\u7ed3\u679c\uff0c\u662f\u53ef\u4ee5\u8f6c\u6362\u6210\u529f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ecb\u7ecd ProtoBuf \u662fgoogle\u56e2\u961f\u5f00\u53d1\u7684\u7528\u4e8e\u9ad8\u6548\u5b58\u50a8\u548c\u8bfb\u53d6\u7ed3\u6784\u5316\u6570\u636e\u7684\u5de5\u5177\u3002\u4ec0\u4e48\u662f\u7ed3\u6784\u5316\u6570\u636e\u5462\uff0c\u6b63\u5982\u5b57 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[97],"tags":[74,554],"class_list":["post-2166","post","type-post","status-publish","format-standard","hentry","category-tools-skills","tag-json","tag-protobuf"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2166","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2166"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2166\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}