1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//! Conversion utilities for working with JSON values and decimal types.
//!
//! Provides functions to convert `serde_json::Value` types into `Decimal` values,
//! handling numeric, string, and other JSON types appropriately.
use Decimal;
use FromPrimitive;
use Value;
use FromStr;
/// Convert a JSON value to a Decimal.
///
/// Attempts to convert the provided JSON value into a `Decimal` type. Supports conversion from:
/// - JSON numbers (integers, unsigned integers, and floats)
/// - JSON strings (parsed as decimal strings)
///
/// # Arguments
///
/// * `value` - A reference to a `serde_json::Value` to convert
///
/// # Returns
///
/// Returns `Some(Decimal)` if the conversion succeeds, or `None` if the value type is
/// unsupported or the conversion fails (e.g., invalid decimal string format).
///
/// # Example
///
/// ```ignore
/// use serde_json::json;
/// use rust_decimal::Decimal;
///
/// let num_value = json!(42);
/// assert_eq!(value_to_decimal(&num_value), Some(Decimal::from(42)));
///
/// let str_value = json!("123.45");
/// assert_eq!(value_to_decimal(&str_value), Some(Decimal::from_str("123.45").unwrap()));
///
/// let null_value = json!(null);
/// assert_eq!(value_to_decimal(&null_value), None);
/// ```