]> git.proxmox.com Git - proxmox.git/commitdiff
serde: take over to/write_canonical_json
authorFabian Grünbichler <f.gruenbichler@proxmox.com>
Tue, 21 Jun 2022 11:45:27 +0000 (13:45 +0200)
committerFabian Grünbichler <f.gruenbichler@proxmox.com>
Wed, 29 Jun 2022 08:32:44 +0000 (10:32 +0200)
from PBS' tools module, and feature-guard via optional `serde_json`
dependency.

proxmox-serde/Cargo.toml
proxmox-serde/src/json.rs [new file with mode: 0644]
proxmox-serde/src/lib.rs

index 5919f21e19f06ea59308969d72ee5e023724fb3c..a5d3da28c20195befda4dbcfb8f8715b90fe59fa 100644 (file)
@@ -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" }
 
diff --git a/proxmox-serde/src/json.rs b/proxmox-serde/src/json.rs
new file mode 100644 (file)
index 0000000..63574d3
--- /dev/null
@@ -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(())
+}
index 79c7401ebd2569e7926ae8d295fe728e4d044b8e..e1dc16a330816bf64193a96b5aa08e1dc5484623 100644 (file)
@@ -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: