Files
zeekayandGitHub 9a5a43e8a0 feat: route all JSON through encoding/json/v2 at the edge (HIP-0106) (#3)
Introduce internal/jsonenc package as the single JSON entry point for
every code path in zip: c.JSON, c.Bind().Body (via Fiber's pluggable
JSONEncoder/JSONDecoder), the typed zip.Get/Post[In,Out] roundtrip,
the HIP-0105 module envelope, and the auto-generated OpenAPI spec.

Two build-tag-gated files swap the implementation:
- v2.go (//go:build goexperiment.jsonv2) uses encoding/json/v2
- v1.go (//go:build !goexperiment.jsonv2) falls back to encoding/json

zip.JSONVariant is a compile-time constant naming the active impl.
zip.New logs it once at startup so operators can confirm v2 in prod
CI/k8s logs (json_variant=encoding/json/v2).

Bench numbers (Apple M1 Max, Go 1.26, fiber/v3 v3.2.0):

| Bench | json v1 ns/op | v2 ns/op | v1 allocs | v2 allocs | Δ |
|---|---|---|---|---|---|
| Edge POST roundtrip | 14972 | 13631 | 73 | 56 | -9% time, -23% allocs |
| Marshal-only | 10798 | 7924 | 34 | 34 | -27% time |
| Unmarshal-only | 13803 | 12729 | 67 | 50 | -8% time, -25% allocs |

Brand policy enforced: no third-party JSON library (no goccy/go-json,
no sonic, no jsoniter). Stdlib only per HIP-0106 canonical Hanzo Go
stack. RawMessage stays in encoding/json (v1) since the type only
lives there; both v1 and v2 honor its MarshalJSON/UnmarshalJSON so
the moduleEnvelope serializes identically under either build.
2026-05-19 11:14:06 -07:00

23 lines
1.1 KiB
Go

// Package jsonenc is zip's single JSON entry point.
//
// All JSON marshalling in zip — c.JSON(), c.Bind().Body(), typed.go's
// generic In/Out handlers, module.go's extension envelope — goes
// through Marshal / Unmarshal here. There is no other JSON path.
//
// Implementation: two build-tag-gated files swap the body. When the
// binary is compiled with GOEXPERIMENT=jsonv2, the import resolver
// can reach the stdlib's encoding/json/v2 package and the v2.go file
// is selected; otherwise v1.go is selected and we fall back to
// stdlib encoding/json. Either way callers see Marshal / Unmarshal —
// no v1/v2 branching in zip's own code.
//
// Per HIP-0106 "Wire protocol stack": JSON is the boundary format
// (ingress → gateway → subsystem handler). Inter-subsystem calls use
// ZAP-typed Go values. JSON marshalling happens at most once per
// request, at the subsystem handler boundary, through THIS package.
//
// Brand policy: no third-party JSON library is allowed. Stdlib only.
// goccy/go-json, sonic, jsoniter, and friends are NOT permitted in
// the Hanzo Go stack — see HIP-0106 canonical Hanzo Go stack.
package jsonenc