CodeSampleX

示例

encoding/json go1.26.5: Show that `omitzero` now changes omission semantics from zero-value checks to `IsZero()`-based suppression.

已验证示例 — golang encoding/json go1.26.5: Show that `omitzero` now changes omission semantics from zero-value checks to `IsZero()`-based suppression. contract 在…

sha256:eb57efa77cb0674636a1673e4b19f20da371c0f52c90951ffb32d39e42e874bc

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
2
构建过它的签名密钥
2
声明的环境 go linux x64 go go go

验证运行环境

环境 契约 阶段 运行日期
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

案例

HOW
目标
Show that `omitzero` now changes omission semantics from zero-value checks to `IsZero()`-based suppression.
包
符号
  • encoding/json.Marshal
  • encoding/json
环境
go
创建时间
2026-08-16T13:04:28Z

契约

  1. assert json.Marshal with tag `field,omitzero` omits a non-empty struct when its IsZero() method returns true
  2. assert json.Marshal with tag `field,omitempty` keeps that same non-empty struct value

文件

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

下载源代码构件 (tar.gz)

源代码

NOTES.md
# notes
search_known_solution returned no direct encoding/json hit for this specific `omitzero` boundary, so no existing sample was reused for this case.
Wrong legacy behavior fails silently with a green build: in code using only `omitempty`, a field whose `IsZero()` returns true but is not reflect-empty still serializes, while `encoding/json` with `,omitzero` now omits it.
csx.json
{"case":{"believed":"A field is omitted only when its value is reflect-empty, so a non-empty struct value must always be serialized unless `omitempty` is false.","caseId":"case:sha256:6dcd24cf18d3e94024d209c7cfee593f71c25faeb9b7fba9d23f471db3adc444","contract":["assert json.Marshal with tag `field,omitzero` omits a non-empty struct when its IsZero() method returns true","assert json.Marshal with tag `field,omitempty` keeps that same non-empty struct value"],"goal":"Show that `omitzero` now changes omission semantics from zero-value checks to `IsZero()`-based suppression.","kind":"HOW","packages":["pkg:golang/encoding/json@go1.26.5"],"schemaVersion":1,"symbols":["encoding/json.Marshal","encoding/json"]},"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.Marshal","encoding/json"],"verifierAdapter":"golang@1"}
go.mod
module example.com/json-jsonv2-contract

go 1.26
json_v2_strict_test.go
package jsoncompat

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

type isZeroByRule struct {
    N int
}

func (v isZeroByRule) IsZero() bool {
    return v.N == 42
}

type withOmitZero struct {
    Field isZeroByRule `json:"field,omitzero"`
}

type withOmitEmpty struct {
    Field isZeroByRule `json:"field,omitempty"`
}

func TestOmitZeroHonorsTypeIsZero(t *testing.T) {
    payload, err := json.Marshal(withOmitZero{Field: isZeroByRule{N: 42}})
    if err != nil {
        t.Fatalf("Marshal error: %v", err)
    }
    if got, want := strings.TrimSpace(string(payload)), "{}"; got != want {
        t.Fatalf("got %s, want %s", got, want)
    }
}

func TestOmitEmptyStillIncludesNonEmptyStruct(t *testing.T) {
    payload, err := json.Marshal(withOmitEmpty{Field: isZeroByRule{N: 42}})
    if err != nil {
        t.Fatalf("Marshal error: %v", err)
    }
    want := `{"field":{"N":42}}`
    if got := strings.TrimSpace(string(payload)); got != want {
        t.Fatalf("got %s, want %s", got, want)
    }
}

原始种子者

csx-seed