serde: take over to/write_canonical_json

from PBS' tools module, and feature-guard via optional `serde_json`
dependency.
This commit is contained in:
Fabian Grünbichler 2022-06-21 13:45:27 +02:00
parent f505240065
commit b239b56999
3 changed files with 59 additions and 0 deletions

View File

@ -12,6 +12,7 @@ exclude = [ "debian" ]
anyhow = "1.0" anyhow = "1.0"
base64 = "0.13" base64 = "0.13"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", optional = true }
proxmox-time = { path = "../proxmox-time", version = "1.0.0" } proxmox-time = { path = "../proxmox-time", version = "1.0.0" }

55
proxmox-serde/src/json.rs Normal file
View File

@ -0,0 +1,55 @@
use anyhow::{bail, Error};
use serde_json::Value;
/// Generate canonical JSON.
///
/// This is used (among other things) for preparing JSON documents for signing.
pub fn to_canonical_json(value: &Value) -> Result<Vec<u8>, Error> {
let mut data = Vec::new();
write_canonical_json(value, &mut data)?;
Ok(data)
}
/// Write canonical JSON to `output`.
///
/// This is used (among other things) for preparing JSON documents for signing.
pub fn write_canonical_json(value: &Value, output: &mut Vec<u8>) -> Result<(), Error> {
match value {
Value::Null => bail!("got unexpected null value"),
Value::String(_) | Value::Number(_) | Value::Bool(_) => {
serde_json::to_writer(output, &value)?;
}
Value::Array(list) => {
output.push(b'[');
let mut iter = list.iter();
if let Some(item) = iter.next() {
write_canonical_json(item, output)?;
for item in iter {
output.push(b',');
write_canonical_json(item, output)?;
}
}
output.push(b']');
}
Value::Object(map) => {
output.push(b'{');
let mut keys: Vec<&str> = map.keys().map(String::as_str).collect();
keys.sort_unstable();
let mut iter = keys.into_iter();
if let Some(key) = iter.next() {
serde_json::to_writer(&mut *output, &key)?;
output.push(b':');
write_canonical_json(&map[key], output)?;
for key in iter {
output.push(b',');
serde_json::to_writer(&mut *output, &key)?;
output.push(b':');
write_canonical_json(&map[key], output)?;
}
}
output.push(b'}');
}
}
Ok(())
}

View File

@ -3,6 +3,9 @@
#[macro_use] #[macro_use]
pub mod serde_macros; pub mod serde_macros;
#[cfg(feature = "serde_json")]
pub mod json;
/// Serialize Unix epoch (i64) as RFC3339. /// Serialize Unix epoch (i64) as RFC3339.
/// ///
/// Usage example: /// Usage example: