JSON Serialization with Integer-keyed Maps #71

Closed
opened 2018-03-28 00:00:46 +00:00 by paralin · 4 comments
paralin commented 2018-03-28 00:00:46 +00:00 (Migrated from github.com)

The following example doesn't seem to work:

tm1 := treemap.NewWithIntComparator()
tm1.Put(1, "x")
tm1.Put(2, "b")
tm1.Put(1, "a")

data, _ := tm1.ToJSON()

tm2 := treemap.NewWithIntComparator()
tm2.FromJSON(data) // fails

The serialized JSON has the integer keys in quotes - {"1":"a","2":"b"} which is, of course, the only possibility conforming to JSON spec. However, this is counter-intuitive. Would it not be better to store an array of key/value pairs?

The following example doesn't seem to work: ``` tm1 := treemap.NewWithIntComparator() tm1.Put(1, "x") tm1.Put(2, "b") tm1.Put(1, "a") data, _ := tm1.ToJSON() tm2 := treemap.NewWithIntComparator() tm2.FromJSON(data) // fails ``` The serialized JSON has the integer keys in quotes - `{"1":"a","2":"b"}` which is, of course, the only possibility conforming to JSON spec. However, this is counter-intuitive. Would it not be better to store an array of key/value pairs?
emirpasic commented 2018-04-09 08:25:00 +00:00 (Migrated from github.com)

I am afraid that it be even more counter-intuitive to store a map data structure as an array JSON representation.

Given the JSON specification, I frankly do not see any way to make this more intuitive. For now, as you'd expect, this should work:

tm2 := treemap.NewWithStringComparator()
tm2.FromJSON(data) // succeeds 
I am afraid that it be even more counter-intuitive to store a map data structure as an array JSON representation. Given the JSON specification, I frankly do not see any way to make this more intuitive. For now, as you'd expect, this should work: ``` tm2 := treemap.NewWithStringComparator() tm2.FromJSON(data) // succeeds ```
paralin commented 2018-04-10 04:01:12 +00:00 (Migrated from github.com)

It doesn't work. It won't parse the produced json

It doesn't work. It won't parse the produced json
emirpasic commented 2018-04-10 10:00:54 +00:00 (Migrated from github.com)

It doesn't work. It won't parse the produced json

@paralin ok, i'll check it out.

> It doesn't work. It won't parse the produced json @paralin ok, i'll check it out.
emirpasic commented 2018-09-19 03:49:35 +00:00 (Migrated from github.com)

@paralin

this does work

package main

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

func main() {
	tm1 := treemap.NewWithIntComparator()
	tm1.Put(1, "x")
	tm1.Put(2, "b")
	tm1.Put(1, "a") // overwrites first key

	data, _ := tm1.ToJSON()

	tm2 := treemap.NewWithStringComparator()
	tm2.FromJSON(data) // does NOT fail

	fmt.Print(tm2.String()) // map[1:a 2:b]  CORRECT
}

the other issue of converting numbers to strings is explained above

@paralin this does work ```go package main import ( "fmt" "github.com/emirpasic/gods/maps/treemap" ) func main() { tm1 := treemap.NewWithIntComparator() tm1.Put(1, "x") tm1.Put(2, "b") tm1.Put(1, "a") // overwrites first key data, _ := tm1.ToJSON() tm2 := treemap.NewWithStringComparator() tm2.FromJSON(data) // does NOT fail fmt.Print(tm2.String()) // map[1:a 2:b] CORRECT } ``` the other issue of converting numbers to strings is explained above
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
golibs/gms-gods#71
No description provided.