英文:
golang appengine outgoing json
问题
所以我正在使用Go语言的App Engine和Go Endpoints包...
我使用结构体来编组和解组我的JSON传入请求和传出响应...
type BusinessWorker struct {
Wid string `json:"wid" datastore:"Worker_id" endpoints:"req,desc=Worker id. string value"`
Phone string `json:"phone" datastore:"Phone" endpoints:"req,desc=Worker phone number. string value"`
}
所以,正如你所看到的,在验证数据之后,这个对象会被保存到或从数据存储中加载...
我的问题是...
有很多情况下,我不想在响应中包含保存在数据存储中的所有数据... 是否有一种属性可以给予参数,以便我只在传入请求中使用,而不在响应中包含它?
这似乎是非常基本的... 但我找不到它...
英文:
So I run golang appengine with go endpoints package ...
I use structs to marshal and un marshal my json incoming requests and out going responses ..
type BusinessWorker struct {
Wid string `json:"wid" datastore:"Worker_id" endpoints:"req,desc=Worker id. string value"`
Phone string `json:"phone" datastore:"Phone" endpoints:"req,desc=Worker phone number. string value"`
}
So as you can see after I validate the data this obj is saved or loaded to/from the datastore ..
My question is ..
there are many cases I dont want to respond with all my data that is saved in the datastore .. is there some sort of attribute that I can give to the param that i dont wanna include in my response only in my incoming requests ?
It seems so elementary .. and I cant find it .. ?
答案1
得分: 1
也许你想尝试以下方法之一或组合使用:
- 使用标签"-"来忽略字段。例如:
json:"-"
- 在你的
json:
标签中加入omitempty
,这将导致该字段不会包含在生成的 JSON 中。因此,你可以在序列化为 JSON 之前将要隐藏的字段设置为nil
。例如:json:"myName,omitempty"
- 使用类似jinzhu的copier的项目,它可以让你将实体复制到一个简化的结构中,或者你可以自己实现一个(JSON的解组和组合可以产生类似的结果)。
有关JSON包的更多详细信息,请参阅Golang Json marshal文档。
英文:
Maybe you would like to try one or a combination of the following approaches:
- Tag of "-" so that the field is ignored. e.g.
json:"-"
- omitempty can be included in your 'json:' and will cause the field not to be included in the resulting json. So you could set the fields you want to hide to nil, prior to serializing to json. e.g.
json:"myName,omitempty"
- copier - there are some projects like: jinzhu's copier that would allow you to copy your entity to a simplified structure, or you could roll your own. (a combination of JSON un-marshalling and marshalling can produce similar results).
For more details about the JSON package see Golang Json marshal docs
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论