cannot parse string null
a8ss opened this issue · comments
import ("github.com/golang-module/carbon/v2")
type Person struct {
Field1 string `json:"field1,omitempty"`
Field2 carbon.Carbon `json:"field2,omitempty" carbon:"omitempty"`
}
var p Person
err := json.Unmarshal([]byte("{\"field1\":null,\"field2\":null}"), &p)
fmt.Println(err, p)
cannot parse string "null" as carbon by layout "2006-01-02 15:04:05", please make sure the value and layout match { }
Add the omitempty tag to prevent parsing errors ?
{"field1":null,"field2":null}
-> {"field1":"","field2":""}
The responses of some APIs are null, and I have no control over them.
Supported since v2.3.10
type Person struct {
Birthday1 carbon.Carbon
Birthday2 carbon.Carbon
}
str := `{
"birthday1":"",
"birthday2":null
}`
var person Person
unmarshalErr := json.Unmarshal([]byte(str), &person)
fmt.Println(unmarshalErr) // nil
fmt.Println(person.Birthday1.String()) // empty string
fmt.Println(person.Birthday2.String()) // empty string