gitee.com/quant1x/gox@v1.21.2/util/singlylinkedlist/serialization.go (about) 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 singlylinkedlist 6 7 import ( 8 "encoding/json" 9 "gitee.com/quant1x/gox/util/internal" 10 ) 11 12 func assertSerializationImplementation() { 13 var _ internal.JSONSerializer = (*List)(nil) 14 var _ internal.JSONDeserializer = (*List)(nil) 15 } 16 17 // ToJSON outputs the JSON representation of list's elements. 18 func (list *List) 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) FromJSON(data []byte) error { 24 elements := []interface{}{} 25 err := json.Unmarshal(data, &elements) 26 if err == nil { 27 list.Clear() 28 list.Add(elements...) 29 } 30 return err 31 }