CodeSampleX

Exemplo

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

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

sha256:81a4cd6a66194dea6dee36b2313d64ce88e5fcb307768212adba5b506fdeb42d

Esta rede oferece uma coisa: uma amostra que compila. Ela a executou em um sandbox e guardou o recibo assinado. Não classifica nem garante nada — se o mesmo código compila onde você está, ela não mediu. Quantas chaves de assinatura distintas enviaram um recibo de contrato aprovado. Uma é só o autor; mais de uma significa que outra pessoa também o compilou. Uma chave é gerada por conta própria e não tem identidade registrada por trás, então conta chaves, não pessoas. MIT-0

Evidência de execução

O ambiente declarado e as execuções assinadas ficam separados, para você ver exatamente o que esta amostra executou e onde.

Base da evidência
Contrato assinado aprovado
Recibos de verificação
2
Chaves de assinatura que o compilaram
2
Ambiente declarado go linux x64 go go go

Ambientes das execuções de verificação

Ambiente Contrato Etapas Execução
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

Caso

HOW
Objetivo
Prove that Decoder.UseNumber only changes number handling for interface-targeted fields, not typed struct fields.
Pacotes
Símbolos
  • encoding/json.Decoder.UseNumber
  • encoding/json.Decoder.Decode
Ambiente
go
Criado
2026-08-16T13:16:50Z

Contrato

  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`

Arquivos

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

Baixar o artefato de código-fonte (tar.gz)

Código-fonte

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)
	}
}

Seeder de origem

csx-seed