github.com/Ingenico-ePayments/connect-sdk-go@v0.0.0-20240318153750-1f8cd329b9c9/defaultimpl/DefaultMarshaller.go (about) 1 package defaultimpl 2 3 import ( 4 "encoding/json" 5 "io" 6 ) 7 8 // DefaultMarshaller represents the default implementation of the JSON Marshaller 9 type DefaultMarshaller struct { 10 } 11 12 // Marshal encodes the given value using json.Marshal 13 func (m *DefaultMarshaller) Marshal(v interface{}) (string, error) { 14 dataBytes, err := json.Marshal(v) 15 data := string(dataBytes) 16 17 return data, err 18 } 19 20 // Unmarshal decodes the given data into the given value using json.Unmarshal 21 func (m *DefaultMarshaller) Unmarshal(data string, v interface{}) error { 22 if len(data) < 1 { 23 return nil 24 } 25 26 return json.Unmarshal([]byte(data), v) 27 } 28 29 // UnmarshalFromReader decodes the data from the given reader into the given value using json.NewDecoder 30 func (m *DefaultMarshaller) UnmarshalFromReader(reader io.Reader, v interface{}) error { 31 decoder := json.NewDecoder(reader) 32 33 err := decoder.Decode(v) 34 if err != io.EOF { 35 return err 36 } 37 38 return nil 39 } 40 41 // NewDefaultMarshaller creates a DefaultMarshaller 42 func NewDefaultMarshaller() (*DefaultMarshaller, error) { 43 return &DefaultMarshaller{}, nil 44 }