Sigil documentation
ReferenceRust referencesigil-core

sigil-core · serde_helpers

Source declarations, signatures and documentation for serde_helpers.

Source: sigil/node/sigil-core/src/serde_helpers.rs. SHA-256: fc6ba0f5c3d82c53c6bcfac185b52fafe9285532e30b7bb7da9165e57f4f237e.

This reference follows declared source modules, retains conditional attributes, and includes public declarations and implementation methods. Private-module re-exports and trait resolution require the compiler; this is a source reference, not a claim that every listed item is a root import. Function bodies and constant values are omitted.

serde_helpers::string_u128

Serde helpers for types that require special JSON wire encoding.

string_u128

JavaScript's Number type is an IEEE-754 double, which can only represent integers exactly up to 2^53 − 1. A u128 field serialized as a bare JSON number therefore loses precision for any value ≥ 2^53. Furthermore, serde_json (without the arbitrary_precision feature) does not support bare u128 values at all — callers receive a hard error.

The string_u128 module solves both problems by encoding u128 as a decimal string on the wire (e.g. "340282366920938463463374607431768211455"). Deserialization accepts the canonical string form or a bare JSON number (for backward compatibility with Rust callers that serialize natively).

string_u128_option

The same encoding for Option<u128> fields. Some(n) serializes as a quoted decimal string; None serializes as JSON null. Deserialization accepts the canonical string form, a bare JSON number, or null / missing field (treated as None).

Usage

use serde::{Deserialize, Serialize};
use sigil_core::serde_helpers;

#[derive(Serialize, Deserialize)]
struct Foo {
    #[serde(with = "serde_helpers::string_u128")]
    pub token_id: u128,

    #[serde(
        with = "serde_helpers::string_u128_option",
        default,
        skip_serializing_if = "Option::is_none"
    )]
    pub max_supply: Option<u128>,
}

Serialize a u128 as a decimal string; deserialize from a decimal string or a bare JSON number (u64 / u128 fallback for Rust callers).

pub mod string_u128;

Source line: 45.

serde_helpers::string_u128::serialize

Serialize value as a quoted decimal string.

pub fn serialize<S: Serializer>(value: &u128, s: S) -> Result<S::Ok, S::Error>;

Source line: 49.

serde_helpers::string_u128::deserialize

Deserialize a u128 from a quoted decimal string or a bare JSON number.

pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<u128, D::Error>;

Source line: 54.

serde_helpers::string_u128_option

Serialize an Option<u128> as a quoted decimal string (Some) or JSON null (None); deserialize from a quoted decimal string, a bare JSON number, or null / missing field.

Use together with #[serde(default, skip_serializing_if = "Option::is_none")] so that absent fields round-trip correctly.

pub mod string_u128_option;

Source line: 94.

serde_helpers::string_u128_option::serialize

Serialize Some(n) as a quoted decimal string; None as JSON null.

pub fn serialize<S: Serializer>(value: &Option<u128>, s: S) -> Result<S::Ok, S::Error>;

Source line: 98.

serde_helpers::string_u128_option::deserialize

Deserialize an Option<u128> from a quoted decimal string, a bare JSON number, or JSON null / a missing field.

pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<Option<u128>, D::Error>;

Source line: 107.

On this page