[GODRIVER-712] ObjectId, json-omitempty Created: 17/Dec/18  Updated: 27/Oct/23  Resolved: 07/Jan/19

Status: Closed
Project: Go Driver
Component/s: JSON & ExtJSON
Affects Version/s: 0.1.0
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Dávid Mikuš Assignee: Kristofer Brandow (Inactive)
Resolution: Gone away Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

E.g

type Obj struct {
    SomeId objectid.ObjectID `bson:"listId,omitempty" json:"someId,omitempty"`
}

When an object is marshalled with SomeId ==  primitive.NilObjectID and omitempty tag, it shouldn't be included in the marshalled string.

Now it's marshalled into

{
   "someId: "00000000000000000000000"
}



 Comments   
Comment by Matthew Browning [ 29/Jan/19 ]

A question related to this. I've decoded the bson data into a struct, want to send it off to the client as json. Looking at the bson.MarshalExtJSON function it seems that it's in a different looking format of JSON than I would want to the client. Is there a way to define my on structure for JSON and omitempty nil object ids?

Thanks

Comment by Kristofer Brandow (Inactive) [ 17/Dec/18 ]

Hi david.mikus@flowup.cz,

The encoding/json.Marshal function only checks if an array has a length of zero, not if all of it's values are zero. If you instead use the bson.MarshalExtJSON function it will omit the zero value for primitive.ObjectID.

--Kris

Generated at Thu Feb 08 08:34:47 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.