Skip to content
forked from aarondl/json

Go json package fork for better omitempty

License

Notifications You must be signed in to change notification settings

ProlificLabs/json-opt

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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
	// }
}

About

Go json package fork for better omitempty

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%