Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-1332

primitive.ObjectID json omitempty doesn't work

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Works as Designed
    • Icon: Major - P3 Major - P3
    • None
    • None
    • BSON
    • None

    Description

      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.

      Attachments

        Activity

          People

            divjot.arora@mongodb.com Divjot Arora (Inactive)
            epelc@greatcloak.com Ed Pelc
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: