jsonpack/jsonpack.go

44 lines
644 B
Go
Raw Permalink Normal View History

2023-04-15 00:06:52 +08:00
package jsonpack
import (
"errors"
"unicode/utf8"
)
const (
TOKEN_TRUE = -(iota + 1)
TOKEN_FALSE
TOKEN_NULL
TOKEN_EMPTY_STRING
)
// 将对象加密为字符串
func Pack(json any) (string, error) {
pack := &JsonEncode{}
ast, err := pack.astBuilder(json)
if err != nil {
return "", err
}
return pack.toString(ast)
}
// 解密数据到JSON字符串
func Unpack(json string) (string, error) {
if !utf8.ValidString(json) {
return "", errors.New("jsonpack 无效的字符串格式, UTF8")
}
unpack := &JsonDecode{}
ast, err := unpack.astBuilder(json)
if err != nil {
return "", err
}
return unpack.toString(ast)
}