lattice-substrate/json-canon

GitHub: lattice-substrate/json-canon

一个严格遵循 RFC 8785 的 Go 语言 JSON 规范化库,提供确定性字节级输出的解析器和序列化器。

Stars: 1 | Forks: 0

# json-canon [![Go 参考文档](https://pkg.go.dev/badge/github.com/lattice-substrate/json-canon.svg)](https://pkg.go.dev/github.com/lattice-substrate/json-canon) [![Go 报告卡](https://goreportcard.com/badge/github.com/lattice-substrate/json-canon)](https://goreportcard.com/report/github.com/lattice-substrate/json-canon) [![CI](https://static.pigsec.cn/wp-content/uploads/repos/2026/04/a70f7c10ac114759.svg)](https://github.com/lattice-substrate/json-canon/actions/workflows/ci.yml) [![覆盖率](https://static.pigsec.cn/wp-content/uploads/repos/2026/04/6ecbe56008114801.svg)](https://github.com/lattice-substrate/json-canon/actions/workflows/coverage.yml) [![DOI](https://zenodo.org/badge/doi/10.5281/zenodo.18890836.svg)](https://doi.org/10.5281/zenodo.18890836) json-canon 生成字节级确定性 JSON。确定性声明的范围限定于精确的源代码提交、工具链、矩阵和为发布记录的证据工件;它们不被声明为无限制的平台保证。它实现了 [RFC 8785](https://www.rfc-editor.org/rfc/rfc8785)(JSON 规范化方案),包含一个拒绝歧义输入的严格解析器、一个经过 286,362 个 oracle 测试向量验证的手写 Burger-Dybvig 数字格式化器,以及一个遵循 SemVer 的稳定 CLI 约定。规范化管道避免了使用 `encoding/json` 和 `strconv.FormatFloat`;发布证据自动化添加了固定版本工具链和 AWS SDK 依赖。仅支持 Linux。 ## Go API ``` import ( "errors" "fmt" "log" "github.com/lattice-substrate/json-canon/jcs" "github.com/lattice-substrate/json-canon/jcserr" "github.com/lattice-substrate/json-canon/jcstoken" ) func main() { input := []byte(`{"b": 2, "a": 1, "c": 3.0}`) v, err := jcstoken.Parse(input) if err != nil { var je *jcserr.Error if errors.As(err, &je) { fmt.Printf("failure class: %s\n", je.Class) } log.Fatal(err) } canonical, err := jcs.Serialize(v) if err != nil { log.Fatal(err) } fmt.Println(string(canonical)) // Output: {"a":1,"b":2,"c":3} } ``` 对于单次调用完成解析和序列化的场景: ``` canonical, err := jcs.Canonicalize(input) ``` ## CLI ``` # Canonicalize echo '{"b":2,"a":1}' | jcs-canon canonicalize - # Output: {"a":1,"b":2} # 验证 canonical form jcs-canon verify document.json ``` ## 安装 **库:** ``` go get github.com/lattice-substrate/json-canon ``` **CLI(从源码构建):** ``` CGO_ENABLED=0 go build -trimpath -buildvcs=false \ -ldflags="-s -w -buildid= -X main.version=v0.0.0-dev" \ -o jcs-canon ./cmd/jcs-canon ``` 发布二进制文件可在 [发布页面](https://github.com/lattice-substrate/json-canon/releases) 获取。 验证说明:[`CONTRIBUTING.md`](
标签:Burger-Dybvig, Canonicalization, CLI, Deterministic, EVTX分析, Go, JSON, RFC 8785, Ruby工具, SemVer, WiFi技术, 严格解析器, 字节确定性, 工具, 序列化, 开源库, 搜索引擎爬虫, 数字格式化, 文档结构分析, 日志审计, 测试向量, 确定性, 规范化