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

ToJSON problem #132

Closed
qianqianyeye opened this issue Dec 25, 2019 · 4 comments
Closed

ToJSON problem #132

qianqianyeye opened this issue Dec 25, 2019 · 4 comments

Comments

@qianqianyeye
Copy link

qianqianyeye commented Dec 25, 2019

m := linkedhashmap.New() m1:= linkedhashmap.New() c :=t{S:"sss",A:"dddd"} m1.Put("c",c) m.Put("b","aaaa") m.Put("a","aaaa") m.Put("c",m1) mb,err:=m.ToJSON() if err != nil { fmt.Println(err) } fmt.Println(string(mb))
linkedhashmap contain linkedhashmap, toJSON result is ni(Code above mb inside c is nil)l,I added some code in this func

`// ToJSON outputs the JSON representation of map.
func (m *Map) ToJSON() ([]byte, error) {
var b []byte
buf := bytes.NewBuffer(b)

buf.WriteRune('{')

it := m.Iterator()
lastIndex := m.Size() - 1
index := 0

for it.Next() {
	var km []byte
	var err error
	switch it.Key().(type) {
	case *Map:
		km,err =it.Key().(*Map).ToJSON()
	default:
		km, err = json.Marshal(it.Key())
	}
	//km, err := json.Marshal(it.Key())
	if err != nil {
		return nil, err
	}
	buf.Write(km)

	buf.WriteRune(':')
	var vm []byte
	switch it.Value().(type) {
	case *Map:
		vm,err =it.Value().(*Map).ToJSON()
		break
	default:
		vm, err = json.Marshal(it.Value())
	}
	if err != nil {
		return nil, err
	}
	buf.Write(vm)

	if index != lastIndex {
		buf.WriteRune(',')
	}

	index++
}

buf.WriteRune('}')

return buf.Bytes(), nil

}`

@JyothirmaiNaga
Copy link

Can you please also add json MarshalIndent method to it.

@godcong
Copy link

godcong commented Jul 3, 2020

hm := hashmap.New()
hm.Put("a", "1")
hm.Put("b", "2")

hmc := hashmap.New()
hmc.Put("d", "4")
hm.Put("c", hmc)
hm.Put("e", hmc)
json, err := hm.ToJSON()

result string:
{"a":"1","b":"2","c":{},"e":{}}

reverse:
{"a":"1","b":"2","c":{"d","4"},"e":{"d","4"}}

	result := `{"a":"1","b":"2","c":{"d":"4"},"e":{"d":"4"}}`
	hm3 := hashmap.New()
	err = hm3.FromJSON([]byte(result))
	get, found := hm3.Get("e")
	fmt.Println(hm3, err)
	fmt.Println(get, found)
	fmt.Println(get.(*hashmap.Map))

panic: interface conversion: interface {} is map[string]interface {}, not *hashmap.Map
it can't unmarshal data(c and e) to hashmap

@emirpasic
Copy link
Owner

@godcong

GoDS does not unmarshal nested data structures at the moment. This could be solved by avoiding golang's json encode/decode interfaces and writing our own GoDS marshaler as discussed in #171 (comment)
Hence you get the panic as the get is nested map (standard map[string]interface{} type) and can not be type asserted to be a GoDS hashmap.

So I will close this issue and leave the other open to remind us of implementing this custom JSON umarshaller that will unmarshal JSON to GoDS data structures even in nested situations.

@emirpasic
Copy link
Owner

@qianqianyeye
I do not understand the question and I've tried to run your example:

import (
	"fmt"
	"github.com/emirpasic/gods/maps/linkedhashmap"
)

// LinkedHashMapExample to demonstrate basic usage of LinkedHashMapExample
func main() {
	m := linkedhashmap.New()
	m1 := linkedhashmap.New()
	c := map[string]interface{}{"S": "sss", "A": "dddd"}
	m1.Put("c", c)
	m.Put("b", "aaaa")
	m.Put("a", "aaaa")
	m.Put("c", m1)
	mb, err := m.ToJSON()
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(string(mb))
}

Output

{
	"b": "aaaa",
	"a": "aaaa",
	"c": {
		"c": {
			"A": "dddd",
			"S": "sss"
		}
	}
}

As far as I can see,this looks as expected?

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

4 participants