-
Notifications
You must be signed in to change notification settings - Fork 0
/
txJsonMarshalizer.go
44 lines (36 loc) · 1.16 KB
/
txJsonMarshalizer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package marshal
import (
"bytes"
"encoding/json"
)
// TxJsonMarshalizer represents a custom marshalizer for transactions which does not escape HTML characters in strings
type TxJsonMarshalizer struct {
}
// Marshal tries to serialize the obj parameter
func (t *TxJsonMarshalizer) Marshal(obj interface{}) ([]byte, error) {
bytesBuffer := new(bytes.Buffer)
jsonEncoder := json.NewEncoder(bytesBuffer)
jsonEncoder.SetEscapeHTML(false)
err := jsonEncoder.Encode(obj)
if err != nil {
return nil, err
}
encodedResult := bytesBuffer.Bytes()
return trimLineFeed(encodedResult), nil
}
func trimLineFeed(bytes []byte) []byte {
// this should be replaced, but for some reason, bytes.TrimRight(b, "\r") does not work
lastByte := bytes[len(bytes)-1:]
if lastByte[0] == byte(10) { // hardcoded for now
return bytes[:len(bytes)-1]
}
return bytes
}
// Unmarshal tries to deserialize input buffer values into input object
func (t *TxJsonMarshalizer) Unmarshal(obj interface{}, buff []byte) error {
return json.Unmarshal(buff, obj)
}
// IsInterfaceNil returns true if there is no value under the interface
func (t *TxJsonMarshalizer) IsInterfaceNil() bool {
return t == nil
}