我想将JSON blob解码为Go结构,对其进行操作,然后将其编码回JSON。但是,JSON中的动态字段与我的struct无关,我想在序列化回JSON时维护它们。
例如:
{ "name": "Joe Smith",
"age": 42,
"phone": "614-555-1212",
"debug": True,
"codeword": "wolf" }
type Person struct {
Name string
Age uint
Phone string
}
var p Person
json.Unmarshal(data, &p)
// Happy birthday
p.Age++
data, _ = json.Marshal(p)
// Any way to maintain the "debug" and "codeword" fields -- which might not
// be known ahead of time?
我知道有一种可能性就是将所有内容解码为map[string]interface{}
,但是当你这样做时,事情会变得很难看。
有没有办法让两全其美?
答案 0 :(得分:6)
使用encoding/json
,无法对结构进行解码并将同一级别的未知字段保存为后重新编码。您可以做的是选择不使用json.RawMessage类型解码部分结构,如下所示:
type Person struct {
Name string
Address json.RawMessage
}
您可以通过实现自己的Unmarshaler来解决这个问题,该Marshaler将文档解码为地图并将未知密钥保存在结构的字段中,然后使用对应的labix.org/v2/mgo/bson将字段放回在编组之前。
出于好奇,您正在寻找的功能确实存在于inline tag flag,通过{{3}},目标是准确解决您所描述的用例
答案 1 :(得分:3)
原来我编写了自己的库来执行此操作:https://github.com/joeshaw/json-lossless
它建立在go-simplejson之上,只要结构被封送或解组,就将解析后的JSON状态保持在simplejson.Json
和代理结构之间。
使用示例:
package main
import (
"encoding/json"
"fmt"
"time"
"github.com/joeshaw/json-lossless"
)
type Person struct {
lossless.JSON `json:"-"`
Name string `json:"name"`
Age int `json:"age"`
Birthdate time.Time `json:"birthdate"`
}
func (p *Person) UnmarshalJSON(data []byte) error {
return p.JSON.UnmarshalJSON(p, data)
}
func (p Person) MarshalJSON() []byte, error) {
return p.JSON.MarshalJSON(p)
}
var jsondata = []byte(`
{"name": "David Von Wolf",
"age": 33,
"birthdate": "1980-09-16T10:44:40.295451647-04:00",
"phone": "614-555-1212"}
`)
func main() {
var p Person
err := json.Unmarshal(jsondata, &p)
if err != nil {
panic(err)
}
// Set values on the struct
p.Age++
// Set arbitrary keys not in the struct
p.Set("title", "Chief Wolf")
fmt.Printf("%#v\n", p)
data, err := json.Marshal(p)
if err != nil {
panic(err)
}
fmt.Println(string(data))
}
打印(格式化以供我阅读):
main.Person{JSON:lossless.JSON{json:(*simplejson.Json)(0x21020a190)},
Name:"David Von Wolf",
Age:34,
Birthdate:time.Time{sec:62473560280,
nsec:295451647,
loc:(*time.Location)(0x16de60)}}
{"age":34,
"birthdate":"1980-09-16T10:44:40.295451647-04:00",
"name":"David Von Wolf",
"phone":"614-555-1212",
"title": "Chief Wolf"}
答案 2 :(得分:1)
套餐go-simplejson
可以帮助完成这类工作。