sdk_LinkedMall-20260828161641/crypto.go

39 lines
1.4 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package sdk_LinkedMall
import (
"bytes"
"encoding/json"
)
// 关于加解密与签名:
//
// 本 SDK 基于阿里云通用 OpenAPI 客户端darabonba-openapi/v2发起 ROA 调用,
// ROA 签名AccessKeyId / AccessKeySecret 签名)由客户端内部自动完成,
// 业务侧无需自行实现签名算法,因此本文件不包含额外的签名/加密实现。
//
// 本文件保留与数据安全相关的辅助函数,供后续扩展加解密、签名能力时使用:
// - MarshalJSON / UnmarshalJSON确保金额等 int64 字段在 JSON 序列化/反序列化过程中
// 不会丢失精度(接口金额单位统一为分,使用 int64禁止使用 float64
// MarshalJSON 将对象安全序列化为 JSON 字节。
// 与标准库 encoding/json.Marshal 的区别:关闭了 HTML 转义,保证输出紧凑且不转义特殊字符。
func MarshalJSON(v interface{}) ([]byte, error) {
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(false)
if err := enc.Encode(v); err != nil {
return nil, err
}
// json.Encoder.Encode 会在末尾追加一个换行符,这里去掉。
return bytes.TrimRight(buf.Bytes(), "\n"), nil
}
// UnmarshalJSON 使用 UseNumber 解析 JSON避免 int64 金额被转换为 float64 丢失精度。
func UnmarshalJSON(data []byte, v interface{}) error {
dec := json.NewDecoder(bytes.NewReader(data))
dec.UseNumber()
return dec.Decode(v)
}
```
```go