Lines Matching defs:JsonValue

63 // TODO: Enhance the encapsulation of JsonValue, makes users can't use enum directly.
65 pub enum JsonValue {
85 /// JsonValue print method 1, prints the content directly (without extra double quotes).
89 /// use ylong_json::JsonValue;
91 /// let value: JsonValue = "hello".into();
95 impl Display for JsonValue {
109 impl Debug for JsonValue {
115 impl JsonValue {
116 /// Creates an instance of JsonValue for Null type.
120 /// use ylong_json::JsonValue;
122 /// let value = JsonValue::new_null();
123 /// assert_eq!(value, JsonValue::Null);
129 /// Creates an instance of JsonValue for Boolean type.
133 /// use ylong_json::JsonValue;
135 /// let value = JsonValue::new_boolean(true);
136 /// assert_eq!(value, JsonValue::Boolean(true));
142 /// Creates an instance of JsonValue for Numerical type.
146 /// use ylong_json::JsonValue;
148 /// let value = JsonValue::new_number(0.0.into());
149 /// assert_eq!(value, JsonValue::Number(0.0.into()));
155 /// Creates an instance of JsonValue for String type.
159 /// use ylong_json::JsonValue;
161 /// let value = JsonValue::new_string("Hello World");
163 /// assert_eq!(value, JsonValue::String(String::from("Hello World")));
166 /// // assert_eq!(value, JsonValue::String(CString::new("Hello World")));
179 /// Creates an instance of JsonValue for Array type.
183 /// use ylong_json::{JsonValue, Array};
185 /// let value = JsonValue::new_array(Array::new());
186 /// assert_eq!(value, JsonValue::Array(Array::new()));
192 /// Creates an instance of JsonValue for Object type.
196 /// use ylong_json::{JsonValue, Object};
198 /// let value = JsonValue::new_object(Object::new());
199 /// assert_eq!(value, JsonValue::Object(Object::new()));
205 /// Determines whether JsonValue is Null type. Returns true if yes, false if no.
209 /// use ylong_json::JsonValue;
211 /// let null_value = JsonValue::new_null();
214 /// let other_value = JsonValue::new_number(0.0.into());
221 /// Determines whether JsonValue is Boolean type. Returns true if yes, false if no.
225 /// use ylong_json::JsonValue;
227 /// let boolean_value = JsonValue::new_boolean(true);
230 /// let other_value = JsonValue::new_null();
237 /// Determines whether JsonValue is true. Returns true if yes, false if no.
241 /// use ylong_json::JsonValue;
243 /// let true_value = JsonValue::new_boolean(true);
246 /// let other_value = JsonValue::new_boolean(false);
256 /// Determines whether JsonValue is false. Returns true if yes, false if no.
260 /// use ylong_json::JsonValue;
262 /// let false_value = JsonValue::new_boolean(false);
265 /// let other_value = JsonValue::new_boolean(true);
275 /// Determines whether JsonValue is Numerical type. Returns true if yes, false if no.
279 /// use ylong_json::JsonValue;
281 /// let number_value = JsonValue::new_number(0.0.into());
284 /// let other_value = JsonValue::new_null();
291 /// Determines whether JsonValue is String type. Returns true if yes, false if no.
295 /// use ylong_json::JsonValue;
297 /// let string_value = JsonValue::new_string("Hello World");
300 /// let other_value = JsonValue::new_null();
307 /// Determines whether JsonValue is Array type. Returns true if yes, false if no.
311 /// use ylong_json::{JsonValue, Array};
313 /// let array_value = JsonValue::new_array(Array::new());
316 /// let other_value = JsonValue::new_null();
323 /// Determines whether JsonValue is Object type. Returns true if yes, false if no.
327 /// use ylong_json::{JsonValue, Object};
329 /// let object_value = JsonValue::new_object(Object::new());
332 /// let other_value = JsonValue::new_null();
339 /// Trys to convert JsonValue to a common reference of Boolean type. Conversion failure will return Error.
343 /// use ylong_json::{JsonValue, Error};
345 /// let boolean_value = JsonValue::new_boolean(true);
348 /// let other_value = JsonValue::new_null();
358 /// Trys to convert JsonValue to a common reference of Numerical type. Conversion failure will return Error.
362 /// use ylong_json::{JsonValue, Number, Error};
364 /// let number_value = JsonValue::new_number(0.0.into());
367 /// let other_value = JsonValue::new_null();
377 /// Trys to convert JsonValue to a common reference of String type. Conversion failure will return Error.
383 /// use ylong_json::{JsonValue, Error};
385 /// let string_value = JsonValue::new_string("Hello World");
393 /// let other_value = JsonValue::new_null();
403 /// Trys to convert JsonValue to a common reference of Array type. Conversion failure will return Error.
407 /// use ylong_json::{JsonValue, Array, Error};
409 /// let array_value = JsonValue::new_array(Array::new());
412 /// let other_value = JsonValue::new_null();
422 /// Trys to convert JsonValue to a common reference of Object type. Conversion failure will return Error.
426 /// use ylong_json::{JsonValue, Object, Error};
428 /// let array_value = JsonValue::new_object(Object::new());
431 /// let other_value = JsonValue::new_null();
441 /// Trys to convert JsonValue to a mutable reference of Boolean type. Conversion failure will return Error.
445 /// use ylong_json::{JsonValue, Error};
447 /// let mut boolean_value = JsonValue::new_boolean(true);
450 /// let mut other_value = JsonValue::new_null();
460 /// Trys to convert JsonValue to a mutable reference of Numerical type. Conversion failure will return Error.
464 /// use ylong_json::{JsonValue, Number, Error};
466 /// let mut number_value = JsonValue::new_number(0.0.into());
469 /// let mut other_value = JsonValue::new_null();
479 /// Trys to convert JsonValue to a mutable reference of String type. Conversion failure will return Error.
485 /// use ylong_json::{JsonValue, Error};
487 /// let mut string_value = JsonValue::new_string("Hello World");
495 /// let mut other_value = JsonValue::new_null();
505 /// Trys to convert JsonValue to a mutable reference of Array type. Conversion failure will return Error.
509 /// use ylong_json::{JsonValue, Error, Array};
511 /// let mut array_value = JsonValue::new_array(Array::new());
514 /// let mut other_value = JsonValue::new_null();
524 /// Trys to convert JsonValue to a mutable reference of Object type. Conversion failure will return Error.
528 /// use ylong_json::{JsonValue, Error, Object};
530 /// let mut object_value = JsonValue::new_object(Object::new());
533 /// let mut other_value = JsonValue::new_null();
543 /// Trys to convert JsonValue to Boolean type. This method transfers ownership.
548 /// use ylong_json::{JsonValue, Error};
550 /// let boolean_value = JsonValue::new_boolean(true);
553 /// let other_value = JsonValue::new_null();
563 /// Trys to convert JsonValue to Numerical type. This method transfers ownership.
570 /// use ylong_json::{JsonValue, Number, Error};
572 /// let number_value = JsonValue::new_number(0.0.into());
575 /// let other_value = JsonValue::new_null();
585 /// Trys to convert JsonValue to String type. This method transfers ownership.
592 /// use ylong_json::{JsonValue, Error};
594 /// let string_value = JsonValue::new_string("Hello World");
602 /// let other_value = JsonValue::new_null();
612 /// Trys to convert JsonValue to Array type. This method transfers ownership.
617 /// use ylong_json::{JsonValue, Error, Array};
619 /// let array_value = JsonValue::new_array(Array::new());
622 /// let other_value = JsonValue::new_null();
632 /// Trys to convert JsonValue to Object type. This method transfers ownership.
637 /// use ylong_json::{JsonValue, Error, Object};
639 /// let object_value = JsonValue::new_object(Object::new());
642 /// let other_value = JsonValue::new_null();
657 /// use ylong_json::{Object, JsonValue};
662 /// let mut value: JsonValue = object.into();
666 /// assert_eq!(value["key"], JsonValue::Null);
668 pub fn remove<I: index::Index>(&mut self, index: I) -> Option<JsonValue> {
672 /// Reads the contents from the file and Trys to deserialize to a JsonValue instance.
676 /// use ylong_json::JsonValue;
678 /// let value = JsonValue::from_file("./json.txt").unwrap();
687 /// by the standard library and Trys to deserialize it into a JsonValue instance.
691 /// use ylong_json::JsonValue;
695 /// let value = JsonValue::from_reader(&mut file).unwrap();
706 /// use ylong_json::JsonValue;
713 /// let value = JsonValue::from_text(text.as_bytes()).unwrap();
722 /// Serializes the JsonValue instance to a formatted string with additional whitespace characters.
726 /// use ylong_json::JsonValue;
732 /// let value = JsonValue::from_text(text.as_bytes()).unwrap();
742 /// Serializes the JsonValue instance to a one-line string with no additional whitespace.
746 /// use ylong_json::JsonValue;
749 /// let value = JsonValue::from_text(text.as_bytes()).unwrap();
759 /// Serializes the JsonValue instance to a formatted string with additional whitespace characters.
764 /// use ylong_json::JsonValue;
770 /// let value = JsonValue::from_text(text.as_bytes()).unwrap();
780 /// Serializes the JsonValue instance to a one-line string with no additional whitespace.
785 /// use ylong_json::JsonValue;
788 /// let value = JsonValue::from_text(text.as_bytes()).unwrap();
799 impl FromStr for JsonValue {
802 /// Generates an instance of JsonValue from &str.
807 /// use ylong_json::JsonValue;
814 /// let value = JsonValue::from_str(text).unwrap();
823 impl PartialEq for JsonValue {
835 /// use ylong_json::{JsonValue, Array, Object};
837 /// assert_eq!(JsonValue::new_null(), JsonValue::new_null());
838 /// assert_eq!(JsonValue::new_boolean(true), JsonValue::new_boolean(true));
839 /// assert_eq!(JsonValue::new_number(0.0.into()), JsonValue::new_number(0.0.into()));
840 /// assert_eq!(JsonValue::new_string(""), JsonValue::new_string(""));
841 /// assert_eq!(JsonValue::new_array(Array::new()), JsonValue::new_array(Array::new()));
842 /// assert_eq!(JsonValue::new_object(Object::new()), JsonValue::new_object(Object::new()));
844 /// assert_ne!(JsonValue::new_null(), JsonValue::new_number(0.0.into()));
845 /// assert_ne!(JsonValue::new_boolean(true), JsonValue::new_boolean(false));
848 /// array1.push(JsonValue::new_null());
851 /// array2.push(JsonValue::new_number(0.0.into()));
852 /// assert_ne!(JsonValue::new_array(array1), JsonValue::new_array(array2));
855 /// object1.insert(String::from("Key"), JsonValue::new_null());
858 /// object2.insert(String::from("Key"), JsonValue::new_number(0.0.into()));
859 /// assert_ne!(JsonValue::new_object(object1), JsonValue::new_object(object2));
863 (JsonValue::Null, JsonValue::Null) => true,
864 (JsonValue::Boolean(a), JsonValue::Boolean(b)) => a == b,
865 (JsonValue::Number(a), JsonValue::Number(b)) => a == b,
866 (JsonValue::String(a), JsonValue::String(b)) => a == b,
867 (JsonValue::Array(a), JsonValue::Array(b)) => a == b,
868 (JsonValue::Object(a), JsonValue::Object(b)) => a == b,
874 impl<I: index::Index> core::ops::Index<I> for JsonValue {
875 type Output = JsonValue;
882 impl<I: index::Index> core::ops::IndexMut<I> for JsonValue {
888 impl From<&str> for JsonValue {
889 /// Converts from &str to JsonValue.
893 /// use ylong_json::JsonValue;
895 /// let value: JsonValue = "Hello World".into();
907 impl From<JsonString> for JsonValue {
908 /// Converts from String to JsonValue.
912 /// use ylong_json::JsonValue;
915 /// let value: JsonValue = String::from("Hello World").into();
917 /// // let value: JsonValue = CString::new("Hello World").into();
926 impl From<$type> for JsonValue {
927 #[doc = concat!("从 ", stringify!($type), " 转换为 JsonValue。")]
933 #[doc = concat!("let value: JsonValue = ", stringify!($type), "::default().into();")]
940 impl From<&$type> for JsonValue {
941 #[doc = concat!("从 &", stringify!($type), " 转换为 JsonValue。")]
947 #[doc = concat!("let value: JsonValue = ", stringify!($type), "::default().into();")]
954 impl From<&mut $type> for JsonValue {
955 #[doc = concat!("从 &mut", stringify!($type), " 转换为 JsonValue。")]
961 #[doc = concat!("let value: JsonValue = ", stringify!($type), "::default().into();")]
973 impl From<$type> for JsonValue {
974 #[doc = concat!("从 ", stringify!($type), " 转换为 JsonValue。")]
978 /// use ylong_json::JsonValue;
981 #[doc = concat!("let value: JsonValue = ", stringify!($type), "::MAX.into();")]
991 json_value_from_type!(bool, JsonValue::new_boolean);
992 json_value_from_type!(Array, JsonValue::new_array);
993 json_value_from_type!(Object, JsonValue::new_object);
999 use super::{array::Array, object::Object, JsonValue};
1003 /// UT test for `JsonValue::fmt`.
1009 /// 1. Creates some `JsonValue`s.
1014 let value = JsonValue::new_null();
1018 let value = JsonValue::new_boolean(false);
1022 let value = JsonValue::new_number(12.34.into());
1026 let value = JsonValue::new_string("Hello");
1030 let value = JsonValue::new_array(array!(false, JsonValue::Null, 12.34));
1034 let object = object!("null" => JsonValue::Null);
1035 let value = JsonValue::new_object(object);
1040 /// UT test for `JsonValue::clone`.
1046 /// 1. Creates some `JsonValue`s.
1047 /// 2. Calls `JsonValue::clone`.
1051 let value1 = JsonValue::new_null();
1055 /// UT test for `JsonValue::is_null`.
1061 /// 1. Creates some `JsonValue`s.
1062 /// 2. Calls `JsonValue::is_null`.
1066 assert!(JsonValue::new_null().is_null());
1067 assert!(!JsonValue::new_boolean(true).is_null());
1068 assert!(!JsonValue::new_boolean(false).is_null());
1069 assert!(!JsonValue::new_number(12.34.into()).is_null());
1070 assert!(!JsonValue::new_string("hello").is_null());
1071 assert!(!JsonValue::new_array(Array::new()).is_null());
1072 assert!(!JsonValue::new_object(Object::new()).is_null());
1075 /// UT test for `JsonValue::is_true`.
1081 /// 1. Creates some `JsonValue`s.
1082 /// 2. Calls `JsonValue::is_true`.
1086 assert!(!JsonValue::new_null().is_true());
1087 assert!(JsonValue::new_boolean(true).is_true());
1088 assert!(!JsonValue::new_boolean(false).is_true());
1089 assert!(!JsonValue::new_number(12.34.into()).is_true());
1090 assert!(!JsonValue::new_string("hello").is_true());
1091 assert!(!JsonValue::new_array(Array::new()).is_true());
1092 assert!(!JsonValue::new_object(Object::new()).is_true());
1095 /// UT test for `JsonValue::is_false`.
1101 /// 1. Creates some `JsonValue`s.
1102 /// 2. Calls `JsonValue::is_false`.
1106 assert!(!JsonValue::new_null().is_false());
1107 assert!(!JsonValue::new_boolean(true).is_false());
1108 assert!(JsonValue::new_boolean(false).is_false());
1109 assert!(!JsonValue::new_number(12.34.into()).is_false());
1110 assert!(!JsonValue::new_string("hello").is_false());
1111 assert!(!JsonValue::new_array(Array::new()).is_false());
1112 assert!(!JsonValue::new_object(Object::new()).is_false());
1115 /// UT test for `JsonValue::is_boolean`.
1121 /// 1. Creates some `JsonValue`s.
1122 /// 2. Calls `JsonValue::is_boolean`.
1126 assert!(!JsonValue::new_null().is_boolean());
1127 assert!(JsonValue::new_boolean(true).is_boolean());
1128 assert!(JsonValue::new_boolean(false).is_boolean());
1129 assert!(!JsonValue::new_number(12.34.into()).is_boolean());
1130 assert!(!JsonValue::new_string("hello").is_boolean());
1131 assert!(!JsonValue::new_array(Array::new()).is_boolean());
1132 assert!(!JsonValue::new_object(Object::new()).is_boolean());
1135 /// UT test for `JsonValue::is_number`.
1141 /// 1. Creates some `JsonValue`s.
1142 /// 2. Calls `JsonValue::is_number`.
1146 assert!(!JsonValue::new_null().is_number());
1147 assert!(!JsonValue::new_boolean(true).is_number());
1148 assert!(!JsonValue::new_boolean(false).is_number());
1149 assert!(JsonValue::new_number(12.34.into()).is_number());
1150 assert!(!JsonValue::new_string("hello").is_number());
1151 assert!(!JsonValue::new_array(Array::new()).is_number());
1152 assert!(!JsonValue::new_object(Object::new()).is_number());
1155 /// UT test for `JsonValue::is_string`.
1161 /// 1. Creates some `JsonValue`s.
1162 /// 2. Calls `JsonValue::is_string`.
1166 assert!(!JsonValue::new_null().is_string());
1167 assert!(!JsonValue::new_boolean(true).is_string());
1168 assert!(!JsonValue::new_boolean(false).is_string());
1169 assert!(!JsonValue::new_number(12.34.into()).is_string());
1170 assert!(JsonValue::new_string("hello").is_string());
1171 assert!(!JsonValue::new_array(Array::new()).is_string());
1172 assert!(!JsonValue::new_object(Object::new()).is_string());
1175 /// UT test for `JsonValue::is_array`.
1181 /// 1. Creates some `JsonValue`s.
1182 /// 2. Calls `JsonValue::is_array`.
1186 assert!(!JsonValue::new_null().is_array());
1187 assert!(!JsonValue::new_boolean(true).is_array());
1188 assert!(!JsonValue::new_boolean(false).is_array());
1189 assert!(!JsonValue::new_number(12.34.into()).is_array());
1190 assert!(!JsonValue::new_string("hello").is_array());
1191 assert!(JsonValue::new_array(Array::new()).is_array());
1192 assert!(!JsonValue::new_object(Object::new()).is_array());
1195 /// UT test for `JsonValue::is_object`.
1201 /// 1. Creates some `JsonValue`s.
1202 /// 2. Calls `JsonValue::is_object`.
1206 assert!(!JsonValue::new_null().is_object());
1207 assert!(!JsonValue::new_boolean(true).is_object());
1208 assert!(!JsonValue::new_boolean(false).is_object());
1209 assert!(!JsonValue::new_number(12.34.into()).is_object());
1210 assert!(!JsonValue::new_string("hello").is_object());
1211 assert!(!JsonValue::new_array(Array::new()).is_object());
1212 assert!(JsonValue::new_object(Object::new()).is_object());
1215 /// UT test for `JsonValue::try_as_boolean`.
1221 /// 1. Creates some `JsonValue`s.
1222 /// 2. Calls `JsonValue::try_as_boolean`.
1226 assert!(JsonValue::new_null().try_as_boolean().is_err());
1227 assert!(JsonValue::new_boolean(true).try_as_boolean().is_ok());
1228 assert!(JsonValue::new_boolean(false).try_as_boolean().is_ok());
1229 assert!(JsonValue::new_number(12.34.into())
1232 assert!(JsonValue::new_string("hello").try_as_boolean().is_err());
1233 assert!(JsonValue::new_array(Array::new()).try_as_boolean().is_err());
1234 assert!(JsonValue::new_object(Object::new())
1239 /// UT test for `JsonValue::try_as_number`.
1245 /// 1. Creates some `JsonValue`s.
1246 /// 2. Calls `JsonValue::try_as_number`.
1250 assert!(JsonValue::new_null().try_as_number().is_err());
1251 assert!(JsonValue::new_boolean(true).try_as_number().is_err());
1252 assert!(JsonValue::new_boolean(false).try_as_number().is_err());
1253 assert!(JsonValue::new_number(12.34.into()).try_as_number().is_ok());
1254 assert!(JsonValue::new_string("hello").try_as_number().is_err());
1255 assert!(JsonValue::new_array(Array::new()).try_as_number().is_err());
1256 assert!(JsonValue::new_object(Object::new())
1261 /// UT test for `JsonValue::try_as_string`.
1267 /// 1. Creates some `JsonValue`s.
1268 /// 2. Calls `JsonValue::try_as_string`.
1272 assert!(JsonValue::new_null().try_as_string().is_err());
1273 assert!(JsonValue::new_boolean(true).try_as_string().is_err());
1274 assert!(JsonValue::new_boolean(false).try_as_string().is_err());
1275 assert!(JsonValue::new_number(12.34.into()).try_as_string().is_err());
1276 assert!(JsonValue::new_string("hello").try_as_string().is_ok());
1277 assert!(JsonValue::new_array(Array::new()).try_as_string().is_err());
1278 assert!(JsonValue::new_object(Object::new())
1283 /// UT test for `JsonValue::try_as_array`.
1289 /// 1. Creates some `JsonValue`s.
1290 /// 2. Calls `JsonValue::try_as_array`.
1294 assert!(JsonValue::new_null().try_as_array().is_err());
1295 assert!(JsonValue::new_boolean(true).try_as_array().is_err());
1296 assert!(JsonValue::new_boolean(false).try_as_array().is_err());
1297 assert!(JsonValue::new_number(12.34.into()).try_as_array().is_err());
1298 assert!(JsonValue::new_string("hello").try_as_array().is_err());
1299 assert!(JsonValue::new_array(Array::new()).try_as_array().is_ok());
1300 assert!(JsonValue::new_object(Object::new()).try_as_array().is_err());
1303 /// UT test for `JsonValue::try_as_object`.
1309 /// 1. Creates some `JsonValue`s.
1310 /// 2. Calls `JsonValue::try_as_object`.
1314 assert!(JsonValue::new_null().try_as_object().is_err());
1315 assert!(JsonValue::new_boolean(true).try_as_object().is_err());
1316 assert!(JsonValue::new_boolean(false).try_as_object().is_err());
1317 assert!(JsonValue::new_number(12.34.into()).try_as_object().is_err());
1318 assert!(JsonValue::new_string("hello").try_as_object().is_err());
1319 assert!(JsonValue::new_array(Array::new()).try_as_object().is_err());
1320 assert!(JsonValue::new_object(Object::new()).try_as_object().is_ok());
1323 /// UT test for `JsonValue::try_as_mut_boolean`.
1329 /// 1. Creates some `JsonValue`s.
1330 /// 2. Calls `JsonValue::try_as_mut_boolean`.
1334 assert!(JsonValue::new_null().try_as_mut_boolean().is_err());
1335 assert!(JsonValue::new_boolean(true).try_as_mut_boolean().is_ok());
1336 assert!(JsonValue::new_boolean(false).try_as_mut_boolean().is_ok());
1337 assert!(JsonValue::new_number(12.34.into())
1340 assert!(JsonValue::new_string("hello").try_as_mut_boolean().is_err());
1341 assert!(JsonValue::new_array(Array::new())
1344 assert!(JsonValue::new_object(Object::new())
1349 /// UT test for `JsonValue::try_as_mut_number`.
1355 /// 1. Creates some `JsonValue`s.
1356 /// 2. Calls `JsonValue::try_as_mut_number`.
1360 assert!(JsonValue::new_null().try_as_mut_number().is_err());
1361 assert!(JsonValue::new_boolean(true).try_as_mut_number().is_err());
1362 assert!(JsonValue::new_boolean(false).try_as_mut_number().is_err());
1363 assert!(JsonValue::new_number(12.34.into())
1366 assert!(JsonValue::new_string("hello").try_as_mut_number().is_err());
1367 assert!(JsonValue::new_array(Array::new())
1370 assert!(JsonValue::new_object(Object::new())
1375 /// UT test for `JsonValue::try_as_mut_string`.
1381 /// 1. Creates some `JsonValue`s.
1382 /// 2. Calls `JsonValue::try_as_mut_string`.
1386 assert!(JsonValue::new_null().try_as_mut_string().is_err());
1387 assert!(JsonValue::new_boolean(true).try_as_mut_string().is_err());
1388 assert!(JsonValue::new_boolean(false).try_as_mut_string().is_err());
1389 assert!(JsonValue::new_number(12.34.into())
1392 assert!(JsonValue::new_string("hello").try_as_mut_string().is_ok());
1393 assert!(JsonValue::new_array(Array::new())
1396 assert!(JsonValue::new_object(Object::new())
1401 /// UT test for `JsonValue::try_as_mut_array`.
1407 /// 1. Creates some `JsonValue`s.
1408 /// 2. Calls `JsonValue::try_as_mut_array`.
1412 assert!(JsonValue::new_null().try_as_mut_array().is_err());
1413 assert!(JsonValue::new_boolean(true).try_as_mut_array().is_err());
1414 assert!(JsonValue::new_boolean(false).try_as_mut_array().is_err());
1415 assert!(JsonValue::new_number(12.34.into())
1418 assert!(JsonValue::new_string("hello").try_as_mut_array().is_err());
1419 assert!(JsonValue::new_array(Array::new())
1422 assert!(JsonValue::new_object(Object::new())
1427 /// UT test for `JsonValue::try_as_mut_object`.
1433 /// 1. Creates some `JsonValue`s.
1434 /// 2. Calls `JsonValue::try_as_mut_object`.
1438 assert!(JsonValue::new_null().try_as_mut_object().is_err());
1439 assert!(JsonValue::new_boolean(true).try_as_mut_object().is_err());
1440 assert!(JsonValue::new_boolean(false).try_as_mut_object().is_err());
1441 assert!(JsonValue::new_number(12.34.into())
1444 assert!(JsonValue::new_string("hello").try_as_mut_object().is_err());
1445 assert!(JsonValue::new_array(Array::new())
1448 assert!(JsonValue::new_object(Object::new())
1453 /// UT test for `JsonValue::try_into_boolean`.
1459 /// 1. Creates some `JsonValue`s.
1460 /// 2. Calls `JsonValue::try_into_boolean`.
1464 assert!(JsonValue::new_null().try_into_boolean().is_err());
1465 assert!(JsonValue::new_boolean(true).try_into_boolean().is_ok());
1466 assert!(JsonValue::new_boolean(false).try_into_boolean().is_ok());
1467 assert!(JsonValue::new_number(12.34.into())
1470 assert!(JsonValue::new_string("hello").try_into_boolean().is_err());
1471 assert!(JsonValue::new_array(Array::new())
1474 assert!(JsonValue::new_object(Object::new())
1479 /// UT test for `JsonValue::try_into_number`.
1485 /// 1. Creates some `JsonValue`s.
1486 /// 2. Calls `JsonValue::try_into_number`.
1490 assert!(JsonValue::new_null().try_into_number().is_err());
1491 assert!(JsonValue::new_boolean(true).try_into_number().is_err());
1492 assert!(JsonValue::new_boolean(false).try_into_number().is_err());
1493 assert!(JsonValue::new_number(12.34.into())
1496 assert!(JsonValue::new_string("hello").try_into_number().is_err());
1497 assert!(JsonValue::new_array(Array::new())
1500 assert!(JsonValue::new_object(Object::new())
1505 /// UT test for `JsonValue::try_into_string`.
1511 /// 1. Creates some `JsonValue`s.
1512 /// 2. Calls `JsonValue::try_into_string`.
1516 assert!(JsonValue::new_null().try_into_string().is_err());
1517 assert!(JsonValue::new_boolean(true).try_into_string().is_err());
1518 assert!(JsonValue::new_boolean(false).try_into_string().is_err());
1519 assert!(JsonValue::new_number(12.34.into())
1522 assert!(JsonValue::new_string("hello").try_into_string().is_ok());
1523 assert!(JsonValue::new_array(Array::new())
1526 assert!(JsonValue::new_object(Object::new())
1531 /// UT test for `JsonValue::try_into_array`.
1537 /// 1. Creates some `JsonValue`s.
1538 /// 2. Calls `JsonValue::try_into_array`.
1542 assert!(JsonValue::new_null().try_into_array().is_err());
1543 assert!(JsonValue::new_boolean(true).try_into_array().is_err());
1544 assert!(JsonValue::new_boolean(false).try_into_array().is_err());
1545 assert!(JsonValue::new_number(12.34.into())
1548 assert!(JsonValue::new_string("hello").try_into_array().is_err());
1549 assert!(JsonValue::new_array(Array::new()).try_into_array().is_ok());
1550 assert!(JsonValue::new_object(Object::new())
1555 /// UT test for `JsonValue::try_into_object`.
1561 /// 1. Creates some `JsonValue`s.
1562 /// 2. Calls `JsonValue::try_into_object`.
1566 assert!(JsonValue::new_null().try_into_object().is_err());
1567 assert!(JsonValue::new_boolean(true).try_into_object().is_err());
1568 assert!(JsonValue::new_boolean(false).try_into_object().is_err());
1569 assert!(JsonValue::new_number(12.34.into())
1572 assert!(JsonValue::new_string("hello").try_into_object().is_err());
1573 assert!(JsonValue::new_array(Array::new())
1576 assert!(JsonValue::new_object(Object::new())
1581 /// UT test for `JsonValue::to_formatted_string`.
1587 /// 1. Creates some `JsonValue`s.
1588 /// 2. Calls `JsonValue::to_formatted_string`.
1593 JsonValue::new_null().to_formatted_string().unwrap(),
1598 /// UT test for `JsonValue::to_compact_string`.
1604 /// 1. Creates some `JsonValue`s.
1605 /// 2. Calls `JsonValue::to_compact_string`.
1609 assert_eq!(JsonValue::new_null().to_compact_string().unwrap(), "null");
1612 /// UT test for `JsonValue::from_str`.
1618 /// 1. Calls `JsonValue::from_str` to create a `JsonValue`.
1622 assert_eq!(JsonValue::from_str("null").unwrap(), JsonValue::new_null());
1625 /// UT test for `JsonValue::eq`.
1631 /// 1. Creates some `JsonValue`s.
1632 /// 2. Calls `JsonValue::eq`.
1636 assert_eq!(JsonValue::new_null(), JsonValue::new_null());
1637 assert_eq!(JsonValue::new_boolean(true), JsonValue::new_boolean(true));
1639 JsonValue::new_number(1.into()),
1640 JsonValue::new_number(1.into())
1643 JsonValue::new_string("string"),
1644 JsonValue::new_string("string")
1647 JsonValue::new_array(Array::new()),
1648 JsonValue::new_array(Array::new())
1651 JsonValue::new_object(Object::new()),
1652 JsonValue::new_object(Object::new())
1654 assert_ne!(JsonValue::new_null(), JsonValue::new_boolean(true));
1657 /// UT test for `JsonValue::from`.
1663 /// 1. Calls `JsonValue::from` to create `JsonValue`s.
1667 assert_eq!(JsonValue::from(true), JsonValue::new_boolean(true));
1668 assert_eq!(JsonValue::from(false), JsonValue::new_boolean(false));
1670 JsonValue::from(Array::new()),
1671 JsonValue::new_array(Array::new())
1674 JsonValue::from(Object::new()),
1675 JsonValue::new_object(Object::new())
1678 assert_eq!(JsonValue::from(&true), JsonValue::new_boolean(true));
1679 assert_eq!(JsonValue::from(&false), JsonValue::new_boolean(false));
1681 JsonValue::from(&Array::new()),
1682 JsonValue::new_array(Array::new())
1685 JsonValue::from(&Object::new()),
1686 JsonValue::new_object(Object::new())
1689 assert_eq!(JsonValue::from(&mut true), JsonValue::new_boolean(true));
1690 assert_eq!(JsonValue::from(&mut false), JsonValue::new_boolean(false));
1692 JsonValue::from(&mut Array::new()),
1693 JsonValue::new_array(Array::new())
1696 JsonValue::from(&mut Object::new()),
1697 JsonValue::new_object(Object::new())
1701 assert_eq!(JsonValue::from(String::new()), JsonValue::new_string(""));
1707 JsonValue::from(CString::new("").unwrap()),
1708 JsonValue::new_string("")
1713 /// UT test for `JsonValue::remove`.
1719 /// 1. Creates some `JsonValue`.
1720 /// 2. Calls `JsonValue::remove` on them.
1724 let mut object = JsonValue::new_object(
1727 assert_eq!(object["key1"], JsonValue::new_string("value1"));
1728 assert_eq!(object["key2"], JsonValue::new_string("value2"));
1729 assert_eq!(object["key3"], JsonValue::new_string("value3"));
1732 assert_eq!(object["key1"], JsonValue::new_string("value1"));
1733 assert_eq!(object["key2"], JsonValue::new_null());
1734 assert_eq!(object["key3"], JsonValue::new_string("value3"));
1736 let mut array = JsonValue::new_array(array!(false, JsonValue::new_null(), 12.34));
1737 assert_eq!(array[0], JsonValue::new_boolean(false));
1738 assert_eq!(array[1], JsonValue::new_null());
1739 assert_eq!(array[2], JsonValue::new_number(12.34.into()));
1742 assert_eq!(array[0], JsonValue::new_boolean(false));
1743 assert_eq!(array[1], JsonValue::new_number(12.34.into()));
1744 assert_eq!(array[2], JsonValue::new_null());
1747 /// UT test for `JsonValue::from_reader`.
1753 /// 1. Calls `JsonValue::from_reader` to create some `JsonValue`.
1765 assert!(JsonValue::from_reader(TestErrorIo).is_err());