Files
node/utils/formatting/encoding_test.go
T
Hanzo AI c3b398bc7b 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)
2026-06-06 22:26:02 -07:00

155 lines
3.2 KiB
Go

// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package formatting
import (
"encoding/hex"
"testing"
"github.com/go-json-experiment/json"
"github.com/go-json-experiment/json/jsontext"
"github.com/stretchr/testify/require"
)
func TestEncodingMarshalJSON(t *testing.T) {
require := require.New(t)
enc := Hex
jsonBytes, err := enc.MarshalJSON()
require.NoError(err)
require.JSONEq(`"hex"`, string(jsonBytes))
}
func TestEncodingUnmarshalJSON(t *testing.T) {
require := require.New(t)
jsonBytes := []byte(`"hex"`)
var enc Encoding
require.NoError(json.Unmarshal(jsonBytes, &enc))
require.Equal(Hex, enc)
var serr *jsontext.SyntacticError
jsonBytes = []byte("")
require.ErrorAs(json.Unmarshal(jsonBytes, &enc), &serr)
jsonBytes = []byte(`""`)
err := json.Unmarshal(jsonBytes, &enc)
require.ErrorIs(err, errInvalidEncoding)
}
func TestEncodingString(t *testing.T) {
enc := Hex
require.Equal(t, "hex", enc.String())
}
// Test encoding bytes to a string and decoding back to bytes
func TestEncodeDecode(t *testing.T) {
require := require.New(t)
type test struct {
encoding Encoding
bytes []byte
str string
}
id := [32]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32}
tests := []test{
{
Hex,
[]byte{},
"0x7852b855",
},
{
Hex,
[]byte{0},
"0x0017afa01d",
},
{
Hex,
[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255},
"0x00010203040506070809ff4482539c",
},
{
Hex,
id[:],
"0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20b7a612c9",
},
}
for _, test := range tests {
// Encode the bytes
strResult, err := Encode(test.encoding, test.bytes)
require.NoError(err)
// Make sure the string repr. is what we expected
require.Equal(test.str, strResult)
// Decode the string
bytesResult, err := Decode(test.encoding, strResult)
require.NoError(err)
// Make sure we got the same bytes back
require.Equal(test.bytes, bytesResult)
}
}
// Test that encoding nil bytes works
func TestEncodeNil(t *testing.T) {
require := require.New(t)
str, err := Encode(Hex, nil)
require.NoError(err)
require.Equal("0x7852b855", str)
}
func TestDecodeHexInvalid(t *testing.T) {
tests := []struct {
inputStr string
expectedErr error
}{
{
inputStr: "0",
expectedErr: errMissingHexPrefix,
},
{
inputStr: "x",
expectedErr: errMissingHexPrefix,
},
{
inputStr: "0xg",
expectedErr: hex.InvalidByteError('g'),
},
{
inputStr: "0x0017afa0Zd",
expectedErr: hex.InvalidByteError('Z'),
},
{
inputStr: "0xafafafafaf",
expectedErr: errBadChecksum,
},
}
for _, test := range tests {
_, err := Decode(Hex, test.inputStr)
require.ErrorIs(t, err, test.expectedErr)
}
}
func TestDecodeNil(t *testing.T) {
require := require.New(t)
result, err := Decode(Hex, "")
require.NoError(err)
require.Empty(result)
}
func FuzzEncodeDecode(f *testing.F) {
f.Fuzz(func(t *testing.T, bytes []byte) {
require := require.New(t)
str, err := Encode(Hex, bytes)
require.NoError(err)
decoded, err := Decode(Hex, str)
require.NoError(err)
require.Equal(bytes, decoded)
})
}