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
58
59
60
61
62
63
64
65
66
//! Utilities for serializing and deserializing types using their string representations.
//!
//! This module provides helper functions to serialize and deserialize types that implement
//! [`ToString`] and [`FromStr`] respectively. These functions are intended to be used with
//! Serde's `#[serde(with = "...")]` attribute to enable (de)serialization via string conversion.
//!
//! # Note
//! The `ToString` and `FromStr` implementations for a type must be true inverses of each other
//! for correct round-trip serialization and deserialization. If this is not the case, data loss
//! or errors may occur.
//!
//! # Example
//! ```rust
//! use std::net::IpAddr;
//! use serde::{Serialize, Deserialize};
//! use serde_json;
//!
//! #[derive(Serialize, Deserialize, Debug, PartialEq)]
//! struct Wrapper {
//! #[serde(with = "serde_extras::to_from_str")]
//! ip: IpAddr,
//! }
//!
//! let w = Wrapper { ip: IpAddr::V4("127.0.0.1".parse().unwrap()) };
//! let json = serde_json::to_string(&w).unwrap();
//! assert_eq!(json, r#"{"ip":"127.0.0.1"}"#);
//! let de: Wrapper = serde_json::from_str(&json).unwrap();
//! assert_eq!(de, w);
//! ```
use ;
use ;
/// Deserializes a value from a string using its [`FromStr`] implementation.
///
/// This function is intended to be used with Serde's `#[serde(deserialize_with = "...")]` attribute.
/// It attempts to parse the input string into the target type `T`. If parsing fails, a Serde error
/// is returned.
///
/// # Errors
/// Returns a Serde error if the input string cannot be parsed into the target type.
/// Serializes a value to a string using its [`ToString`] implementation.
///
/// This function is intended to be used with Serde's `#[serde(serialize_with = "...")]` attribute.
/// It converts the value to a string and serializes it as a string.
///
/// # Errors
/// Returns a Serde error if serialization fails.