json.Marshal is not converting []byte to base64 string

xuanbao · · 311 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m trying to pass an array of bytes to a client in a JSON object. Here&#39;s the struct I&#39;m trying to marshal:</p> <pre><code>type SomeResponse struct { data []byte `json:&#34;data&#34;` } </code></pre> <p>So here&#39;s my code creating the struct and marshaling it:</p> <pre><code>bytes := getSomeBytes() // returns somewhat large []byte someResponse := SomeResponse{data: bytes} jsonResponse, err := json.Marshal(someResponse) </code></pre> <p>There are no errors returned from json.Marshal. However when I print the jsonResponse, it&#39;s a tiny []byte with a length of 2. Why is my data not there?</p> <hr/>**评论:**<br/><br/>wwader: <pre><p>Try rename data to Data with capital d to make it public</p></pre>aminoglycine: <pre><p>Thank you! I guess it makes sense that if you made an attribute private, you wouldn&#39;t want it potentially being sent out the door. Duh.</p></pre>throwlikepollock: <pre><p>I don&#39;t know if it&#39;s a &#34;feature&#34; or not, i had always assumed that it&#39;s simply because the json.* lib cannot access/view private variables. To put it differently, i don&#39;t think it&#39;s a security feature of the marshal/unmarshal, i think it&#39;s the general private/public feature of the language.</p></pre>j_d_q: <pre><p>The marshaller uses reflection, so it definitely has access to variables that aren&#39;t exported. But as a language construct it makes sense not to Marshal private fields so that (like you mentioned) it is aligned with how the rest of the language works</p> <p>Let&#39;s say, for example, you get a struct from a library that describes a user. The fields are id, name and email. It also has secretKey as a private field. Would you want it included if you marshalled the struct? It&#39;s information that your app didn&#39;t even know existed on the struct, but it&#39;s somehow showing up in the json</p></pre>throwlikepollock: <pre><p>I decided to look it up and try to figure out how to actually get private fields content. Based on <a href="http://stackoverflow.com/a/17982725/6189743" rel="nofollow">this</a> comment it sounds like it&#39;s quite difficult (ie, supported via &#34;normal&#34; reflect methods) to get the content.</p> <p>Do you disagree? I was having trouble doing it without the pointer hackery.</p></pre>j_d_q: <pre><p>It&#39;s not terribly difficult. <a href="https://play.golang.org/p/NJ721P5ZL_" rel="nofollow">Here&#39;s a short example</a></p></pre>throwlikepollock: <pre><p>Yea i knew it used reflection, i just didn&#39;t think those were available. Good to know they are! I&#39;ve used reflection in the past and swore i couldn&#39;t access private fields haha</p></pre>dirkharrington: <pre><p>fwiw this behavior is documented and is w.a.i.</p></pre>

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

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