欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Golang JSON的进阶用法实例讲解

程序员文章站 2022-06-05 22:09:47
痛点 json 是当前最常用的数据传输格式之一,纯文本,容易使用,方便阅读,在通信过程中大量被使用。  你是否遇到过json中某个字段填入某种类型都适合而陷入...

痛点

json 是当前最常用的数据传输格式之一,纯文本,容易使用,方便阅读,在通信过程中大量被使用。 

你是否遇到过json中某个字段填入某种类型都适合而陷入两难境地? (例如:定义了一个port字段,你却不知道是填入 8080 ,还是 “8080” 的尴尬局面)

你是否遇到过json反解析报错是因为填入字段的类型不匹配导致的?例如:

json: cannot unmarshal number into go struct field host.port of type string

你是否有json某字段兼容2种或者多种的数据结构的需求?

你是否想让程序更优雅,更具有适配性,而不在被这些小细节头痛?

如果你有或者你想,获取你可以看看这篇文章。

重现问题

我们给了用户一个json如下:

{
 "name":"yulibaozi",
 "port":8080
}

但是,业务方却误填了”8080”,结果我们程序反解析报错,导致业务失败。

json: cannot unmarshal number into go struct field host.port of type string

或许你认为这是业务方的问题,但我认为我们可以更优雅的解决这个问题。

如何解决问题

我们先定义了一个结构体

type host struct {
 name string `json:"name"`
 port port `json:"port"`
}

心细的你会发现,port既不是int也不是string类型,而是port类型,而port类型是:

type type int

const (
 int type = iota
 string
)

type port struct {
 type type
 intval int
 strval string
}

在port结构体中,我们发现了type类型, 而type类型包括了int,string两种类型。接下来就非常重要了,我们需要实现以下这两个接口。

json.unmarshaller interface
json.marshaller interface

实现代码如下:

type port struct {
 type type
 intval int
 strval string
}

// 实现 json.unmarshaller 接口
func (port *port) unmarshaljson(value []byte) error {
 if value[0] == '"' {
  port.type = string
  return json.unmarshal(value, &port.strval)
 }
 port.type = int
 return json.unmarshal(value, &port.intval)
}

// 实现 json.marshaller 接口
func (port port) marshaljson() ([]byte, error) {
 switch port.type {
 case int:
  return json.marshal(port.intval)
 case string:
  return json.marshal(port.strval)
 default:
  return []byte{}, fmt.errorf("impossible port.type")
 }
}

接下来测试:

测试反解析

测试反解析int

给出json数据:

{"name":"yulibaozi","port":8090}

反解析得到的结构体数据如下:

&{name:yulibaozi port:{type:0 intval:8090 strval:}}

测试反解析string:

给出json数据:

{"name":"yulibaozi","port":"8090"}

反解析得到的结构体数据如下:

&{name:yulibaozi port:{type:1 intval:0 strval:8090}}

测试编码的json

测试编码int的结构体如下:

host := &host{
   name: "yulibaozi",
   port: port{
     type:  int,
     intval: 8080,
   },
 }

编码后的json如下:

{"name":"yulibaozi","port":8080}

测试编码string的结构体如下:

host := &host{
   name: "yulibaozi",
   port: port{
     type:  string,
     strval: "8080",
   },
 }

编码后的json数据如下:

{"name":"yulibaozi","port":"8080"}

在反编码测试中,你会发现当json填入的类型不同时,会编码到结构体中对应的字段中。

在编码测试中, 具体编码那个数据是由type来确定的。

总结

其实,这篇文章只是分享了下json中使用的小技巧,他打破了在使用json时,需要呆板的数据结构的印象,转而走向了多变,灵活跳脱的风格,其实,这这个小tips的核心在于实现unmarshaller,marshaller这两个结构体,他们的实现是这个分享的关键,当然,你可以实现如开篇所说的那样,json某字段兼容2种及以上结构,当然,你也可以对yaml,toml等进行折腾,都会得到你想要的答案。

好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。