serde: take over to/write_canonical_json
from PBS' tools module, and feature-guard via optional `serde_json` dependency.
This commit is contained in:
parent
f505240065
commit
b239b56999
@ -12,6 +12,7 @@ exclude = [ "debian" ]
|
||||
anyhow = "1.0"
|
||||
base64 = "0.13"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = { version = "1.0", optional = true }
|
||||
|
||||
proxmox-time = { path = "../proxmox-time", version = "1.0.0" }
|
||||
|
||||
|
55
proxmox-serde/src/json.rs
Normal file
55
proxmox-serde/src/json.rs
Normal 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(())
|
||||
}
|
@ -3,6 +3,9 @@
|
||||
#[macro_use]
|
||||
pub mod serde_macros;
|
||||
|
||||
#[cfg(feature = "serde_json")]
|
||||
pub mod json;
|
||||
|
||||
/// Serialize Unix epoch (i64) as RFC3339.
|
||||
///
|
||||
/// Usage example:
|
||||
|
Loading…
Reference in New Issue
Block a user