CodeSampleX

示例

github.com/json-iterator/go v1.1.11: jsoniter.ParseBytes

已验证示例 — golang github.com/json-iterator/go v1.1.11: jsoniter.ParseBytes. contract 在 go 1.26 · linux debian/x64 · docker 上运行并通过.

sha256:01ce8b117c1a390b46ac16ef2eee971f4aa3c7cd4abde248316ad58d744ac21d

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

执行证据

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

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 go

验证运行环境

环境 契约 阶段 运行日期
go 1.26 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a…
2026-09-05

案例

HOW
目标
verify jsoniter.ParseBytes in pkg:golang/github.com/json-iterator/go@v1.1.11
包
符号
  • jsoniter.ParseBytes
创建时间
2026-09-05T01:47:46Z

契约

  1. jsoniter.ParseBytes creates an Iterator from a byte slice and unmarshals into a struct
  2. jsoniter.ParseBytes streams sequential tokens from a byte slice using ReadArrayCB
  3. jsoniter.ParseBytes handles nested JSON objects via ReadObjectCB
  4. jsoniter.ParseBytes sets a non-nil Error on malformed JSON input
  5. jsoniter.ParseBytes allows reusing the iterator across different byte slices via ResetBytes

文件

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.go
  • spec.json
  • test/contract.go

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

源代码

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify jsoniter.ParseBytes in pkg:golang/github.com/json-iterator/go@v1.1.11
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/json-iterator/go@v1.1.11
Demonstrate these symbols/APIs:
  - jsoniter.ParseBytes

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:f33d918be95d37c98f982f4c1c5eafdb07f81be6cf744b5d4d6fa07c3c182631","contract":["jsoniter.ParseBytes creates an Iterator from a byte slice and unmarshals into a struct","jsoniter.ParseBytes streams sequential tokens from a byte slice using ReadArrayCB","jsoniter.ParseBytes handles nested JSON objects via ReadObjectCB","jsoniter.ParseBytes sets a non-nil Error on malformed JSON input","jsoniter.ParseBytes allows reusing the iterator across different byte slices via ResetBytes"],"goal":"verify jsoniter.ParseBytes in pkg:golang/github.com/json-iterator/go@v1.1.11","kind":"HOW","packages":["pkg:golang/github.com/json-iterator/go@v1.1.11"],"schemaVersion":1,"symbols":["jsoniter.ParseBytes"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/json-iterator/go@v1.1.11"],"schemaVersion":1,"subject":"pkg:golang/github.com/json-iterator/go@v1.1.11","symbols":["jsoniter.ParseBytes"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.26.6

require github.com/json-iterator/go v1.1.11

require (
	github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
	github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
)
go.sum
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.11 h1:uVUAXhF2To8cbw/3xN3pxj6kk7TYKs98NIrTqPlMWAQ=
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
main.go
package main

import (
	"fmt"

	jsoniter "github.com/json-iterator/go"
)

type ConfigRecord struct {
	ServiceName string   `json:"service_name"`
	Port        int      `json:"port"`
	Features    []string `json:"features"`
}

func main() {
	raw := []byte(`{"service_name":"auth-svc","port":8080,"features":["tls","metrics"]}`)

	iter := jsoniter.ParseBytes(jsoniter.ConfigDefault, raw)

	var record ConfigRecord
	iter.ReadVal(&record)

	if iter.Error != nil {
		fmt.Printf("parse error: %v\n", iter.Error)
		return
	}

	fmt.Printf("Parsed: service=%s port=%d features=%v\n", record.ServiceName, record.Port, record.Features)
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify jsoniter.ParseBytes in pkg:golang/github.com/json-iterator/go@v1.1.11",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/json-iterator/go@v1.1.11"
  ],
  "symbols": [
    "jsoniter.ParseBytes"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"
	"reflect"

	jsoniter "github.com/json-iterator/go"
)

type testPayload struct {
	Title string   `json:"title"`
	Score int      `json:"score"`
	Tags  []string `json:"tags"`
}

func main() {
	// Assertion 1: jsoniter.ParseBytes creates an Iterator from a byte slice and unmarshals into a struct
	{
		raw := []byte(`{"title":"alpha","score":95,"tags":["fast","compact"]}`)
		iter := jsoniter.ParseBytes(jsoniter.ConfigDefault, raw)

		var val testPayload
		iter.ReadVal(&val)

		if iter.Error != nil {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: unexpected error: %v\n", iter.Error)
			os.Exit(1)
		}
		if val.Title != "alpha" || val.Score != 95 || !reflect.DeepEqual(val.Tags, []string{"fast", "compact"}) {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: parsed value mismatch: %+v\n", val)
			os.Exit(1)
		}
	}

	// Assertion 2: jsoniter.ParseBytes streams sequential tokens from a byte slice using ReadArrayCB
	{
		raw := []byte(`[10, 20, 30, 40]`)
		iter := jsoniter.ParseBytes(jsoniter.ConfigDefault, raw)

		var numbers []int
		iter.ReadArrayCB(func(it *jsoniter.Iterator) bool {
			numbers = append(numbers, it.ReadInt())
			return true
		})

		if iter.Error != nil {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: array iteration error: %v\n", iter.Error)
			os.Exit(1)
		}
		expected := []int{10, 20, 30, 40}
		if !reflect.DeepEqual(numbers, expected) {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: expected %v, got %v\n", expected, numbers)
			os.Exit(1)
		}
	}

	// Assertion 3: jsoniter.ParseBytes handles nested JSON objects via ReadObjectCB
	{
		raw := []byte(`{"status":"ok","code":200}`)
		iter := jsoniter.ParseBytes(jsoniter.ConfigDefault, raw)

		fields := make(map[string]interface{})
		iter.ReadObjectCB(func(it *jsoniter.Iterator, field string) bool {
			switch field {
			case "status":
				fields["status"] = it.ReadString()
			case "code":
				fields["code"] = it.ReadInt()
			default:
				it.Skip()
			}
			return true
		})

		if iter.Error != nil {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: object callback error: %v\n", iter.Error)
			os.Exit(1)
		}
		if fields["status"] != "ok" || fields["code"] != 200 {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: expected status 'ok' and code 200, got %+v\n", fields)
			os.Exit(1)
		}
	}

	// Assertion 4: jsoniter.ParseBytes sets a non-nil Error on malformed JSON input
	{
		raw := []byte(`{"unclosed": "string`)
		iter := jsoniter.ParseBytes(jsoniter.ConfigDefault, raw)

		var val map[string]interface{}
		iter.ReadVal(&val)

		if iter.Error == nil {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: expected error on malformed JSON, got nil\n")
			os.Exit(1)
		}
	}

	// Assertion 5: jsoniter.ParseBytes allows reusing the iterator across different byte slices via ResetBytes
	{
		firstBytes := []byte(`"hello"`)
		iter := jsoniter.ParseBytes(jsoniter.ConfigDefault, firstBytes)

		s1 := iter.ReadString()
		if iter.Error != nil || s1 != "hello" {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: first read failed: %q, err: %v\n", s1, iter.Error)
			os.Exit(1)
		}

		secondBytes := []byte(`"world"`)
		iter.ResetBytes(secondBytes)

		s2 := iter.ReadString()
		if iter.Error != nil || s2 != "world" {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: second read failed after ResetBytes: %q, err: %v\n", s2, iter.Error)
			os.Exit(1)
		}
	}

	fmt.Println("All contract assertions passed successfully.")
}

原始种子者

匿名