-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from emirpasic/json
JSON serialization (marshaling) / deserialization (unmarshaling)
- Loading branch information
Showing
36 changed files
with
1,231 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright (c) 2015, Emir Pasic. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package containers | ||
|
||
// JSONSerializer provides JSON serialization | ||
type JSONSerializer interface { | ||
// ToJSON outputs the JSON representation of containers's elements. | ||
ToJSON() ([]byte, error) | ||
} | ||
|
||
// JSONDeserializer provides JSON deserialization | ||
type JSONDeserializer interface { | ||
// FromJSON populates containers's elements from the input JSON representation. | ||
FromJSON([]byte) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package examples | ||
|
||
import ( | ||
"fmt" | ||
"github.com/emirpasic/gods/lists/arraylist" | ||
"github.com/emirpasic/gods/maps/hashmap" | ||
) | ||
|
||
// ListSerializationExample demonstrates how to serialize and deserialize lists to and from JSON | ||
func ListSerializationExample() { | ||
list := arraylist.New() | ||
list.Add("a", "b", "c") | ||
|
||
// Serialization (marshalling) | ||
json, err := list.ToJSON() | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
fmt.Println(string(json)) // ["a","b","c"] | ||
|
||
// Deserialization (unmarshalling) | ||
json = []byte(`["a","b"]`) | ||
err = list.FromJSON(json) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
fmt.Println(list) // ArrayList ["a","b"] | ||
} | ||
|
||
// MapSerializationExample demonstrates how to serialize and deserialize maps to and from JSON | ||
func MapSerializationExample() { | ||
m := hashmap.New() | ||
m.Put("a", "1") | ||
m.Put("b", "2") | ||
m.Put("c", "3") | ||
|
||
// Serialization (marshalling) | ||
json, err := m.ToJSON() | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
fmt.Println(string(json)) // {"a":"1","b":"2","c":"3"} | ||
|
||
// Deserialization (unmarshalling) | ||
json = []byte(`{"a":"1","b":"2"}`) | ||
err = m.FromJSON(json) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
fmt.Println(m) // HashMap {"a":"1","b":"2"} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) 2015, Emir Pasic. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package arraylist | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/emirpasic/gods/containers" | ||
) | ||
|
||
func assertSerializationImplementation() { | ||
var _ containers.JSONSerializer = (*List)(nil) | ||
var _ containers.JSONDeserializer = (*List)(nil) | ||
} | ||
|
||
// ToJSON outputs the JSON representation of list's elements. | ||
func (list *List) ToJSON() ([]byte, error) { | ||
return json.Marshal(list.elements[:list.size]) | ||
} | ||
|
||
// FromJSON populates list's elements from the input JSON representation. | ||
func (list *List) FromJSON(data []byte) error { | ||
err := json.Unmarshal(data, &list.elements) | ||
if err == nil { | ||
list.size = len(list.elements) | ||
} | ||
return err | ||
} |
Oops, something went wrong.