Skip to main content

df_hyperliquid_core/utils/json/
mod.rs

1//! JSON that keeps what a signature depends on: keys in the order they were
2//! written and numbers as the digits they were written with. An L1 action is
3//! hashed as msgpack, which keeps key order, where `serde_json` sorts keys.
4
5mod msgpack;
6mod parse;
7mod write;
8
9pub use msgpack::Unpackable;
10pub use parse::BadJson;
11
12/// A JSON value that keeps key order and the digits of each number.
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub enum Json {
15    /// `null`.
16    Null,
17    /// `true` or `false`.
18    Bool(bool),
19    /// The number exactly as written: no float ever holds it.
20    Number(String),
21    /// A string, unescaped.
22    String(String),
23    /// An array, items in order.
24    Array(Vec<Json>),
25    /// Keys in the order they were written.
26    Object(Vec<(String, Json)>),
27}
28
29impl Json {
30    /// An object's value under `key`, the first if the key repeats; `None` for a
31    /// missing key or a value that is not an object.
32    #[must_use]
33    pub fn get(&self, key: &str) -> Option<&Json> {
34        match self {
35            Json::Object(entries) => entries.iter().find(|(name, _)| name == key).map(|(_, value)| value),
36            _ => None,
37        }
38    }
39
40    /// The text of a string; `None` for anything else.
41    #[must_use]
42    pub fn as_str(&self) -> Option<&str> {
43        match self {
44            Json::String(text) => Some(text),
45            _ => None,
46        }
47    }
48
49    /// `true` or `false`; `None` for anything else, a `"true"` string too.
50    #[must_use]
51    pub const fn as_bool(&self) -> Option<bool> {
52        match self {
53            Json::Bool(value) => Some(*value),
54            _ => None,
55        }
56    }
57
58    /// A whole non-negative number, as the venue writes counts and ids.
59    #[must_use]
60    pub fn as_u64(&self) -> Option<u64> {
61        match self {
62            Json::Number(digits) => digits.parse().ok(),
63            _ => None,
64        }
65    }
66
67    /// An array's items; `None` for anything else.
68    #[must_use]
69    pub fn as_array(&self) -> Option<&[Json]> {
70        match self {
71            Json::Array(items) => Some(items),
72            _ => None,
73        }
74    }
75
76    /// An object's keys and values in the order written; `None` for anything else.
77    #[must_use]
78    pub fn as_object(&self) -> Option<&[(String, Json)]> {
79        match self {
80            Json::Object(entries) => Some(entries),
81            _ => None,
82        }
83    }
84
85    /// What kind of value this is, for an error message: `null`, `a boolean`, `a number`,
86    /// `a string`, `an array` or `an object`.
87    #[must_use]
88    pub const fn kind(&self) -> &'static str {
89        match self {
90            Json::Null => "null",
91            Json::Bool(_) => "a boolean",
92            Json::Number(_) => "a number",
93            Json::String(_) => "a string",
94            Json::Array(_) => "an array",
95            Json::Object(_) => "an object",
96        }
97    }
98}
99
100#[cfg(test)]
101#[path = "tests/json.rs"]
102mod tests;