rogchap / v8go

Execute JavaScript from Go

Home Page:https://rogchap.com/v8go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using MarshallJSON() on objects yields strange results

mustafaakin opened this issue · comments

ctx := v8.NewContext()
val, _ := ctx.RunScript("result = { name: 'mustafa'}; result", "main.js")
b, _ := val.MarshalJSON()
fmt.Println(val, string(b))

The outputs are:

[object Object] {"name":"mustafa"}

However when I run the following output does not care about my object and when I define another field its just an error

{ name: 'mustafa'} => mustafa "mustafa"
{name: 'mustafa', age: 32} => SyntaxError: Unexpected token ':'

Not sure what's going on here but it works on arrays without a problem

[1, false, 3.14, 'mustafa', {name: 'mustafa', age : 30}] 
=>
1,false,3.14,mustafa,[object Object] [1,false,3.14,"mustafa",{"name":"mustafa","age":30}]

Any pointers would be great. When I type the object definition to Chrome console or Node REPL it does not complain, so what is different here? I'm building an template execution engine and current parsing code is a quick hack and not ideal. https://github.com/mustafaakin/templ8go