primitive.ObjectID json omitempty doesn't work

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Works as Designed
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • Component/s: BSON
    • None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      package main
      
      import (
      	"encoding/json"
      	"fmt"
      
      	"go.mongodb.org/mongo-driver/bson/primitive"
      )
      
      func main() {
      	td := struct {
      		EmptyExample primitive.ObjectID `json:",omitempty"`
      	}{}
      
      	out, err := json.Marshal(td)
      	if err != nil {
      		panic(err)
      	}
      
      	fmt.Println(string(out))
      }
      

      You get the following output `

      {"EmptyExample":"000000000000000000000000"}

      ` instead of an omitted field because the `ObjectID` type is an alias for a 12 byte slice and it doesn't have a custom json marshaler.

            Assignee:
            Divjot Arora (Inactive)
            Reporter:
            Ed Pelc
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: