Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GODRIVER-1765 Improve JSON marshaling/unmarshaling for bson.D, bson.M and bson.A. #1594

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
231 changes: 231 additions & 0 deletions bson/primitive/primitive.go
Expand Up @@ -12,6 +12,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"time"
)

Expand Down Expand Up @@ -208,6 +209,55 @@ func (d D) Map() M {
return m
}

// MarshalJSON encodes D into JSON.
func (d D) MarshalJSON() ([]byte, error) {
if d == nil {
return json.Marshal(nil)
}
var err error
var buf bytes.Buffer
buf.Write([]byte("{"))
enc := json.NewEncoder(&buf)
for i, e := range d {
err = enc.Encode(e.Key)
if err != nil {
return nil, err
}
buf.Write([]byte(":"))
err = enc.Encode(e.Value)
if err != nil {
return nil, err
}
if i < len(d)-1 {
buf.Write([]byte(","))
}
}
buf.Write([]byte("}"))
return json.RawMessage(buf.Bytes()).MarshalJSON()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of returning the bytes via json.RawMessage().MarshalJSON here rather than returning them directly (i.e. return buf.Bytes(), nil)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

// UnmarshalJSON decodes D from JSON.
func (d *D) UnmarshalJSON(b []byte) error {
dec := json.NewDecoder(bytes.NewReader(b))
t, err := dec.Token()
if err != nil {
return err
}
if t == nil {
*d = nil
return nil
}
if v, ok := t.(json.Delim); !ok || v != '{' {
return &json.UnmarshalTypeError{
Value: tokenString(t),
Type: reflect.TypeOf(*d),
Offset: dec.InputOffset(),
}
}
*d, err = jsonDecodeD(dec)
return err
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Optional] You could combine the logic in D.UnmarshalJSON and m.UnmarshalJSON using generics:

func newJsonObjDecoder[T D | M](b []byte, val *T) (*json.Decoder, error) {
	dec := json.NewDecoder(bytes.NewReader(b))

	t, err := dec.Token()
	if err != nil {
		return nil, err
	}

	if t == nil {
		*val = nil
		return nil, nil
	}

	if v, ok := t.(json.Delim); !ok || v != '{' {
		return nil, &json.UnmarshalTypeError{
			Value:  tokenString(t),
			Type:   reflect.TypeOf(*val),
			Offset: dec.InputOffset(),
		}
	}

	return dec, nil
}

}

// E represents a BSON element for a D. It is usually used inside a D.
type E struct {
Key string
Expand All @@ -223,9 +273,190 @@ type E struct {
// bson.M{"foo": "bar", "hello": "world", "pi": 3.14159}
type M map[string]interface{}

// UnmarshalJSON decodes M from JSON.
func (m *M) UnmarshalJSON(b []byte) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to define custom JSON unmarshaling logic for bson.M? Other than the type names being different, the JSON unmarshaling logic seems identical.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The custom unmarshaling for bson.M and bson.A is to handle nested structures correspondingly rather than simply unmarshal into interface{} as case tested at https://github.com/mongodb/mongo-go-driver/pull/1594/files#diff-1649d029623b36c04e8a0636bd65aab5bb1047410b1affb26206198a34a4c784R374-R381

dec := json.NewDecoder(bytes.NewReader(b))
t, err := dec.Token()
if err != nil {
return err
}
if t == nil {
*m = nil
return nil
}
if v, ok := t.(json.Delim); !ok || v != '{' {
return &json.UnmarshalTypeError{
Value: tokenString(t),
Type: reflect.TypeOf(*m),
Offset: dec.InputOffset(),
}
}
*m, err = jsonDecodeM(dec)
return err
}

// An A is an ordered representation of a BSON array.
//
// Example usage:
//
// bson.A{"bar", "world", 3.14159, bson.D{{"qux", 12345}}}
type A []interface{}

// UnmarshalJSON decodes A from JSON.
func (a *A) UnmarshalJSON(b []byte) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to define custom JSON unmarshaling logic for bson.A? Other than the type names being different, the JSON unmarshaling logic seems identical.

dec := json.NewDecoder(bytes.NewReader(b))
t, err := dec.Token()
if err != nil {
return err
}
if t == nil {
*a = nil
return nil
}
if v, ok := t.(json.Delim); !ok || v != '[' {
return &json.UnmarshalTypeError{
Value: tokenString(t),
Type: reflect.TypeOf(*a),
Offset: dec.InputOffset(),
}
}
*a, err = jsonDecodeA(dec, func(dec *json.Decoder) (interface{}, error) {
return jsonDecodeD(dec)
})
return err
}

func jsonDecodeD(dec *json.Decoder) (D, error) {
res := D{}
for {
var e E

t, err := dec.Token()
if err != nil {
return nil, err
}
key, ok := t.(string)
if !ok {
break
}
e.Key = key

t, err = dec.Token()
if err != nil {
return nil, err
}
switch v := t.(type) {
case json.Delim:
switch v {
case '[':
e.Value, err = jsonDecodeA(dec, func(dec *json.Decoder) (interface{}, error) {
return jsonDecodeD(dec)
})
if err != nil {
return nil, err
}
case '{':
e.Value, err = jsonDecodeD(dec)
if err != nil {
return nil, err
}
}
default:
e.Value = t
}

res = append(res, e)
}
return res, nil
}

func jsonDecodeM(dec *json.Decoder) (M, error) {
res := make(M)
for {
t, err := dec.Token()
if err != nil {
return nil, err
}
key, ok := t.(string)
if !ok {
break
}

t, err = dec.Token()
if err != nil {
return nil, err
}
switch v := t.(type) {
case json.Delim:
switch v {
case '[':
res[key], err = jsonDecodeA(dec, func(dec *json.Decoder) (interface{}, error) {
return jsonDecodeM(dec)
})
if err != nil {
return nil, err
}
case '{':
res[key], err = jsonDecodeM(dec)
if err != nil {
return nil, err
}
}
default:
res[key] = t
}
}
return res, nil
}

func jsonDecodeA(dec *json.Decoder, objectDecoder func(*json.Decoder) (interface{}, error)) (A, error) {
res := A{}
done := false
for !done {
t, err := dec.Token()
if err != nil {
return nil, err
}
switch v := t.(type) {
case json.Delim:
switch v {
case '[':
a, err := jsonDecodeA(dec, objectDecoder)
if err != nil {
return nil, err
}
res = append(res, a)
case '{':
d, err := objectDecoder(dec)
if err != nil {
return nil, err
}
res = append(res, d)
default:
done = true
}
default:
res = append(res, t)
}
}
return res, nil
}

func tokenString(t json.Token) string {
switch v := t.(type) {
case json.Delim:
switch v {
case '{':
return "object"
case '[':
return "array"
}
case bool:
return "bool"
case float64:
return "number"
case json.Number, string:
return "string"
}
return "unknown"
}