xml2json_rs/
utils.rs

1use serde_json::Value as JsonValue;
2
3// Check if a JSON value is empty
4pub fn json_is_empty(node: &JsonValue) -> bool {
5  match node {
6    JsonValue::Null => true,
7    JsonValue::Bool(_) => false,
8    JsonValue::Number(_) => false,
9    JsonValue::String(ref v) => v.is_empty(),
10    JsonValue::Array(ref v) => v.is_empty(),
11    JsonValue::Object(ref v) => v.is_empty()
12  }
13}
14
15// Check the number of keys in a JsonValue object. If it is another type return 0
16pub fn json_object_key_len(node: &JsonValue) -> usize {
17  match node.as_object() {
18    Some(o) => o.keys().len(),
19    None => 0
20  }
21}
22
23// serde's to_string will wrap strings in quotes, use as_str for Strings,
24// to_string for everything else
25pub fn to_string_raw(node: &JsonValue) -> String {
26  if node.is_string() {
27    node.as_str().unwrap_or("").to_owned()
28  } else {
29    node.to_string()
30  }
31}