-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduces
macros
feature and cleans up serde documentation a littl…
…e bit (#628)
- Loading branch information
Showing
7 changed files
with
159 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Examples | ||
|
||
This contains some more advanced examples of using the rust decimal library of complex usage. | ||
|
||
All examples are crate based to demonstrate feature configurations. Examples can be run by using: | ||
|
||
```shell | ||
cd examples/<example-name> | ||
cargo run | ||
``` | ||
|
||
## serde-json-scenarios | ||
|
||
This example shows how to use the `serde` crate to serialize and deserialize the `Decimal` type using multiple different | ||
serialization formats. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "serde-json-scenarios" | ||
version = "0.0.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[workspace] | ||
|
||
[dependencies] | ||
rust_decimal = { path = "../..", features = ["macros", "serde-with-arbitrary-precision"] } | ||
serde = { version = "1.0", features = ["derive"] } | ||
serde_json = { version = "1.0", features = ["arbitrary_precision"]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use rust_decimal::prelude::*; | ||
|
||
type ExampleResult = Result<(), Box<dyn std::error::Error>>; | ||
|
||
fn main() -> ExampleResult { | ||
demonstrate_default_behavior()?; | ||
demonstrate_arbitrary_precision_deserialization_with_string_serialization()?; | ||
Ok(()) | ||
} | ||
|
||
/// The default behavior of the library always expects string results. That is, it will serialize the | ||
/// Decimal as string, but also expect a string when deserializing. | ||
/// Note: this is not enough for bincode representations since there is no deserialization hint that the | ||
/// field is a string. | ||
fn demonstrate_default_behavior() -> ExampleResult { | ||
#[derive(serde::Serialize, serde::Deserialize)] | ||
struct Total { | ||
value: Decimal, | ||
} | ||
let total = Total { value: dec!(1.23) }; | ||
let serialized = serde_json::to_string(&total)?; | ||
assert_eq!(r#"{"value":"1.23"}"#, serialized); | ||
|
||
// If we try to deserialize the same string we should succeed | ||
let deserialized: Total = serde_json::from_str(&serialized)?; | ||
assert_eq!(dec!(1.23), deserialized.value); | ||
|
||
// Technically, by default we also support deserializing from a number, however this is doing a float | ||
// conversion and is not recommended. | ||
let deserialized: Total = serde_json::from_str(r#"{"value":1.23}"#)?; | ||
assert_eq!(dec!(1.23), deserialized.value); | ||
Ok(()) | ||
} | ||
|
||
/// This demonstrates using arbitrary precision for a decimal value - even though the | ||
/// default string serialization behavior is baked in. | ||
fn demonstrate_arbitrary_precision_deserialization_with_string_serialization() -> ExampleResult { | ||
#[derive(serde::Serialize, serde::Deserialize)] | ||
struct Total { | ||
#[serde(deserialize_with = "rust_decimal::serde::arbitrary_precision::deserialize")] | ||
value: Decimal, | ||
} | ||
|
||
let total = Total { value: dec!(1.23) }; | ||
let serialized = serde_json::to_string(&total)?; | ||
assert_eq!(r#"{"value":"1.23"}"#, serialized); | ||
|
||
// If we try to deserialize the same string we should succeed | ||
let deserialized: Total = serde_json::from_str(&serialized)?; | ||
assert_eq!(dec!(1.23), deserialized.value); | ||
|
||
// If we try to deserialize a float then this will succeed as well | ||
let deserialized: Total = serde_json::from_str(r#"{"value":1.23}"#)?; | ||
assert_eq!(dec!(1.23), deserialized.value); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters