mattn / anko

Scriptable interpreter written in golang

Home Page:http://play-anko.appspot.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

json: unsupported type: map[interface {}]interface {}

chyroc opened this issue · comments

new map data type is map[interface {}]interface {}

and json.Marshal of "encoding/json" not support it.

two options:

  1. add buildin function dumps and loads
  2. replace "encoding/json" to "github.com/json-iterator/go", it support interface{} map key

or else better answer?

thank you .

package main

import (
	"fmt"
	"encoding/json"

	"github.com/json-iterator/go"
)

func main() {
	a, err := jsoniter.Marshal(map[interface{}]interface{}{"1": "1", 1: 2, false: 3,})
	fmt.Printf("%s - %v\n", a, err)

	b, err := json.Marshal(map[interface{}]interface{}{"1": "1", 1: 2, false: 3,})
	fmt.Printf("%s - %v\n", b, err)
}

output:

{"1":"1","1":2,"false":3} - <nil>
 - json: unsupported type: map[interface {}]interface {}

Could do something like this:

package main

import (
	"fmt"
	"log"

	"github.com/mattn/anko/packages"
	"github.com/mattn/anko/vm"
)

func main() {

	env := vm.NewEnv()
	packages.DefineImport(env)

	err := env.DefineType("mapStringInterface", map[string]interface{}{})
	if err != nil {
		log.Fatalf("DefineType error: %v\n", err)
	}

	script := `
fmt = import("fmt")
json = import("encoding/json")
a = make(mapStringInterface)
a["b"] = "b"
c, err = json.Marshal(a)
if err != nil {
	fmt.Println(err)
}
`

	_, err = env.Execute(script)
	if err != nil {
		log.Fatalf("execute error: %v\n", err)
	}

	dataInterface, err := env.Get("c")
	if err != nil {
		log.Fatalf("Get error: %v\n", err)
	}

	data := dataInterface.([]byte)
	fmt.Println(string(data))
}

@chyroc
Can this be closed?

i think json.Marshal should support buildin type map without mapStringInterface

or we should add new function map() to new a map whitch is mapStringInterface

Just need better support for creating custom types inside of Anko. It is on my to do list. This way don't need to do the type outside of the script.

Improved make: #302

@chyroc Good to close?

@mattn Close this?