Debian Codegraph @

lists/doublylinkedlist/serialization.go

raw symbols SCIP
1 // Copyright (c) 2015, Emir Pasic. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package doublylinkedlist
6
7 import (
8 "encoding/json"
9
10 "github.com/emirpasic/gods/v2/containers"
11 )
12
13 // Assert Serialization implementation
14 var _ containers.JSONSerializer = (*List[int])(nil)
15 var _ containers.JSONDeserializer = (*List[int])(nil)
16
17 // ToJSON outputs the JSON representation of list's elements.
18 func (list *List[T]) ToJSON() ([]byte, error) {
19 return json.Marshal(list.Values())
20 }
21
22 // FromJSON populates list's elements from the input JSON representation.
23 func (list *List[T]) FromJSON(data []byte) error {
24 var elements []T
25 err := json.Unmarshal(data, &elements)
26 if err == nil {
27 list.Clear()
28 list.Add(elements...)
29 }
30 return err
31 }
32
33 // UnmarshalJSON @implements json.Unmarshaler
34 func (list *List[T]) UnmarshalJSON(bytes []byte) error {
35 return list.FromJSON(bytes)
36 }
37
38 // MarshalJSON @implements json.Marshaler
39 func (list *List[T]) MarshalJSON() ([]byte, error) {
40 return list.ToJSON()
41 }