main
Raw Download raw file
 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
 5package arraylist
 6
 7import (
 8	"encoding/json"
 9	"github.com/emirpasic/gods/containers"
10)
11
12// Assert Serialization implementation
13var _ containers.JSONSerializer = (*List)(nil)
14var _ containers.JSONDeserializer = (*List)(nil)
15
16// ToJSON outputs the JSON representation of list's elements.
17func (list *List) ToJSON() ([]byte, error) {
18	return json.Marshal(list.elements[:list.size])
19}
20
21// FromJSON populates list's elements from the input JSON representation.
22func (list *List) FromJSON(data []byte) error {
23	err := json.Unmarshal(data, &list.elements)
24	if err == nil {
25		list.size = len(list.elements)
26	}
27	return err
28}
29
30// UnmarshalJSON @implements json.Unmarshaler
31func (list *List) UnmarshalJSON(bytes []byte) error {
32	return list.FromJSON(bytes)
33}
34
35// MarshalJSON @implements json.Marshaler
36func (list *List) MarshalJSON() ([]byte, error) {
37	return list.ToJSON()
38}