An open API service indexing awesome lists of open source software.

https://github.com/infiniteloopcloud/json

Copy of Go's built-in json with IsZero support
https://github.com/infiniteloopcloud/json

golang golang-json golang-library golang-package json

Last synced: 2 months ago
JSON representation

Copy of Go's built-in json with IsZero support

Awesome Lists containing this project

README

          

# json

Copy of Golang's json library with IsZero feature from [CL13977](https://go-review.googlesource.com/c/go/+/13977/)

## Usage

Exactly the same as Go's [JSON](https://pkg.go.dev/encoding/json). Plus...

```go
package main

import (
"fmt"

"github.com/infiniteloopcloud/json"
)

type nullString struct {
Valid bool
String string
}

func (n nullString) IsZero() bool {
return !n.Valid
}

type RandomDataSet struct {
Data1 nullString `json:"data1,omitempty"`
Data2 nullString `json:"data2,omitempty"`
}

func main() {
result, err := json.Marshal(RandomDataSet{Data1: nullString{Valid: false, String: "test"}})
if err != nil {
panic(err)
}
fmt.Println(string(result))
// Output: {}

result, err = json.Marshal(RandomDataSet{Data1: nullString{Valid: true, String: "test"}})
if err != nil {
panic(err)
}
fmt.Println(string(result))
// Output: {"data1":{"Valid":true,"String":"test"}}
}
```

## Contribution

These changes automatically generated by [jsongen](https://github.com/infiniteloopcloud/jsongen).

```shell
git checkout -b go1.x.x
jsongen --version go1.x.x --destination .
```