CodeSampleX

Exemple

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

Échantillon vérifié pour golang github.com/json-iterator/go v1.1.11: jsoniter.Parse. Le contrat s'est exécuté sur go 1.26 · linux debian/x64 · docker et a…

sha256:ff0ad17faedcbe71e4326ba6fca26344ad4224ffa521ca81a104b17bad24c966

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
1
Clés de signature qui l’ont compilé
1
Environnement déclaré go 1.26 linux 24 · ubuntu · glibc 2.39 amd64 go 1.26 go 1.26 go 1

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
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

Cas

HOW
Objectif
verify jsoniter.Parse in pkg:golang/github.com/json-iterator/go@v1.1.11
Paquets
Symboles
  • jsoniter.Parse
Environnement
go 1.26.6
Créé
2026-09-05T01:40:05Z

Contrat

  1. jsoniter.Parse creates an Iterator from an io.Reader and decodes JSON into structured values
  2. jsoniter.Parse streams sequential tokens from an io.Reader using callback-based iteration
  3. jsoniter.Parse handles nested JSON objects via ReadObjectCB
  4. jsoniter.Parse sets a non-nil Error on malformed JSON streams
  5. jsoniter.Parse allows reusing the iterator across different readers via Reset

Fichiers

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

Télécharger l’artefact source (tar.gz)

Code source

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.Parse 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.Parse
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

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:09c0230a935984fefdbb8503433786e7eb30e4a3d83482470feab49d4910a5b1","contract":["jsoniter.Parse creates an Iterator from an io.Reader and decodes JSON into structured values","jsoniter.Parse streams sequential tokens from an io.Reader using callback-based iteration","jsoniter.Parse handles nested JSON objects via ReadObjectCB","jsoniter.Parse sets a non-nil Error on malformed JSON streams","jsoniter.Parse allows reusing the iterator across different readers via Reset"],"goal":"verify jsoniter.Parse 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.Parse"]},"contractCommand":["go","run","./test"],"environment":{"arch":"amd64","compiler":"go","compilerVersion":"1.26.6","distro":"ubuntu","ecosystem":"golang","language":"go","languageVersion":"1.26.6","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","packageManagerVersion":"1.26.6","runtime":"go","runtimeVersion":"1.26.6","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.Parse"],"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"
	"strings"

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

// UserProfile represents sample structured user data.
type UserProfile struct {
	ID     int      `json:"id"`
	Name   string   `json:"name"`
	Skills []string `json:"skills"`
}

func main() {
	jsonData := `{"id": 101, "name": "Alice", "skills": ["go", "distributed-systems"]}`

	// Create an Iterator instance from an io.Reader with a 512-byte buffer
	reader := strings.NewReader(jsonData)
	iter := jsoniter.Parse(jsoniter.ConfigDefault, reader, 512)

	var profile UserProfile
	iter.ReadVal(&profile)

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

	fmt.Printf("Parsed profile: ID=%d, Name=%s, Skills=%v\n", profile.ID, profile.Name, profile.Skills)
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify jsoniter.Parse 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.Parse"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
test/contract.go
package main

import (
	"fmt"
	"os"
	"reflect"
	"strings"

	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.Parse creates an Iterator from an io.Reader and decodes JSON into structured values
	{
		raw := `{"title":"alpha","score":95,"tags":["fast","compact"]}`
		reader := strings.NewReader(raw)
		iter := jsoniter.Parse(jsoniter.ConfigDefault, reader, 256)

		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.Parse streams sequential tokens from an io.Reader using callback-based iteration
	{
		raw := `[10, 20, 30, 40]`
		reader := strings.NewReader(raw)
		iter := jsoniter.Parse(jsoniter.ConfigDefault, reader, 4)

		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.Parse handles nested JSON objects via ReadObjectCB
	{
		raw := `{"status":"ok","code":200}`
		reader := strings.NewReader(raw)
		iter := jsoniter.Parse(jsoniter.ConfigDefault, reader, 64)

		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.Parse sets a non-nil Error on malformed JSON streams
	{
		raw := `{"unclosed": "string`
		reader := strings.NewReader(raw)
		iter := jsoniter.Parse(jsoniter.ConfigDefault, reader, 64)

		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.Parse allows reusing the iterator across different readers via Reset
	{
		firstReader := strings.NewReader(`"hello"`)
		iter := jsoniter.Parse(jsoniter.ConfigDefault, firstReader, 64)

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

		secondReader := strings.NewReader(`"world"`)
		iter.Reset(secondReader)

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

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

Seeder d'origine

anonyme