serde_json_ext
An extension library for serde_json that provides configurable bytes serialization formats.
Features
- Configurable bytes serialization formats: Supports multiple byte serialization methods
- Default format: Array format
[1, 2, 3] - Hexadecimal:
"0x010203"or"010203" - Base64: Standard Base64 encoding
- Base64 URL-safe: URL-safe Base64 encoding
- Default format: Array format
- Flexible configuration options:
- Support for hexadecimal prefix (
0x) - Support for EIP-55 checksum encoding
- Support for hexadecimal prefix (
- Complete serialization/deserialization support: Provides the same API as
serde_jsonbut with custom configuration support
Installation
Add to your Cargo.toml:
[]
= "0.1.0"
= { = "1.0", = ["derive"] }
= "0.11" # For marking byte fields
Usage
Serialization Example
use ;
use Serialize;
Deserialization Example
use ;
use Deserialize;
Configuration Options
use Config;
// Create configuration and set byte format
let config = default
.set_bytes_hex // Set to hexadecimal format
.enable_hex_prefix // Enable 0x prefix
.enable_hex_eip55; // Enable EIP-55 checksum encoding
// Or use other formats
let config = default
.set_bytes_base64; // Base64 format
let config = default
.set_bytes_base64_url_safe; // Base64 URL-safe format
let config = default
.set_bytes_default; // Default array format
API Documentation
Serialization Functions
to_string<T>(value: &T, config: &Config) -> Result<String>- Serialize to stringto_string_pretty<T>(value: &T, config: &Config) -> Result<String>- Serialize to formatted stringto_vec<T>(value: &T, config: &Config) -> Result<Vec<u8>>- Serialize to byte vectorto_vec_pretty<T>(value: &T, config: &Config) -> Result<Vec<u8>>- Serialize to formatted byte vectorto_writer<W, T>(writer: &mut W, value: &T, config: &Config) -> Result<()>- Serialize to writerto_writer_pretty<W, T>(writer: &mut W, value: &T, config: &Config) -> Result<()>- Serialize to writer with formatting
Deserialization Functions
from_str<'a, T>(s: &'a str, config: &'a Config) -> Result<T>- Deserialize from stringfrom_slice<'a, T>(v: &'a [u8], config: &'a Config) -> Result<T>- Deserialize from byte slicefrom_reader<R, T>(rdr: R, config: &Config) -> Result<T>- Deserialize from reader
Configuration Methods
set_bytes_default()- Set byte format to default array formatset_bytes_hex()- Set byte format to hexadecimalset_bytes_base64()- Set byte format to Base64set_bytes_base64_url_safe()- Set byte format to Base64 URL-safeenable_hex_prefix()/disable_hex_prefix()- Enable/disable hexadecimal prefixenable_hex_eip55()/disable_hex_eip55()- Enable/disable EIP-55 checksum encoding
Supported Formats
Default Format (Array)
Hexadecimal Format
// With prefix
// Without prefix
Base64 Format
Base64 URL-Safe Format
Notes
- Use
#[serde(with = "serde_bytes")]attribute to mark byte fields that need special serialization - Serialization and deserialization must use the same configuration format
- Hexadecimal strings can be with or without
0xprefix, both are handled correctly during deserialization
License
MIT