Efficient way of parsing this json, without reflecting

agolangf · · 518 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi all!</p> <p>I&#39;m working with a very weird websocket connection. Instead of having objects with keys which can be mapped to structs they send array with all dynamic types.</p> <p>example: <code> [ 27291, [ 1508101980000, 5510, 5506.1, 5510.3, 5506, 5.71021852 ] ] </code></p> <p>another example: <code> [ 70, [ 5511, 2, -0.94000001 ] ] </code></p> <p>another one: <code> [ 0, &#34;pb&#34;, [] ] </code></p> <p>This couldn&#39;t be mapped to any concrete type without using reflection. I&#39;ve been using the <code>bytes</code> package to clean the byte arrays retrieved and split to get the right data. As the endpoints I use from the api grow this becomes really cumbersome. I was wondering if there is an more efficient way, without reflection.</p> <p>Thanks in advance!</p> <hr/>**评论:**<br/><br/>tv64738: <pre><p><code>[]json.RawMessage</code>, and perhaps <code>json.Decoder.Token</code>.</p> <p>How will you know what types the items will be? For example, does the first item dictate the types of the rest? If so, <a href="http://eagain.net/articles/go-dynamic-json/">http://eagain.net/articles/go-dynamic-json/</a> and <a href="http://eagain.net/articles/go-json-array-to-struct/">http://eagain.net/articles/go-json-array-to-struct/</a> should show you the way.</p></pre>forfunc: <pre><p>Thanks!! Json. Decoder. Token is exactly what I needed</p></pre>sgmansfield: <pre><p>FWIW I use this in a project and it works well. I recommend it as well.</p> <p>One caveat, you have to remember to replace the remainder of the buffer at the front of the input reader after you&#39;re done reading the json. The parser has its own buffer.</p></pre>sh41: <pre><p>Definitely consider using <a href="https://godoc.org/encoding/json#Decoder.Token" rel="nofollow">https://godoc.org/encoding/json#Decoder.Token</a>, as <a href="/u/tv64738" rel="nofollow">/u/tv64738</a> suggested:</p> <p><a href="https://play.golang.org/p/Q1SwIdYCSm" rel="nofollow">https://play.golang.org/p/Q1SwIdYCSm</a></p></pre>forfunc: <pre><p>That looks as the solution I needed!! Thank you</p></pre>itsmontoya: <pre><p>You might be able to do it with <a href="https://github.com/itsmontoya/jsoon" rel="nofollow">jsoon</a>, but it&#39;s no where near production-ready. If you decide to use it and encounter bugs, submit a github issue</p></pre>ericzhill: <pre><p>Easy. Use the existing json grammar with antlr. Have antlr generate your parser code, and write event handlers to handle the begin/end events as the string is parsed.</p> <p><a href="http://www.antlr.org" rel="nofollow">http://www.antlr.org</a></p> <p><a href="https://github.com/antlr/grammars-v4/blob/master/json/JSON.g4" rel="nofollow">https://github.com/antlr/grammars-v4/blob/master/json/JSON.g4</a></p> <p>It&#39;s a bit of a learning curve and not terribly well documented, but it&#39;s lightning fast and rock solid stable.</p></pre>

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

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