Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot parse string null #225

Closed
a8ss opened this issue Mar 4, 2024 · 3 comments
Closed

cannot parse string null #225

a8ss opened this issue Mar 4, 2024 · 3 comments
Labels
Question Indicates that an issue, pull request, or discussion needs more information

Comments

@a8ss
Copy link

a8ss commented Mar 4, 2024

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 ?

@a8ss a8ss added the Question Indicates that an issue, pull request, or discussion needs more information label Mar 4, 2024
@gouguoyin
Copy link
Member

{"field1":null,"field2":null} -> {"field1":"","field2":""}

@a8ss
Copy link
Author

a8ss commented Mar 5, 2024

The responses of some APIs are null, and I have no control over them.

@gouguoyin
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question Indicates that an issue, pull request, or discussion needs more information
Development

No branches or pull requests

2 participants