Module num_bigfloat::serde

source ·
Expand description

Serialization and deserialization using different formats and data types.

§Examples

Suppose you have a struct that you want to serialize to json and you want the BigFloat values contained in the struct to be exported as formatted strings. The following is an example of how this can be achieved using serde field attributes and num_bigfloat::serde::str module:

use num_bigfloat::BigFloat;
use serde::{Serialize, Deserialize};

// A struct with a field of type BigFloat
#[derive(Serialize, Deserialize)]
struct SomeStruct {

    #[serde(with = "num_bigfloat::serde::str")]
    pub f: BigFloat,
}

// Value to be serialized
let f = BigFloat::parse("-1.234567890123456789012345678901234567890e-12").unwrap();
let val = SomeStruct {
    f,
};

// Serialization
let json = serde_json::to_string(&val).unwrap();

// Result
assert_eq!("{\"f\":\"-1.234567890123456789012345678901234567890e-12\"}", json);

Modules§

  • Serialization to string and deserialization from string.