Debian Codegraph @

lists/arraylist/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 arraylist
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.elements[:list.size])
20 }
21
22 // FromJSON populates list's elements from the input JSON representation.
23 func (list *List[T]) FromJSON(data []byte) error {
24 err := json.Unmarshal(data, &list.elements)
25 if err == nil {
26 list.size = len(list.elements)
27 }
28 return err
29 }
30
31 // UnmarshalJSON @implements json.Unmarshaler
32 func (list *List[T]) UnmarshalJSON(bytes []byte) error {
33 return list.FromJSON(bytes)
34 }
35
36 // MarshalJSON @implements json.Marshaler
37 func (list *List[T]) MarshalJSON() ([]byte, error) {
38 return list.ToJSON()
39 }