iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > GO >Golang GinWeb框架之如何掌握XML/JSON/YAML/ProtoBuf等渲染
  • 304
分享到

Golang GinWeb框架之如何掌握XML/JSON/YAML/ProtoBuf等渲染

2024-04-02 19:04:59 304人浏览 泡泡鱼
摘要

这篇文章主要讲解了“golang GinWEB框架之如何掌握XML/JSON/YAML/ProtoBuf等渲染”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“

这篇文章主要讲解了“golang GinWEB框架之如何掌握XML/JSON/YAML/ProtoBuf等渲染”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Golang GinWeb框架之如何掌握XML/jsON/YAML/ProtoBuf等渲染”吧!

XML,JSON,YAML,ProtoBuf等渲染

package main  import (   "GitHub.com/gin-gonic/gin"   "github.com/gin-gonic/gin/testdata/protoexample"   "net/Http" )  func main() {   r := gin.Default()    // gin.H is a shortcut for map[string]interface{}   // gin.H对象是一个map映射,键名为字符串类型, 键值是接口,所以可以传递所有的类型   r.GET("/someJSON", func(c *gin.Context) {     c.JSON(http.StatusOK, gin.H{"message": "hey", "status": http.StatusOK})   })    r.GET("/moreJSON", func(c *gin.Context) {     // You also can use a struct     var msg struct {       Name    string `json:"user"`       Message string       Number  int     }     msg.Name = "Lena"     msg.Message = "hey"     msg.Number = 123     // Note that msg.Name becomes "user" in the JSON     // Will output  :   {"user": "Lena", "Message": "hey", "Number": 123}      //JSON serializes the given struct as JSON into the response body. It also sets the Content-Type as "application/json".     //JSON方法将给定的结构序列化为JSON到响应体, 并设置内容类型Content-Type为:"application/json"     c.JSON(http.StatusOK, msg)   })    r.GET("/someXML", func(c *gin.Context) {     c.XML(http.StatusOK, gin.H{"message": "hey", "status": http.StatusOK})   })    r.GET("/someYAML", func(c *gin.Context) {     c.YAML(http.StatusOK, gin.H{"message": "hey", "status": http.StatusOK})   })    //Protocol buffers are a language-neutral, platfORM-neutral extensible mechanism for serializing structured data.   //Protocol buffers(简称ProtoBuf)是来自Google的一个跨语言,跨平台,用于将结构化数据序列化的可扩展机制,   //详见:https://developers.google.com/protocol-buffers   r.GET("/someProtoBuf", func(c *gin.Context) {     reps := []int64{int64(1), int64(2)}     label := "test"     // The specific definition of protobuf is written in the testdata/protoexample file.     // 使用protoexample.Test这个特别的protobuf结构来定义测试数据     data := &protoexample.Test{       Label: &label,       Reps:  reps,     }     // Note that data becomes binary data in the response  //将data序列化为二进制的响应数据     // Will output protoexample.Test protobuf serialized data     // ProtoBuf serializes the given struct as ProtoBuf into the response body.     // ProtoBuf方法将给定的结构序列化为ProtoBuf响应体     c.ProtoBuf(http.StatusOK, data)   })    // Listen and serve on 0.0.0.0:8080   r.Run(":8080") }  

安全的JSOn

使用SecureJSON方法保护Json不被劫持, 如果响应体是一个数组, 该方法会默认添加`while(1)`前缀到响应头,  这样的死循环可以防止后面的代码被恶意执行, 也可以自定义安全JSON的前缀.

package main  import (   "github.com/gin-gonic/gin"   "net/http" )  func main() {   r := gin.Default()    // You can also use your own secure json prefix   // 你也可以自定义安全Json的前缀   r.SecureJsonPrefix(")]}',\n")    //使用SecureJSON方法保护Json不被劫持, 如果响应体是一个数组, 该方法会默认添加`while(1)`前缀到响应头,  这样的死循环可以防止后面的代码被恶意执行, 也可以自定义安全JSON的前缀.   r.GET("/someJSON", func(c *gin.Context) {     names := []string{"lena", "austin", "foo"}      //names := map[string]string{     //  "hello": "world",     //}      // Will output  :   while(1);["lena","austin","foo"]     c.SecureJSON(http.StatusOK, names)   })    // Listen and serve on 0.0.0.0:8080   r.Run(":8080") }  

JSONP

使用JSONP可以实现跨域请求数据, 如果请求中有查询字符串参数callback, 则将返回数据作为参数传递给callback值(前端函数名),整体作为一个响应体,返回给前端.

JSONP是服务器与客户端跨源通信的常用方法. 最大特点就是简单适用, 老式浏览器全部支持, 服务器改造非常小, 它的基本思想是: 网页通过添加一个<script>元素, 向服务器请求JSON数据, 这种做法不受同源政策限制, 服务器收到请求后, 将数据放在一个指定名字的回调函数里传回来, 这样, 前端可以完成一次前端函数的调用, 而参数是后端返回的数据.

注意: 这种方式存在被劫持的风险

package main  import (   "github.com/gin-gonic/gin"   "net/http" )  func main() {   r := gin.Default()    r.GET("/JSONP", func(c *gin.Context) {     data := gin.H{       "foo": "bar",     }      //callback is x     // Will output  :   x({\"foo\":\"bar\"})     // 使用JSONP可以实现跨域请求数据, 如果请求中有查询字符串参数callback, 则将返回数据作为参数传递给callback值(前端函数名),整体作为一个响应体,返回给前端     //JSONP是服务器与客户端跨源通信的常用方法。最大特点就是简单适用,老式浏览器全部支持,服务器改造非常小。     //它的基本思想是,网页通过添加一个<script>元素,向服务器请求JSON数据,这种做法不受同源政策限制;服务器收到请求后,将数据放在一个指定名字的回调函数里传回来     c.JSONP(http.StatusOK, data)   })    // Listen and serve on 0.0.0.0:8080   r.Run(":8080")    // 模拟客户端,请求参数中有callback参数,值为x(前端函数名),最后响应内容为x("foo":"bar")   // curl http://127.0.0.1:8080/JSONP?callback=x }

AsciiJSON

使用ASCII编码, 将非ASCII的字符进行转义和编码, 生成纯ASCII编码的JSON

package main  import (   "github.com/gin-gonic/gin"   "net/http" )  func main() {   r := gin.Default()    r.GET("/someJSON", func(c *gin.Context) {     data := gin.H{       "lang": "GO语言",       "tag":  "<br>",     }      // 输出结果 : {"lang":"GO\u8bed\u8a00","tag":"\u003cbr\u003e"}     // AsciiJSON方法返回带有Unicode编码和转义组成的纯ASCII字符串     c.AsciiJSON(http.StatusOK, data)   })    // Listen and serve on 0.0.0.0:8080   r.Run(":8080") }  

不带转义的原始JSON

通常, JSON会将特殊的html字符转化为他们的unicode编码, 如标签`<`转为`\u003c` 使用PureJSON方法可以得到原始不做转义的字符串.

注意: 该方法至少需要Go版本1.6以上

package main  import "github.com/gin-gonic/gin"  func main() {   r := gin.Default()    // Serves unicode entities   r.GET("/json", func(c *gin.Context) {     c.JSON(200, gin.H{       "html": "<b>Hello, world!</b>",     })   })    // Serves literal characters   r.GET("/purejson", func(c *gin.Context) {     c.PureJSON(200, gin.H{       "html": "<b>Hello, world!</b>",     })   })    // listen and serve on 0.0.0.0:8080   r.Run(":8080") } 

感谢各位的阅读,以上就是“Golang GinWeb框架之如何掌握XML/JSON/YAML/ProtoBuf等渲染”的内容了,经过本文的学习后,相信大家对Golang GinWeb框架之如何掌握XML/JSON/YAML/ProtoBuf等渲染这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!

您可能感兴趣的文档:

--结束END--

本文标题: Golang GinWeb框架之如何掌握XML/JSON/YAML/ProtoBuf等渲染

本文链接: https://www.lsjlt.com/news/84257.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Golang GinWeb框架之如何掌握XML/JSON/YAML/ProtoBuf等渲染
    这篇文章主要讲解了“Golang GinWeb框架之如何掌握XML/JSON/YAML/ProtoBuf等渲染”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“...
    99+
    2024-04-02
  • Golang GinWeb框架之如何使用静态文件/模板渲染
    这篇文章主要讲解了“Golang GinWeb框架之如何使用静态文件/模板渲染”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Golang GinWeb框架之...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作