Skip to content

infiniteloopcloud/json

Repository files navigation

json

Copy of Golang's json library with IsZero feature from CL13977

Usage

Exactly the same as Go's JSON. Plus...

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.

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