mirror of
https://github.com/emirpasic/gods.git
synced 2026-06-24 17:44:56 +03:00
14f714261f
* Generics migration This attempts to migrate this library in the least invasive way by preserving as much of the original API as possible. It does not change the tests in a meaningful way nor does it attempt to upgrade any logic that can be simplified or improved with generics. This is purely an API migration, and still requires a lot of additional work to be fully ready. * Fix a few broken tests around serialization * Add v2 suffix * Temporarily change mod name for testing * Rename module to /v2
42 lines
1.0 KiB
Go
42 lines
1.0 KiB
Go
// 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 linkedhashset
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"github.com/emirpasic/gods/v2/containers"
|
|
)
|
|
|
|
// Assert Serialization implementation
|
|
var _ containers.JSONSerializer = (*Set[int])(nil)
|
|
var _ containers.JSONDeserializer = (*Set[int])(nil)
|
|
|
|
// ToJSON outputs the JSON representation of the set.
|
|
func (set *Set[T]) ToJSON() ([]byte, error) {
|
|
return json.Marshal(set.Values())
|
|
}
|
|
|
|
// FromJSON populates the set from the input JSON representation.
|
|
func (set *Set[T]) FromJSON(data []byte) error {
|
|
var elements []T
|
|
err := json.Unmarshal(data, &elements)
|
|
if err == nil {
|
|
set.Clear()
|
|
set.Add(elements...)
|
|
}
|
|
return err
|
|
}
|
|
|
|
// UnmarshalJSON @implements json.Unmarshaler
|
|
func (set *Set[T]) UnmarshalJSON(bytes []byte) error {
|
|
return set.FromJSON(bytes)
|
|
}
|
|
|
|
// MarshalJSON @implements json.Marshaler
|
|
func (set *Set[T]) MarshalJSON() ([]byte, error) {
|
|
return set.ToJSON()
|
|
}
|