Skip to content

Feature proposal: allow_omitempty_struct flag #427

Description

@denis96z

It may be convenient in some cases to allow omit empty structs when marshalling, for example to save bytes, as default golang unmarshaller does not make difference between empty object and omitted object. So the proposal is to add flag to easyjson generator to allow omit empty structs without breaking default behaviour.

Example:

package types

//go:generate easyjson -allow_omitempty_struct

//easyjson:json
type MyStruct struct {
    N MyNestedStruct `json:"n,omitempty"`
}

type MyNestedStruct struct {
    A int `json:"a,omitempty"`
    B int `json:"b,omitempty"`
}
package main

import "types"

func main() {
    b, _ := json.Marshal(types.MyStruct{})
    fmt.Println(string(b)) // {}
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions