2023-07-17 16:43:17 +00:00
|
|
|
//go:build !binary_log
|
2018-03-28 18:49:41 +00:00
|
|
|
// +build !binary_log
|
|
|
|
|
2022-03-20 19:19:42 +00:00
|
|
|
package zlog
|
2018-03-28 18:49:41 +00:00
|
|
|
|
|
|
|
// encoder_json.go file contains bindings to generate
|
|
|
|
// JSON encoded byte stream.
|
|
|
|
|
|
|
|
import (
|
2023-06-18 23:30:44 +00:00
|
|
|
"encoding/base64"
|
2022-11-03 15:18:09 +00:00
|
|
|
"tuxpa.in/a/zlog/internal/json"
|
2018-03-28 18:49:41 +00:00
|
|
|
)
|
|
|
|
|
2018-05-10 22:01:41 +00:00
|
|
|
var (
|
|
|
|
_ encoder = (*json.Encoder)(nil)
|
2018-03-28 18:49:41 +00:00
|
|
|
|
2018-05-10 22:01:41 +00:00
|
|
|
enc = json.Encoder{}
|
|
|
|
)
|
2018-03-28 18:49:41 +00:00
|
|
|
|
2021-05-20 12:46:36 +00:00
|
|
|
func init() {
|
|
|
|
// using closure to reflect the changes at runtime.
|
|
|
|
json.JSONMarshalFunc = func(v interface{}) ([]byte, error) {
|
|
|
|
return InterfaceMarshalFunc(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-28 18:49:41 +00:00
|
|
|
func appendJSON(dst []byte, j []byte) []byte {
|
|
|
|
return append(dst, j...)
|
|
|
|
}
|
2023-06-18 23:30:44 +00:00
|
|
|
func appendCBOR(dst []byte, cbor []byte) []byte {
|
|
|
|
dst = append(dst, []byte("\"data:application/cbor;base64,")...)
|
|
|
|
l := len(dst)
|
|
|
|
enc := base64.StdEncoding
|
|
|
|
n := enc.EncodedLen(len(cbor))
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
dst = append(dst, '.')
|
|
|
|
}
|
|
|
|
enc.Encode(dst[l:], cbor)
|
|
|
|
return append(dst, '"')
|
|
|
|
}
|
2018-03-28 18:49:41 +00:00
|
|
|
|
|
|
|
func decodeIfBinaryToString(in []byte) string {
|
|
|
|
return string(in)
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeObjectToStr(in []byte) string {
|
|
|
|
return string(in)
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeIfBinaryToBytes(in []byte) []byte {
|
|
|
|
return in
|
|
|
|
}
|