CodeSampleX

샘플

serde_json 1.0.151: Serialize and deserialize a struct as JSON with serde in Rust

검증된 샘플 — cargo serde_json 1.0.151: Serialize and deserialize a struct as JSON with serde in Rust. rust 1 · linux alpine/x64 · docker에서 contract를 실행해 통과했습니다.

sha256:84ec319af14787a23f24200632d3cff0015b1c04e3f4838979bf83d795318e50

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. MIT-0

실행 증거

선언된 환경과 서명된 실행을 분리해 두었습니다. 이 샘플이 무엇을 어디서 실행했는지 그대로 볼 수 있습니다.

증거 기준
서명된 컨트랙트 통과
검증 영수증
3
빌드한 서명 키
3
선언된 환경 rust 1 linux x64 rust 1 rust cargo

검증 실행 환경

환경 컨트랙트 단계 실행일
rust 1 · linux alpine/x64 · docker ed25519:a2ec939a4c60e243 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1
2026-08-13
rust 1 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1
2026-08-17
rust 1 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1rust:1-alpine@sha256:a10e64dd139b…
2026-09-08

케이스

HOW
목표
Serialize and deserialize a struct as JSON with serde in Rust
패키지
심벌
  • serde_json::to_string
  • serde_json::from_str
  • Deserialize
환경
rust 1
생성일
2026-08-13T15:48:13Z

컨트랙트

  1. derive Serialize and Deserialize on a struct
  2. assert a struct round-trips through JSON unchanged
  3. assert a missing optional field defaults instead of failing
  4. assert a type mismatch produces an error rather than a panic

파일

  • Cargo.lock
  • Cargo.toml
  • csx.json
  • src/main.rs

소스 아티팩트 내려받기 (tar.gz)

소스

Cargo.lock
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4

[[package]]
name = "csx-sample-serde"
version = "1.0.0"
dependencies = [
 "serde",
 "serde_json",
]

[[package]]
name = "itoa"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"

[[package]]
name = "memchr"
version = "2.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"

[[package]]
name = "proc-macro2"
version = "1.0.107"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
dependencies = [
 "unicode-ident",
]

[[package]]
name = "quote"
version = "1.0.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
dependencies = [
 "proc-macro2",
]

[[package]]
name = "serde"
version = "1.0.229"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba"
dependencies = [
 "serde_core",
 "serde_derive",
]

[[package]]
name = "serde_core"
version = "1.0.229"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48"
dependencies = [
 "serde_derive",
]

[[package]]
name = "serde_derive"
version = "1.0.229"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348"
dependencies = [
 "proc-macro2",
 "quote",
 "syn",
]

[[package]]
name = "serde_json"
version = "1.0.151"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c841b55ecdae098c80dcae9cf767f6f8a0c2cdb3416bbef72181df4d0fe73f14"
dependencies = [
 "itoa",
 "memchr",
 "serde",
 "serde_core",
 "zmij",
]

[[package]]
name = "syn"
version = "3.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53e9bae58849f64dfa4f5d5ae372c8341f7305f82a3868709269343628b659a3"
dependencies = [
 "proc-macro2",
 "quote",
 "unicode-ident",
]

[[package]]
name = "unicode-ident"
version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"

[[package]]
name = "zmij"
version = "1.0.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b"
Cargo.toml
[package]
name = "csx-sample-serde"
version = "1.0.0"
edition = "2021"
license = "MIT-0"

[dependencies]
serde = { version = "1.0.229", features = ["derive"] }
serde_json = "1.0.151"
csx.json
{"case":{"caseId":"case:sha256:afdc13bd2d5b2abd3c3d35e48c93cba270ab7104369f9ef9911d2cd864d7997c","constraints":{"runtime":"rust"},"contract":["derive Serialize and Deserialize on a struct","assert a struct round-trips through JSON unchanged","assert a missing optional field defaults instead of failing","assert a type mismatch produces an error rather than a panic"],"goal":"Serialize and deserialize a struct as JSON with serde in Rust","kind":"HOW","packages":["pkg:cargo/serde_json@1.0.151"],"schemaVersion":1,"symbols":["serde_json::to_string","serde_json::from_str","Deserialize"]},"contractCommand":["cargo","run","--offline","--quiet"],"environment":{"arch":"x64","ecosystem":"cargo","executionContext":"rust","language":"rust","os":"linux","packageManager":"cargo","runtime":"rust","runtimeVersion":"1","schemaVersion":1},"license":"MIT-0","packages":["pkg:cargo/serde_json@1.0.151"],"schemaVersion":1,"symbols":["serde_json::to_string","serde_json::from_str","Deserialize"],"verifierAdapter":"cargo@1"}
src/main.rs
use serde::{Deserialize, Serialize};

/// The derive macros live behind serde's "derive" feature. Depending on
/// serde without it is the usual cause of "cannot find derive macro
/// `Serialize`" — the crate is present, the macro is not.
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct Peer {
    peer_id: String,
    port: u16,
    /// Absent in the input rather than null: serde needs the default, or
    /// deserialization fails with "missing field".
    #[serde(default)]
    tags: Vec<String>,
}

fn main() {
    let peer = Peer {
        peer_id: "ed25519:abc".to_string(),
        port: 48620,
        tags: vec!["blob".to_string()],
    };

    let text = serde_json::to_string(&peer).expect("serializes");
    let back: Peer = serde_json::from_str(&text).expect("deserializes");
    assert_eq!(peer, back);

    // A missing field with #[serde(default)] is filled in, not an error.
    let partial: Peer =
        serde_json::from_str(r#"{"peer_id":"ed25519:def","port":1}"#).expect("defaults apply");
    assert_eq!(partial.tags, Vec::<String>::new());

    // A type mismatch is a Result, never a panic.
    let wrong = serde_json::from_str::<Peer>(r#"{"peer_id":"x","port":"not-a-number"}"#);
    assert!(wrong.is_err());

    println!("CONTRACT PASS: serde round-tripped JSON, applied defaults and reported errors");
}

오리진 시더

anonymous