1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
use crate::{models::JsonPropertyMap, models::JsonValue};
use crate::parsing::JsonNodeParser;
use crate::utils::SurroundWith;
use crate::Result;
#[derive(Debug, PartialEq, Clone)]
pub enum JsonNode {
Value(JsonValue),
Object(JsonPropertyMap),
Array(Vec<JsonNode>),
}
impl JsonNode {
/// Parse a JSON string slice into a `JsonNode` structure.
///
/// # Arguments
///
/// * `json` - The JSON you wish to be parsed.
///
/// # Examples
///
/// ```
/// use json_node::{JsonNode, JsonValue};
///
/// // Create a valid JSON string.
/// let json = "10";
/// // Manually create a tree with the expected structure and value.
/// let expected = JsonNode::Value(JsonValue::Integer(10));
///
/// // Parse the json string into a node tree.
/// let node_tree = JsonNode::parse(json).unwrap();
///
/// assert_eq!(node_tree, expected);
/// ```
pub fn parse(json: &str) -> Result<JsonNode> {
JsonNodeParser::parse_node(json, None)
}
/// Checks if the node is the `JsonNode::Value` discriminant.
///
/// # Examples
///
/// ```
/// use json_node::{JsonNode, JsonValue};
///
/// // Create a value node.
/// let value_node = JsonNode::Value(JsonValue::Boolean(true));
/// // Create a non-value node.
/// let non_value_node = JsonNode::Array(Vec::new());
///
/// assert!(value_node.is_value());
/// assert!(!non_value_node.is_value())
/// ```
///
/// # Remarks
///
/// Note that this function with return true even if the value type is `JsonValue::Null`.
///
/// ```
/// use json_node::{JsonNode, JsonValue};
///
/// let null_value_node = JsonNode::Value(JsonValue::Null);
/// assert!(null_value_node.is_value());
/// ```
pub fn is_value(&self) -> bool {
match self {
JsonNode::Value(_) => true,
_ => false,
}
}
/// Checks if the node is the JsonNode::Object discriminant.
///
/// # Examples
///
/// ```
/// use json_node::{JsonNode, JsonValue, JsonPropertyMap};
///
/// // Create an object node.
/// let object_node = JsonNode::Object(JsonPropertyMap::new());
/// // Create a non-object node.
/// let non_object_node = JsonNode::Value(JsonValue::Null);
///
/// assert!(object_node.is_object());
/// assert!(!non_object_node.is_object())
/// ```
pub fn is_object(&self) -> bool {
match self {
JsonNode::Object(_) => true,
_ => false,
}
}
/// Checks if the node is the JsonNode::Array discriminant.
///
/// # Examples
///
/// ```
/// use json_node::{JsonNode, JsonValue};
/// // Create an array node.
/// let array_node = JsonNode::Array(Vec::new());
/// // Create a non-array node.
/// let non_array_node = JsonNode::Value(JsonValue::Null);
///
/// assert!(array_node.is_array());
/// assert!(!non_array_node.is_array())
/// ```
pub fn is_array(&self) -> bool {
match self {
JsonNode::Array(_) => true,
_ => false,
}
}
/// Extracts the `JsonValue` contained inside the node if it is the `JsonNode::Value` discriminant.
///
/// # Examples
///
/// ```
/// use json_node::{JsonNode, JsonValue};
///
/// // Create a value node.
/// let value_node = JsonNode::Value(JsonValue::Integer(42));
///
/// // Extract `JsonValue`.
/// let as_value_some = value_node.as_value(); // Option<&JsonValue>
///
/// assert_eq!(as_value_some, Some(&JsonValue::Integer(42)));
///
/// // Create a non-value node.
/// let non_value_node = JsonNode::Array(Vec::new());
///
/// // Fail to extract `JsonValue`.
/// let as_value_none = non_value_node.as_value();
///
/// assert_eq!(as_value_none, None);
/// ```
pub fn as_value(&self) -> Option<&JsonValue> {
match self {
JsonNode::Value(value) => Some(value),
_ => None,
}
}
/// Extracts the `JsonPropertyMap` contained inside the node if it is the `JsonNode::Object` discriminant.
///
/// # Examples
///
/// ```
/// use json_node::{JsonNode, JsonValue, JsonPropertyMap};
///
/// // Create an object node.
/// let object_node = JsonNode::Object(JsonPropertyMap::new());
///
/// // Extract `JsonPropertyMap`.
/// let as_object_some = object_node.as_object(); // Option<&JsonPropertyMap>
///
/// assert_eq!(as_object_some, Some(&JsonPropertyMap::new()));
///
/// // Create a non-object node.
/// let non_object_node = JsonNode::Value(JsonValue::Null);
///
/// // Fail to extract `JsonPropertyMap`.
/// let as_object_none = non_object_node.as_object();
///
/// assert_eq!(as_object_none, None);
/// ```
pub fn as_object(&self) -> Option<&JsonPropertyMap> {
match self {
JsonNode::Object(object) => Some(object),
_ => None,
}
}
/// Extracts the `Vec<JsonNode>` contained inside the node if it is the `JsonNode::Array` discriminant.
///
/// # Examples
///
/// ```
/// use json_node::{JsonNode, JsonValue};
///
/// // Create an array node.
/// let array_node = JsonNode::Array(Vec::new());
///
/// // Extract `Vec<JsonNode>`.
/// let as_array_some = array_node.as_array(); // Option<&Vec<JsonNode>>
///
/// assert_eq!(as_array_some, Some(&Vec::new()));
///
/// // Create a non-array node.
/// let non_array_node = JsonNode::Value(JsonValue::Null);
///
/// // Fail to extract `Vec<JsonNode>`.
/// let as_array_none = non_array_node.as_array();
///
/// assert_eq!(as_array_none, None);
/// ```
pub fn as_array(&self) -> Option<&Vec<JsonNode>> {
match self {
JsonNode::Array(array) => Some(array),
_ => None,
}
}
/// Extracts the `JsonValue` contained inside the node if it is the `JsonNode::Value` discriminant as a mutable value.
///
/// # Examples
///
/// ```
/// use json_node::{JsonNode, JsonValue};
///
/// // Create a value node.
/// let mut value_node = JsonNode::Value(JsonValue::Integer(42));
///
/// // Extract `JsonValue`.
/// let as_value_some = value_node.as_value_mut(); // Option<&mut JsonValue>
///
/// assert_eq!(as_value_some, Some(&mut JsonValue::Integer(42)));
///
/// // Create a non-value node.
/// let mut non_value_node = JsonNode::Array(Vec::new());
///
/// // Fail to extract `JsonValue`.
/// let as_value_none = non_value_node.as_value_mut();
///
/// assert_eq!(as_value_none, None);
/// ```
pub fn as_value_mut(&mut self) -> Option<&mut JsonValue> {
match self {
JsonNode::Value(value) => Some(value),
_ => None,
}
}
/// Extracts the `JsonPropertyMap` contained inside the node if it is the `JsonNode::Object` discriminant as a mutable value.
///
/// # Examples
///
/// ```
/// use json_node::{JsonNode, JsonValue, JsonPropertyMap};
///
/// // Create an object node.
/// let mut object_node = JsonNode::Object(JsonPropertyMap::new());
///
/// // Extract `JsonPropertyMap`.
/// let as_object_some = object_node.as_object_mut(); // Option<&mut JsonPropertyMap>
///
/// assert_eq!(as_object_some, Some(&mut JsonPropertyMap::new()));
///
/// // Create a non-object node.
/// let mut non_object_node = JsonNode::Value(JsonValue::Null);
///
/// // Fail to extract `JsonPropertyMap`.
/// let as_object_none = non_object_node.as_object_mut();
///
/// assert_eq!(as_object_none, None);
/// ```
pub fn as_object_mut(&mut self) -> Option<&mut JsonPropertyMap> {
match self {
JsonNode::Object(object) => Some(object),
_ => None,
}
}
/// Extracts the `Vec<JsonNode>` contained inside the node if it is the `JsonNode::Array` discriminant as a mutable value.
///
/// # Examples
///
/// ```
/// use json_node::{JsonNode, JsonValue, JsonPropertyMap};
///
/// // Create an array node.
/// let mut array_node = JsonNode::Array(Vec::new());
///
/// // Extract `Vec<JsonNode>`.
/// let as_array_some = array_node.as_array_mut(); // Option<&mut Vec<JsonNode>>
///
/// assert_eq!(as_array_some, Some(&mut Vec::new()));
///
/// // Create a non-array node.
/// let mut non_array_node = JsonNode::Value(JsonValue::Null);
///
/// // Fail to extract `JsonPropertyMap`.
/// let as_array_none = non_array_node.as_array_mut();
///
/// assert_eq!(as_array_none, None);
/// ```
pub fn as_array_mut(&mut self) -> Option<&mut Vec<JsonNode>> {
match self {
JsonNode::Array(array) => Some(array),
_ => None,
}
}
/// Convert the node tree to a JSON string.
///
/// # Examples
///
/// ```
/// use json_node::{JsonNode, JsonValue};
///
/// // Create a JsonNode tree.
/// let node_tree = JsonNode::Array(Vec::from([
/// JsonNode::Value(JsonValue::Integer(0)),
/// JsonNode::Value(JsonValue::Float(0.5)),
/// JsonNode::Value(JsonValue::Integer(1)),
/// JsonNode::Value(JsonValue::Null),
/// JsonNode::Value(JsonValue::Boolean(false))
/// ]));
///
/// let json_string = node_tree.to_json_string();
///
/// assert_eq!(json_string, "[0,0.5,1,null,false]".to_owned());
/// ```
///
/// # Remarks
///
/// This function does zero formatting. The entire JSON string is returned without any spaces or new-lines.
pub fn to_json_string(&self) -> String {
match self {
JsonNode::Value(value) => value.to_json_string(),
JsonNode::Object(object) => object.to_json_string(),
JsonNode::Array(array) => {
array
.iter()
.map(|node| node.to_json_string())
.collect::<Vec<String>>()
.join(",")
.surround_with("[", "]")
},
}
}
}
impl<'a> IntoIterator for &'a JsonNode {
type Item = &'a JsonNode;
type IntoIter = Iter<'a>;
/// Turns the node tree into an iterator which iterates over evey `JsonValue` in the tree in a depth first manner.
///
/// # Examples
///
/// ```
/// use json_node::{JsonNode, JsonValue};
///
/// let node_tree = JsonNode::Array(Vec::from([
/// JsonNode::Array(Vec::from([ // First element is an array with the value `1` inside.
/// JsonNode::Value(JsonValue::Integer(1)),
/// ])),
/// JsonNode::Value(JsonValue::Integer(2)), // Second element is the value `2`.
/// JsonNode::Array(Vec::from([
/// JsonNode::Value(JsonValue::Integer(3)) // Third element is an array with the value `3` inside.
/// ]))
/// ]));
///
/// let sequence = node_tree.into_iter().collect::<Vec<&JsonNode>>();
///
/// let expected = vec![
/// &JsonNode::Value(JsonValue::Integer(1)),
/// &JsonNode::Value(JsonValue::Integer(2)),
/// &JsonNode::Value(JsonValue::Integer(3))
/// ];
///
/// assert_eq!(sequence, expected);
/// ```
fn into_iter(self) -> Self::IntoIter {
Iter {
node: Some(self),
array_index: None,
object_index: None,
child: None,
}
}
}
pub struct Iter<'a> {
node: Option<&'a JsonNode>,
array_index: Option<usize>,
object_index: Option<usize>,
child: Option<Box<Iter<'a>>>,
}
impl<'a> Iterator for Iter<'a> {
type Item = &'a JsonNode;
fn next(&mut self) -> Option<Self::Item> {
if let Some(iter) = &mut self.child {
if let Some(node) = iter.next() {
return Some(node);
}
self.child = None;
}
if let None = self.node {
return None; // Termination point for iteration. If the iterator has recursed, this allows the parent iterator to continue.
}
match self.node.unwrap() {
JsonNode::Array(nodes) => {
match self.array_index {
Some(mut index) => {
index = index + 1;
self.array_index = Some(index);
self.child = Some(Box::new(nodes[index].into_iter()));
let next = self.next();
if index == nodes.len() - 1 {
self.array_index = None;
self.node = None;
}
return next;
},
None => {
self.array_index = Some(0);
self.child = Some(Box::new(nodes[0].into_iter()));
let next = self.next();
if nodes.len() == 1 {
self.array_index = None;
self.node = None;
}
return next;
},
}
},
JsonNode::Object(properties) => {
match self.object_index {
Some(mut index) => {
index = index + 1;
self.object_index = Some(index);
self.child = Some(Box::new(properties[index].1.into_iter()));
let next = self.next();
if index == properties.len() - 1 {
self.object_index = None;
self.node = None;
}
return next;
},
None => {
self.object_index = Some(0);
self.child = Some(Box::new(properties[0].1.into_iter()));
let next = self.next();
if properties.len() == 1 {
self.object_index = None;
}
return next;
},
}
},
JsonNode::Value(_) => {
let node = self.node.unwrap();
self.node = None;
Some(node)
},
}
}
}
#[cfg(test)]
mod tests {
use crate::JsonValue;
use super::JsonNode;
#[test]
fn json_node_is_value() {
let mut node = JsonNode::Value(JsonValue::String("Hello, World!".to_string()));
assert!(node.is_value());
node = JsonNode::Value(JsonValue::Integer(123));
assert!(node.is_value());
node = JsonNode::Value(JsonValue::Float(123.456));
assert!(node.is_value());
node = JsonNode::Value(JsonValue::Boolean(true));
assert!(node.is_value());
node = JsonNode::Value(JsonValue::Null);
assert!(node.is_value());
}
#[test]
fn iterate_works() {
let json = r#"
{
"name": "Jason",
"age": 30,
"isMale": true,
"height": 1.8,
"numbers": [1, 2, 3, 4, 5],
"children": [
{
"name": "Jason Jr.",
"age": 5,
"isMale": true,
"height": 1.2
},
{
"name": "Jasmine",
"age": 3,
"isMale": false,
"height": 1.1
}
]
}"#;
let node = JsonNode::parse(json).unwrap();
for e in node.into_iter() {
println!("{:?}", e)
}
}
}
#[cfg(test)]
mod doc_tests{
#[test]
fn parse_doc() {
use super::{JsonNode, JsonValue};
// Create a valid JSON string.
let json = "10";
// Manually create a tree with the expected structure and value.
let expected = JsonNode::Value(JsonValue::Integer(10));
// Parse the json string into a node tree.
let node_tree = JsonNode::parse(json).unwrap();
assert_eq!(node_tree, expected);
}
#[test]
fn into_iter() {
use crate::{JsonNode, JsonValue};
let node_tree = JsonNode::Array(Vec::from([
JsonNode::Array(Vec::from([ // First element is an array with the value `1` inside.
JsonNode::Value(JsonValue::Integer(1)),
])),
JsonNode::Value(JsonValue::Integer(2)), // Second element is the value `2`.
JsonNode::Array(Vec::from([
JsonNode::Value(JsonValue::Integer(3)) // Third element is an array with the value `3` inside.
]))
]));
let sequence = node_tree.into_iter().collect::<Vec<&JsonNode>>();
let expected = vec![
&JsonNode::Value(JsonValue::Integer(1)),
&JsonNode::Value(JsonValue::Integer(2)),
&JsonNode::Value(JsonValue::Integer(3))
];
assert_eq!(sequence, expected);
}
}