Skip to content

Commit

Permalink
feat: added JsonElem::Null
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenzoLeonardo committed Mar 24, 2024
1 parent d15d3c3 commit be1e64d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/jsonelem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::error::Error;
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(untagged)]
pub enum JsonElem {
Null,
Integer(i32),
Float(f64),
Bool(bool),
Expand Down Expand Up @@ -71,6 +72,7 @@ impl TryInto<Vec<u8>> for JsonElem {
impl Display for JsonElem {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
JsonElem::Null => write!(f, "{}", JsonElem::Null),
JsonElem::Integer(val) => write!(f, "{}", val),
JsonElem::Float(val) => write!(f, "{}", val),
JsonElem::Bool(val) => write!(f, "{}", val),
Expand Down
27 changes: 27 additions & 0 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,30 @@ fn test_from_jsonelem_to_json_slice() {
let result: Vec<u8> = given.try_into().unwrap();
assert_eq!(expected, result.as_slice());
}

#[test]
fn test_null() {
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct CallMethod {
object: String,
method: String,
param: JsonElem,
}

let call = CallMethod {
object: "my_object".to_string(),
method: "my_function".to_string(),
param: JsonElem::Null,
};
assert_eq!(
r#"{"object":"my_object","method":"my_function","param":null}"#.to_string(),
serde_json::to_string(&call).unwrap()
);

let output = serde_json::from_slice::<CallMethod>(
r#"{"object":"my_object","method":"my_function","param":null}"#.as_bytes(),
)
.unwrap();

assert_eq!(call, output);
}

0 comments on commit be1e64d

Please sign in to comment.