英文:
difference between Json.Unmarshal() and gin.BindJson()
问题
你好,我是你的中文翻译助手。以下是你要翻译的内容:
嗨,我是gin框架和golang的新手,我想知道Json.Unmarshal()和gin.BindJson()之间有什么区别。
在教程中,他正在构建一个视频服务,其中有两个方法,save和findall。
func (vc *videoControllerStruct) Save(ctx *gin.Context) entity.Video {
video := entity.Video{}
ctx.ShouldBind(&video) // ctx.BindJson(&video)
vc.services.Save(video)
return video
}
如你所见,他首先使用了ctx.BindJson,然后改为了ShouldBind(我不知道为什么)。有没有办法用json.unmarshal重写这段代码?
英文:
hi i'm new to gin framework and golang and i was wondering what's the difference between Json.Unmarshal() and gin.BindJson()
in the tutorial he's building a video service which has two methods, save and findall
func (vc *videoControllerStruct) Save(ctx *gin.Context) entity.Video {
video := entity.Video{}
ctx.ShouldBind(&video) // ctx.BindJson(&video)
vc.services.Save(video)
return video
as you can see he used ctx.BindJson first and then changed it to ShouldBind (idk why)
is there a way to re-write this code with json.unmarshal?
答案1
得分: 3
ShouldBind
的代码如下:
func (c *Context) ShouldBind(obj any) error {
b := binding.Default(c.Request.Method, c.ContentType())
return c.ShouldBindWith(obj, b)
}
它主要检查请求的方法和内容类型,并根据此解析绑定。因此,传入的内容可以是JSON之外的其他内容。
BindJson
只是MustBindWith(obj, binding.JSON)
的别名,它严格尝试将请求体解组为JSON。传递任何其他内容将产生400的HTTP状态。
你也可以使用json.Unmarshal
,但你必须显式地访问Gin上下文中的请求体。
英文:
ShouldBind
looks like this:
func (c *Context) ShouldBind(obj any) error {
b := binding.Default(c.Request.Method, c.ContentType())
return c.ShouldBindWith(obj, b)
}
It basically checks for the request method and content type, and based on that, it resolves the binding. So, the incoming content can be something else than JSON.
BindJson
is just an alias for MustBindWith(obj, binding.JSON)
that strictly tries to unmarshal the request body to JSON. Passing any other content will produce a 400 HTTP status.
You could also use json.Unmarshal
, but you'd have to explicitly access the request body in a Gin context.
答案2
得分: 2
json.Unmarshal和gin.BindJson之间的主要区别是:
- json.Unmarshal操作原始的JSON数据,而gin.BindJson将请求体绑定到一个结构体上。
- json.Unmarshal需要手动获取JSON数据([]byte),而gin.BindJson可以透明地从请求中获取JSON数据。
- gin.BindJson可以进行额外的验证和绑定,而json.Unmarshal只是将原始的JSON数据解组。
要在gin中使用json.Unmarshal,你需要像下面这样做:
func (vc *videoController) Save(ctx *gin.Context) {
// 获取JSON数据
var jsonData []byte
if ctx.Request.Body != nil {
jsonData, _ = ioutil.ReadAll(ctx.Request.Body)
}
// 声明video结构体
var video entity.Video
// 解组到结构体中
err := json.Unmarshal(jsonData, &video)
if err != nil {
// 处理错误
}
// 保存video
vc.services.Save(video)
// 返回保存的video
return video
}
英文:
The main differences between json.Unmarshal and gin.BindJson are:
- json.Unmarshal operates on raw JSON data, while gin.BindJson binds
from a request body to a struct. - json.Unmarshal requires manually getting the JSON data ([]byte),
while gin.BindJson handles getting JSON data from request
transparently. - gin.BindJson can do extra validation and binding, while
json.Unmarshal just unmarshals raw JSON.
To use json.Unmarshal with gin, you would have to do something like:
func (vc *videoController) Save(ctx *gin.Context) {
// Get JSON data
var jsonData []byte
if ctx.Request.Body != nil {
jsonData, _ = ioutil.ReadAll(ctx.Request.Body)
}
// Declare video struct
var video entity.Video
// Unmarshal into struct
err := json.Unmarshal(jsonData, &video)
if err != nil {
// handle error
}
// Save video
vc.services.Save(video)
// Return saved video
return video
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论