Skip to content

Commit

Permalink
Update documentation on JSON serialization with the new json/encoding…
Browse files Browse the repository at this point in the history
… interface implementation
  • Loading branch information
emirpasic committed Apr 12, 2022
1 parent d7c0bc8 commit 74bf8a8
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1355,10 +1355,12 @@ All data structures can be serialized (marshalled) and deserialized (unmarshalle
Outputs the container into its JSON representation.

Typical usage for key-value structures:

```go
package main

import (
"encoding/json"
"fmt"
"github.com/emirpasic/gods/maps/hashmap"
)
Expand All @@ -1369,18 +1371,21 @@ func main() {
m.Put("b", "2")
m.Put("c", "3")

json, err := m.ToJSON()
bytes, err := json.Marshal(m) // Same as "m.ToJSON(m)"
if err != nil {
fmt.Println(err)
}
fmt.Println(string(json)) // {"a":"1","b":"2","c":"3"}
fmt.Println(string(bytes)) // {"a":"1","b":"2","c":"3"}
}
```

Typical usage for value-only structures:

```go
package main

import (
"encoding/json"
"fmt"
"github.com/emirpasic/gods/lists/arraylist"
)
Expand All @@ -1389,11 +1394,11 @@ func main() {
list := arraylist.New()
list.Add("a", "b", "c")

json, err := list.ToJSON()
bytes, err := json.Marshal(list) // Same as "list.ToJSON(list)"
if err != nil {
fmt.Println(err)
}
fmt.Println(string(json)) // ["a","b","c"]
fmt.Println(string(bytes)) // ["a","b","c"]
}
```

Expand Down

0 comments on commit 74bf8a8

Please sign in to comment.