github.com/dfklegend/cell2/utils@v0.0.0-20240402033734-a0a9f3d9335d/serialize/json/json.go (about)

     1  package json
     2  
     3  import (
     4  	"encoding/json"
     5  )
     6  
     7  // Serializer implements the serialize.Serializer interface
     8  type Serializer struct{}
     9  
    10  var (
    11  	jsonSerializer = NewSerializer()
    12  )
    13  
    14  // NewSerializer returns a new Serializer.
    15  func NewSerializer() *Serializer {
    16  	return &Serializer{}
    17  }
    18  
    19  func GetDefaultSerializer() *Serializer {
    20  	return jsonSerializer
    21  }
    22  
    23  // Marshal returns the JSON encoding of v.
    24  func (s *Serializer) Marshal(v interface{}) ([]byte, error) {
    25  	return json.Marshal(v)
    26  }
    27  
    28  // Unmarshal parses the JSON-encoded data and stores the result
    29  // in the value pointed to by v.
    30  func (s *Serializer) Unmarshal(data []byte, v interface{}) error {
    31  	return json.Unmarshal(data, v)
    32  }
    33  
    34  // GetName returns the name of the serializer.
    35  func (s *Serializer) GetName() string {
    36  	return "json"
    37  }