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

Should empty bson.Raw follow the json.RawMessage encoding conventions?

    • Type: Icon: Question Question
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      If a user tries to encode a struct with a reference to an empty bson.Raw, the marshaler will return the following error:

      couldn't read length from src, not enough bytes. length=0
      

      This is because the encoder is attempting to marshal an empty byte slice into a BSON document which is invalid. You can use a pointer reference to avoid this error:

      	type rd2 struct {
      		Foo *bson.Raw
      	}
      

      The user can also use the "omitempty" tag:

      	type rd2 struct {
      		Foo bson.Raw `bson:",omitempty"`
      	}
      

      It’s interesting to note that the encoding/json package does not follow this convention and simply converts the RawMessage field to “null”: https://go.dev/play/p/fLnMX-nstJr?v=goprev

      Should the bson package determine if a "Raw" value is zero and then marshal the data to "null" as the default?

            Assignee:
            Unassigned Unassigned
            Reporter:
            preston.vasquez@mongodb.com Preston Vasquez
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: