Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
chyroc opened this issue Jul 18, 2018 · 8 comments
Closed

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

chyroc opened this issue Jul 18, 2018 · 8 comments

Comments

@chyroc
Copy link
Contributor

chyroc commented Jul 18, 2018

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 .

@chyroc
Copy link
Contributor Author

chyroc commented Jul 18, 2018

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 {}

@MichaelS11
Copy link
Contributor

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))
}

@MichaelS11
Copy link
Contributor

@chyroc
Can this be closed?

@chyroc
Copy link
Contributor Author

chyroc commented Jul 19, 2018

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

@MichaelS11
Copy link
Contributor

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.

@MichaelS11
Copy link
Contributor

Improved make: #302

@MichaelS11
Copy link
Contributor

@chyroc Good to close?

@MichaelS11
Copy link
Contributor

@mattn Close this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants