serde_json_ext 0.1.10

A serde_json extension that provides configurable bytes serialization formats (hex, base64, default array)
Documentation

serde_json_ext

JSON helpers built on top of serde_json with configurable byte serialization formats.

Overview

  • default array encoding for byte fields
  • hexadecimal encoding with optional 0x prefix
  • base64 and base64 URL-safe encoding
  • matching serialization and deserialization helpers

Installation

[dependencies]
serde_json_ext = "0.1.9"
serde = { version = "1", features = ["derive"] }
serde_bytes = "0.11"

Usage

use serde::Serialize;
use serde_json_ext::{to_string, Config};

#[derive(Serialize)]
struct Example {
    #[serde(with = "serde_bytes")]
    data: Vec<u8>,
}

let value = Example { data: vec![1, 2, 3, 255] };
let config = Config::default().set_bytes_hex().enable_hex_prefix();

let json = to_string(&value, &config).unwrap();
assert_eq!(json, r#"{"data":"0x010203ff"}"#);

API

  • to_string
  • to_string_pretty
  • to_vec
  • to_vec_pretty
  • to_writer
  • to_writer_pretty
  • to_value
  • from_str
  • from_slice
  • from_reader
  • from_value
  • Config
  • BytesFormat

Notes

  • Use #[serde(with = "serde_bytes")] on byte fields that should use the custom encoding rules.
  • Serialization and deserialization must use the same Config values.
  • Hex strings are accepted with or without the 0x prefix during deserialization.

License

MIT