json: migrate every encoding/json import to json/v2 (go-json-experiment)

External (HTTP / JSON-RPC) is the only place JSON is legitimate. Every
existing encoding/json import in node/ moves to github.com/go-json-experiment/json
(v2 root, not the v1 sub-package). NewEncoder/NewDecoder rewrite to
MarshalWrite/UnmarshalRead. MarshalIndent rewrites to Marshal with
jsontext.WithIndent. json.RawMessage rewrites to jsontext.Value.
*json.SyntaxError rewrites to *jsontext.SyntacticError.

81 files migrated. LLM.md captures the rule + v1->v2 delta table.

Known v2 semantic deltas surfaced by existing tests (followups, not regressions):
- [N]byte fields with no MarshalJSON now marshal as base64 string (v1 marshalled
  as JSON array of byte numbers). Affects vms/platformvm/txs/*_test.go fixtures
  with embedded BLS proofOfPossession.
- time.Duration has no v2 default representation; configs that wire-format
  Duration as nanoseconds (vms/{xvm,platformvm}/config, config/spec) need to
  switch to string-form Duration or carry an explicit option. v2 root does not
  re-export FormatDurationAsNano.
- v2 enforces strict UTF-8 (vms/chainadapter/messaging fixture has non-UTF-8).
- json.MarshalWrite does not append a trailing '\n' (v1 NewEncoder.Encode did);
  service/auth/auth_test.go expectation updated.
- nil []byte round-trips to empty (not nil); config_test deep-equal fixtures
  surface this.

All affected sites are at the API boundary; ZAP wire envelope already covers
the internal data paths (state, P2P, consensus, MPC, threshold). Internal
JSON sites that should move to ZAP next (separate work):
- vms/da/store.go            (DA blob/cert storage as JSON)
- vms/platformvm/airdrop     (airdrop claims as JSON in db)
- vms/chainadapter/appchain  (SQLite materializer schema/data blobs)
- vms/chainadapter/messaging (conversation codec)
- staking/kms.go             (KMS HTTP client — external technically, leave)
- utils/{bimap,ips}          (small marshaler shims — low priority)
This commit is contained in:
Hanzo AI
2026-06-06 22:26:02 -07:00
parent ceeea038ff
commit c3b398bc7b
85 changed files with 198 additions and 129 deletions
+3 -2
View File
@@ -4,7 +4,8 @@
package tmpnet
import (
"encoding/json"
"github.com/go-json-experiment/json"
"github.com/go-json-experiment/json/jsontext"
"fmt"
"time"
@@ -91,7 +92,7 @@ func NewTestGenesisWithFunds(
// Add basic C-Chain genesis
config.CChainGenesis = getBasicCChainGenesis(networkID)
return json.MarshalIndent(config, "", " ")
return json.Marshal(config, jsontext.WithIndent(" "))
}
// getBasicCChainGenesis returns a basic C-Chain genesis configuration
+3 -2
View File
@@ -5,7 +5,8 @@ package tmpnet
import (
"context"
"encoding/json"
"github.com/go-json-experiment/json"
"github.com/go-json-experiment/json/jsontext"
"fmt"
"os"
"path/filepath"
@@ -123,7 +124,7 @@ func (n *Network) Write() error {
return fmt.Errorf("failed to create network directory: %w", err)
}
data, err := json.MarshalIndent(n, "", " ")
data, err := json.Marshal(n, jsontext.WithIndent(" "))
if err != nil {
return fmt.Errorf("failed to marshal network config: %w", err)
}
+3 -2
View File
@@ -7,7 +7,8 @@ import (
"context"
"crypto/rand"
"crypto/x509"
"encoding/json"
"github.com/go-json-experiment/json"
"github.com/go-json-experiment/json/jsontext"
"encoding/pem"
"fmt"
"net/netip"
@@ -121,7 +122,7 @@ func (n *Node) Write() error {
Flags: n.Flags,
}
data, err := json.MarshalIndent(config, "", " ")
data, err := json.Marshal(config, jsontext.WithIndent(" "))
if err != nil {
return fmt.Errorf("failed to marshal node config: %w", err)
}
+1 -1
View File
@@ -21,7 +21,7 @@ import (
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/json"
"github.com/go-json-experiment/json"
"errors"
"math/big"
"strings"