Go Json包:easyjson

最近在项目中遇到了easyjson包的使用,于是自己系统的学习了以下。

1、easyjson的安装:

go get -u github.com/mailru/easyjson/
go install  github.com/mailru/easyjson/easyjsonorgo 
build -o easyjson github.com/mailru/easyjson/easyjson

2、验证是否安装成功,在终端输入easyjson

[@ledudeMacBook-Pro:goTest (master)]$ easyjson
Usage of easyjson:
  -all
        generate marshaler/unmarshalers for all structs in a file
  -build_tags string
        build tags to add to generated file
  -disallow_unknown_fields
        return error if any unknown field in json appeared
  -leave_temps
        do not delete temporary files
  -lower_camel_case
        use lowerCamelCase names instead of CamelCase by default
  -no_std_marshalers
        don't generate MarshalJSON/UnmarshalJSON funcs
  -noformat
        do not run 'gofmt -w' on output file
  -omit_empty
        omit empty fields by default
  -output_filename string
        specify the filename of the output
  -pkg
        process the whole package instead of just the given file
  -snake_case
        use snake_case names instead of CamelCase by default
  -stubs
        only generate stubs for marshaler/unmarshaler funcs

3、使用:在需要序列化的结构体上面加上注解://easyjson:json

在终端输入命令:easyjson -all 含有序列化结构体的文件名

//easyjson:json
type User struct {
	Id   int    `json:"id"`
	Name string `json:"name"`
	Age  int    `json:"age"`
}

func Test(){
	u := User{Name:"yan1",Age:12}
	user1,err :=u.MarshalJSON()
	if err != nil {
		println("error")
	}
	println("after marshal user = ",user1)

	//json:=`{"id":11,"name":"yan","age":11}`
	user2 := User{}
	user2.UnmarshalJSON([]byte(user1))
	println("name = ",user2.Name)
}

在终端输入命令:easyjson -all user.go(文件路径),后会在user.go下生成一个user_easyjson.go文件。

在这个user_easyjson.go文件中会自动生成对应的序列化和反序列化方法:

 

// MarshalJSON supports json.Marshaler interface
func (v User) MarshalJSON() ([]byte, error) {
	w := jwriter.Writer{}
	easyjson9e1087fdEncodeGoTestEasyJson(&w, v)
	return w.Buffer.BuildBytes(), w.Error
}

// MarshalEasyJSON supports easyjson.Marshaler interface
func (v User) MarshalEasyJSON(w *jwriter.Writer) {
	easyjson9e1087fdEncodeGoTestEasyJson(w, v)
}

// UnmarshalJSON supports json.Unmarshaler interface
func (v *User) UnmarshalJSON(data []byte) error {
	r := jlexer.Lexer{Data: data}
	easyjson9e1087fdDecodeGoTestEasyJson(&r, v)
	return r.Error()
}

// UnmarshalEasyJSON supports easyjson.Unmarshaler interface
func (v *User) UnmarshalEasyJSON(l *jlexer.Lexer) {
	easyjson9e1087fdDecodeGoTestEasyJson(l, v)
}

 

在 Crow 库中,crow::json::rvalue 和 crow::json::wvalue 是两个不同的 JSON 类型,其中 crow::json::rvalue 是只读的 JSON 类型,而 crow::json::wvalue 是可写的 JSON 类型。 如果我们想要将 crow::json::wvalue 对象转换为 crow::json::rvalue 对象,可以直接将它们的引用进行转换,即: ```c++ const crow::json::rvalue& r = w; ``` 这里的 "w" 是一个 crow::json::wvalue 对象。 这种转换方式非常简单,因为 crow::json::rvalue 是 crow::json::wvalue 的基类,可以直接将 crow::json::wvalue 对象转换为 crow::json::rvalue 对象。 如果我们想要将 crow::json::rvalue 对象转换为 crow::json::wvalue 对象,则需要使用 crow::json::rvalue 类的成员函数 `dump()` 将其序列化为 JSON 字符串,然后使用 crow::json::load() 函数将其反序列化为一个 crow::json::wvalue 对象,例如: ```c++ const crow::json::rvalue& r = ...; std::string json_str = crow::json::dump(r); crow::json::wvalue w; crow::json::load(w, json_str); ``` 这里的 "r" 是一个 crow::json::rvalue 对象,`crow::json::dump(r)` 将其序列化为 JSON 字符串,然后使用 `crow::json::load()` 函数将其反序列化为一个 crow::json::wvalue 对象 "w"。 需要注意的是,由于 crow::json::wvalue 对象是可写的,因此在转换过程中可能会丢失一些信息。例如,如果 crow::json::rvalue 对象中含一些只读的属性,这些属性在转换为 crow::json::wvalue 对象时可能会被忽略。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值