Skip to content

Using MarshallJSON() on objects yields strange results #404

@mustafaakin

Description

@mustafaakin
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions