golang-module / carbon

A simple, semantic and developer-friendly golang package for time

Home Page:https://pkg.go.dev/github.com/golang-module/carbon/v2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cannot parse string null

a8ss opened this issue · comments

commented
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":""}

commented

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