Categorygithub.com/aarondl/json
repositorypackage
0.0.0-20221020222930-8b0db17ef1bf
Repository: https://github.com/aarondl/json.git
Documentation: pkg.go.dev

# README

json

Fork of the official JSON package with a single addition of an interface that allows custom types to decide if they should be omitted or not when omitempty is in the struct tag.

Implement the following interface to be able to omit a field of a custom type from the output:

type isZeroer interface {
	MarshalJSONIsZero() bool
}

Example

package main

import (
	"fmt"

	"github.com/aarondl/json"
)

type OmitMe struct{}

func (OmitMe) MarshalJSONIsZero() bool { return true }
func (o OmitMe) MarshalJSON() ([]byte, error) {
	return []byte(`5`), nil
}

func main() {
	a := struct {
		NotOmitted OmitMe
		Omitted    OmitMe `json:",omitempty"`
	}{}

	b, err := json.MarshalIndent(a, "", "  ")
	if err != nil {
		panic(err)
	}
	fmt.Printf("%s\n", b)
	// {
	//    "NotOmitted": 5
	// }
}