JSON字段设置为null vs字段不存在

JSON字段设置为null vs字段不存在

问题描述:

Is there a way, in golang, to see if I can differentiate between a json field being set to null vs a json field not being there when unmarshalled into a struct? Because both set the value in the struct to be nil, but I need to know if the field was there to begin with and to see if someone set it to null.

{
  "somefield1":"somevalue1",
  "somefield2":null
}

VS

{
  "somefield1":"somevalue1",
}

Both jsons will be nil when unmarshalled into a struct. Any useful resources will be very appreciated!

在golang中是否有办法查看我是否可以区分设置为null的json字段与 解组为结构时,json字段不存在? 因为两者都将struct中的值设置为nil,但是我需要知道该字段是否以该字段开头,并查看是否有人将其设置为null。 p>

  {  
“ somefield1”:“ somevalue1”,
“ somefield2”:null 
} 
  code>  pre> 
 
 

VS p>

  {
“ somefield1”:“ somevalue1”,
} 
  code>  pre> 
 
 

当解组到结构中时,两个json都将为零。 任何有用的资源都将是 非常感谢! p> div>

Use json.RawMessage to "delay" the unmarshaling process to determine the raw byte before deciding to do something:

var data = []byte(`{
        "somefield1":"somevalue1",
        "somefield2": null
}`)

type Data struct {
    SomeField1 string          
    SomeField2 json.RawMessage
}

func main() {
    d := &Data{}

    _ = json.Unmarshal(data, &d)

    fmt.Println(d.SomeField1)

    if len(d.SomeField2) > 0 {
        if string(d.SomeField2) == "null" {
            fmt.Println("somefield2 is there but null")
        } else {
            fmt.Println("somefield2 is there and not null")
            // Do something with the data
        }
    } else {
        fmt.Println("somefield2 doesn't exist")
    }
}

See the playground https://play.golang.org/p/Wganpf4sbO

If you unmarshall the object into a map[string]interface{} then you can just check if a field is there

type unMarshalledObject map[string]interface{}
json.Unmarshall(input, unMarshhaledObject)
_, ok := unMarshalledObject["somefield2"]

Go Playground

Good question.

I believe you can use https://golang.org/pkg/encoding/json/#RawMessage as:

type MyMessage struct {
  somefield1 string
  somefield2 json.RawMessage
}

So after unmarshalling you should have []byte("null") in case of null and nil if missing.

Here is a playground code: https://play.golang.org/p/UW8L68K068

If struct field is a pointer, JSON decoder will allocate new variable if the field is present or leave it nil if not. So I suggest to use pointers.

type Data struct {
    StrField *string
    IntField *int
}
...
if data.StrField != nil {
    handle(*data.StrField)
}

By using github.com/golang/protobuf/ptypes/struct and jsonpb github.com/golang/protobuf/jsonpb, you can do like this:

func TestFunTest(t *testing.T) {
    p := &pb.KnownTypes{}
    e := UnmarshalString(`{"val":null}`, p)
    fmt.Println(e, p)
    p = &pb.KnownTypes{}
    e = UnmarshalString(`{"val":1}`, p)
    fmt.Println(e, p)
    p = &pb.KnownTypes{}
    e = UnmarshalString(`{"val":"string"}`, p)
    fmt.Println(e, p)
    p = &pb.KnownTypes{}
    e = UnmarshalString(`{}`, p)
    fmt.Println(e, p)
}

Output:

[ `go test -test.run="^TestFunTest$"` | done: 1.275431416s ]
    <nil> val:<null_value:NULL_VALUE > 
    <nil> val:<number_value:1 > 
    <nil> val:<string_value:"string" > 
    <nil> 
    PASS