robertkrimen / otto

A JavaScript interpreter in Go (golang)
http://godoc.org/github.com/robertkrimen/otto
MIT License
8.04k stars 584 forks source link

Implemented MarshalJSON for Value and Object #437

Closed snej closed 1 year ago

snej commented 2 years ago

Value and Object now conform to the json.Marshaler interface and produce the correct JSON when passed (directly or indirectly) to json.Marshal().

Before, both types would marshal as "{}" because they're structs with no public fields.

This fixes some nasty problems marshaling object trees that mix Go and JS collections. This can happen when you pass a Go collection into a JS function, which modifies it adding JS values; and then back in Go you marshal the collection to JSON. Before this commit the JS values would marshal to "{}". (The new unit test TestNestedJSONMarshaling demonstrates this.)

Fixes #262