Skip to content

Commit 228ac2d

Browse files
authored
Added MarshalJSON for ObjectID
Currently, if we sent data to client, client gets data and type of id is array. MarshalJSON encodes the ObjectID in to string.
1 parent b7c48f5 commit 228ac2d

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

bson/objectid/objectid.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ func FromHex(s string) (ObjectID, error) {
7777
return oid, nil
7878
}
7979

80+
// MarshalJSON returns the ObjectID as a string
81+
func (id *ObjectID) MarshalJSON() ([]byte, error) {
82+
return json.Marshal(id.Hex())
83+
}
84+
8085
// UnmarshalJSON populates the byte slice with the ObjectID. If the byte slice is 64 bytes long, it
8186
// will be populated with the hex representation of the ObjectID. If the byte slice is twelve bytes
8287
// long, it will be populated with the BSON representation of the ObjectID. Otherwise, it will

0 commit comments

Comments
 (0)