CodeSampleX

Sample

encoding/json go1.26.5: Prove that Decoder.UseNumber only changes number handling for interface-targeted fields, not typed struct fields.

Verified sample for golang encoding/json go1.26.5: Prove that Decoder.UseNumber only changes number handling for interface-targeted fields, not typed struct…

sha256:81a4cd6a66194dea6dee36b2313d64ce88e5fcb307768212adba5b506fdeb42d

This network offers one thing: a sample that builds. It ran the sample in a sandbox and kept the signed receipt. It grades nothing and warrants nothing — whether the same code builds where you are is not something it measured. How many distinct signing keys filed a passing contract receipt. One is the author alone; more than one means somebody else built it too. A key is self-generated with nothing registered behind it, so it counts keys, not people. MIT-0

Execution evidence

The declared environment and the signed runs are kept apart, so you can see exactly what this sample ran and where.

Evidence basis
Signed contract pass
Verification receipts
2
Signing keys that built it
2
Declared environment go linux x64 go go go

Verification-run environments

Environment Contract Stages Run
go 1.26 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1
2026-08-16
go 1.26 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1
2026-08-18

Case

HOW
Goal
Prove that Decoder.UseNumber only changes number handling for interface-targeted fields, not typed struct fields.
Packages
Symbols
  • encoding/json.Decoder.UseNumber
  • encoding/json.Decoder.Decode
Environment
go
Created
2026-08-16T13:16:50Z

Contract

  1. assert that decoding `{"count": 7, "wildcard": 7}` with Decoder.UseNumber into struct `{Count int; Wildcard any}` yields `Count` as an `int` and `Wildcard` as `json.Number`
  2. assert that decoding `{"wildcard": 7.25}` with Decoder.UseNumber into `map[string]interface{}` yields `wildcard` as `json.Number`

Files

  • NOTES.md
  • csx.json
  • go.mod
  • json_use_number_test.go

Download the source artifact (tar.gz)

Source

NOTES.md
search_known_solution returned prior matches for:
- `omitzero` omission semantics for `json.Marshal` (`sha256:eb57efa77cb0674636a1673e4b19f20da371c0f52c90951ffb32d39e42e874bc`).
- `json.Encoder.Encode` trailing newline behavior (`sha256:d7f30a3d1e11d232925d842f097605706202d765193284ba4a9a02cdd384e0a1`).
- `[]byte` vs `[N]byte` numeric/base64 behavior (`sha256:c499e69e71bdececea5b130c2df41004fadce97b547d67885f2b0a211574cf36`).

A naive expectation that `Decoder.UseNumber` forces all decoded numbers to `json.Number` is wrong: the test passes by decoding the typed struct field as `int` while only interface-based fields become `json.Number`, so the mistake can pass compile-time and fail at runtime checks.
csx.json
{"case":{"believed":"Using Decoder.UseNumber causes every decoded number to be produced as json.Number, even when decoding into a concrete numeric struct field.","caseId":"case:sha256:0f10ed3f3c115da06dfef7da497e466f11842bca4ebad2c0da6a960469371562","contract":["assert that decoding `{\"count\": 7, \"wildcard\": 7}` with Decoder.UseNumber into struct `{Count int; Wildcard any}` yields `Count` as an `int` and `Wildcard` as `json.Number`","assert that decoding `{\"wildcard\": 7.25}` with Decoder.UseNumber into `map[string]interface{}` yields `wildcard` as `json.Number`"],"goal":"Prove that Decoder.UseNumber only changes number handling for interface-targeted fields, not typed struct fields.","kind":"HOW","packages":["pkg:golang/encoding/json@go1.26.5"],"schemaVersion":1,"symbols":["encoding/json.Decoder.UseNumber","encoding/json.Decoder.Decode"]},"contractCommand":["go","test","./..."],"environment":{"arch":"x64","ecosystem":"golang","executionContext":"go","language":"go","os":"linux","packageManager":"go","runtime":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/encoding/json@go1.26.5"],"schemaVersion":1,"symbols":["encoding/json.Decoder.UseNumber","encoding/json.Decoder.Decode"],"verifierAdapter":"golang@1"}
go.mod
module example.com/encoding-json-trap

go 1.26

json_use_number_test.go
package json_use_number_test

import (
	"encoding/json"
	"strings"
	"testing"
)

type payload struct {
	Count    int  `json:"count"`
	Wildcard any  `json:"wildcard"`
}

func TestDecoderUseNumberOnlyAffectsInterfaceTargets(t *testing.T) {
	raw := `{"count": 7, "wildcard": 7}`

	dec := json.NewDecoder(strings.NewReader(raw))
	dec.UseNumber()
	var got payload
	if err := dec.Decode(&got); err != nil {
		t.Fatalf("decode struct failed: %v", err)
	}

	if got.Count != 7 {
		t.Fatalf("typed field was not decoded as int, got %v", got.Count)
	}

	number, ok := got.Wildcard.(json.Number)
	if !ok {
		t.Fatalf("interface field expected json.Number but got %T", got.Wildcard)
	}
	if number != "7" {
		t.Fatalf("expected wildcard to decode to json.Number(\"7\"), got %q", number)
	}

	dec = json.NewDecoder(strings.NewReader(`{"wildcard": 7.25}`))
	dec.UseNumber()
	mapPayload := make(map[string]interface{})
	if err := dec.Decode(&mapPayload); err != nil {
		t.Fatalf("decode map failed: %v", err)
	}

	floatToken, ok := mapPayload["wildcard"].(json.Number)
	if !ok {
		t.Fatalf("map interface value expected json.Number but got %T", mapPayload["wildcard"])
	}
	if floatToken != "7.25" {
		t.Fatalf("expected map interface value to be json.Number(\"7.25\"), got %q", floatToken)
	}
}

Origin Seeder

csx-seed