add (*Object).Into(v any)
Currently, quickjs.Object is simply a struct containing a json string.
I propose adding a Into(v any) method which calls json.Unmarshal.
This would eliminate error handling for MarshalJSON and make code more concise in general.
While you could use .String() instead of .MarshalJSON() to avoid the useless error, I fear this will cause reader confusion.
Example:
vm.RegisterFunc("test", func (obj quickjs.Object) error {
var v map[string]string
- data, err := obj.MarshalJSON()
- if err != nil { // this can never happen
- return err
- }
- if err := json.Unmarshal(data, &v); err != nil {
+ if err := obj.Into(&v); err != nil {
return err
}
// ...
})