When unmarshaling JSON into a Go struct, discerning whether a field is null or simply absent can be a challenge. Both scenarios result in a nil value within the struct, making it difficult to determine the field's original presence.
To address this issue, let's explore two solutions:
1. Using Generics (Go 1.18 )
Go 1.18 introduces generics, allowing us to create a generic struct that tracks the presence of JSON values:
type Optional[T any] struct { Defined bool Value *T }
This Optional struct uses a Defined field to indicate whether the value exists in the JSON payload. When the field is present, the Value field holds the unmarshaled value. By incorporating this struct into your own structs, you can distinguish between null and absent fields:
type Payload struct { Field1 Optional[string] `json:"field1"` Field2 Optional[bool] `json:"field2"` Field3 Optional[int32] `json:"field3"` }
2. Custom Type (Pre-Generics)
Before generics, we can create a custom type to handle optional values:
type OptionalString struct { Defined bool Value *string }
This type includes a Defined field to indicate presence and a Value field to hold the unmarshaled string. By defining a custom type for each type you need to handle (e.g., OptionalInt for integers), you can retain the information about field presence.
Examples:
Consider the following JSON payloads:
{ "somefield1":"somevalue1", "somefield2":null }
{ "somefield1":"somevalue1", }
Using our solutions, we can differentiate between these cases:
p1 := Payload{} _ = json.Unmarshal([]byte(`{ "somefield1": "somevalue1", "somefield2": null }`), &p1) fmt.Printf("Should be defined == true and value == nil: \n%+v\n\n", p1)
Output:
Should be defined == true and value == nil: {SomeField1:somevalue1 SomeField2:{Defined:true Value:<nil>}}
p2 := Payload{} _ = json.Unmarshal([]byte(`{ "somefield1": "somevalue1", }`), &p2) fmt.Printf("Should be defined == false: \n%+v\n\n", p2)
Output:
Should be defined == false: {SomeField1:somevalue1 SomeField2:{Defined:false Value:<nil>}}
These examples demonstrate how our solutions can distinguish between null and absent JSON fields, providing valuable information during deserialization.
The above is the detailed content of How to Differentiate Between Null and Absent JSON Fields in Go?. For more information, please follow other related articles on the PHP Chinese website!