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

Marshal empty as null #9

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions bool.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions byte.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions cmd/optional/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,16 @@ func ({{ .VariableName }} {{ .OutputName }}) MarshalJSON() ([]byte, error) {
if {{ .VariableName }}.Present() {
return json.Marshal({{ .VariableName }}.value)
}
var zero {{ .TypeName }}
return json.Marshal(zero)
return json.Marshal(nil)
}

func ({{ .VariableName }} *{{ .OutputName }}) UnmarshalJSON(data []byte) error {

if string(data) == "null" {
{{ .VariableName }}.value = nil
return nil
}

var value {{ .TypeName }}

if err := json.Unmarshal(data, &value); err != nil {
Expand Down
11 changes: 8 additions & 3 deletions cmd/optional/testdata/optional_bar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions cmd/optional/testdata/optional_foo.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions cmd/optional/testdata/string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions complex128.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions complex64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions error.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions float32.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions float64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions int.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions int16.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions int32.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions int64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading