Best data serialization format between Go and Javascript?

polaris · · 873 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m working on project that includes a server in Golang and a client in Javascript, it&#39;s a game therefore they need to exchange data, do you know any good format that is network efficient ( binary protocol )?</p> <p>I was interested into Flatbuffers from Google but there is no JS library yet :/</p> <hr/>**评论:**<br/><br/>dgryski: <pre><p>Gzip+json. These will be implemented natively on C in the browser, but all other protocols will be in Javascript. Benchmark to determine if this isn&#39;t fast enough. </p></pre>tex0: <pre><p>Why would you want to use anything but JSON for a JavaScript client?</p> <p>Valid JSON is a valid JavaScript object so this is executed by the Browsers JavaScript engine. I don&#39;t think there is anything more optmized than the JavaScript engine in mordern browsers.</p> <p>But yeah, benchmark it.</p></pre>KrakatoaSpelunker: <pre><blockquote> <p>Why would you want to use anything but JSON for a JavaScript client?</p> </blockquote> <p>Protobuf is faster and more efficient, which OP says he/she cares about.</p></pre>giovannibajo: <pre><p>Alas, that&#39;s not true for JS. Javascript protobuf implementation is in Javascript, while JSON parsing is down with native code. There is a big speed difference.</p></pre>KrakatoaSpelunker: <pre><blockquote> <p>Javascript protobuf implementation is in Javascript, while JSON parsing is down with native code. There is a big speed difference.</p> </blockquote> <p>OP asked for network efficiency. Protobuf is going to be much faster to transfer over the wire, and that&#39;s almost certainly going to outweigh the extra cost of parsing.</p></pre>tlianza: <pre><p>Someone posted their experience here and the on-the-wire size of protobuffs versus gzipped json is surprisingly marginal: <a href="https://blog.wearewizards.io/using-protobuf-instead-of-json-to-communicate-with-a-frontend">https://blog.wearewizards.io/using-protobuf-instead-of-json-to-communicate-with-a-frontend</a></p></pre>seufert: <pre><p>But why premature optimization without even knowing? </p></pre>jerf: <pre><blockquote> <p>that&#39;s <em>almost certainly</em> going to outweigh the extra cost of parsing.</p> </blockquote> <p>Profile. Profile, profile, profile. Your instinct to qualify that statement was a good start, but doesn&#39;t really go far enough. You really need a round-trip profile for the whole system.</p> <p>I&#39;d also suggest to the OP that if you haven&#39;t done anything much yet, you won&#39;t really know what &#34;good enough&#34; performance is. JSON is probably going to be pretty good.</p></pre>KrakatoaSpelunker: <pre><p>...&#34;a good start&#34;? OP is asking for suggestions on Reddit. I think that&#39;s about as far as any comment can be expected to go. </p></pre>egonelbre: <pre><blockquote> <p>Why would you want to use anything but JSON for a JavaScript client?</p> </blockquote> <p>Medium datasets; e.g. 1GB of JSON data is quite problematic in browser - by parsing it into JS Objects you will construct millions of objects which adds GC pressure and has large overhead in object headers.</p></pre>cogman10: <pre><p>Cap&#39;n proto is a good option. Should be pretty quick on both ends.</p></pre>dasgurks: <pre><p>JavaScript internally uses a floating point type for all numbers, which leads to extremely slow bit operations. I would therefore not consider a binary format. So realistically gzipped JSON should be the fastest option considering the client side.</p></pre>cogman10: <pre><p>Modern JavaScript engines are pretty smart. When doing int operations (bit shifts and the like) most engines will internally represent these numbers as ints and only promote to floats when a float operation happens.</p> <p>Internally things are complex in the JavaScript world.</p></pre>captncraig: <pre><p>I&#39;ve used thrift with great success.</p></pre>vbaspcppguy: <pre><p>MessagePack </p> <p>Edit: Huh. There used to be an official JS library but not anymore. </p></pre>giovannibajo: <pre><p>I tried this, and there are many issues, the bigger one being that there is no sane way to handle the int64/uin64 in Javascript. It gets silently converted into a floating point number which then drops precision so the number... is different :)</p></pre>vbaspcppguy: <pre><p>Yeah, you likely won&#39;t find a good fix for that.</p></pre>giovannibajo: <pre><p>FWIW our workaround was to unmarshal uint64 into strings. Since they were used for ids, it kind of worked :)</p></pre>kurin: <pre><p>You can do that kinda-sorta painlessly with something like:</p> <pre><code>type jsonInt64 int64 func (i jsonInt64) MarshalJSON() ([]byte, error) { return []byte(`&#34;` + strconv.FormatInt(int64(i), 10) + `&#34;`), nil } func (i *jsonInt64) UnmarshalJSON(data []byte) error { val, err := strconv.ParseInt(string(data[1:len(data)-1]), 10, 64) if err != nil { return err } *i = jsonInt64(val) return nil } </code></pre></pre>cihangirsavas: <pre><p>you can just use <code>,string</code> json tag</p> <p><a href="http://golang.org/pkg/encoding/json/#Marshal" rel="nofollow">http://golang.org/pkg/encoding/json/#Marshal</a></p> <blockquote> <p>The &#34;string&#34; option signals that a field is stored as JSON inside a JSON-encoded string. It applies only to fields of string, floating point, or integer types. This extra level of encoding is sometimes used when communicating with JavaScript programs:</p> </blockquote> <pre><code>Int64String int64 `json:&#34;,string&#34;` </code></pre></pre>kurin: <pre><p>Well or that.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

873 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传