Lines Matching refs:JsonValue
14 use crate::{Array, JsonValue, Object};
17 static NULL: JsonValue = JsonValue::Null;
19 /// This trait can be used to get an index based on the subscript of an internal member of JsonValue.
21 /// Gets a common reference to the value with the specified subscript (or key) from a JsonValue.
22 fn index_into<'a>(&self, value: &'a JsonValue) -> &'a JsonValue;
24 /// Gets a mutable reference to the value of the specified subscript (or key) from a JsonValue.
25 fn index_into_mut<'a>(&self, value: &'a mut JsonValue) -> &'a mut JsonValue;
27 /// Removes the member with the specified subscript (or key) from a JsonValue.
28 fn index_remove(&self, value: &mut JsonValue) -> Option<JsonValue>;
32 /// Uses the array subscript to visit the Array type of JsonValue
33 /// and get a common reference to the corresponding JsonValue.
42 /// use ylong_json::{JsonValue, Array};
45 /// assert_eq!(JsonValue::Number(0.0.into())[0], JsonValue::Null);
49 /// array.push(JsonValue::Null);
50 /// array.push(JsonValue::Boolean(true));
51 /// array.push(JsonValue::Number(0.0.into()));
53 /// let value = JsonValue::Array(array);
56 /// assert_eq!(value[0], JsonValue::Null);
57 /// assert_eq!(value[1], JsonValue::Boolean(true));
58 /// assert_eq!(value[2], JsonValue::Number(0.0.into()));
60 /// assert_eq!(value[3], JsonValue::Null);
62 fn index_into<'a>(&self, value: &'a JsonValue) -> &'a JsonValue {
63 if let JsonValue::Array(ref array) = value {
71 /// Uses the array subscript to visit the Array type of JsonValue
72 /// and get a mutable reference to the corresponding JsonValue.
74 /// If the visited JsonValue is not Array type, the JsonValue will be
77 /// If the visited JsonValue is Array type, but the subscript exceeds the length of the array,
78 /// then adds a Null type JsonValue at the end of the array and return a mutable reference of it.
82 /// use ylong_json::{JsonValue, Array};
85 /// let mut value = JsonValue::Null;
86 /// value[0] = JsonValue::Null;
89 /// array.push(JsonValue::Null);
90 /// assert_eq!(value, JsonValue::Array(array));
94 /// array.push(JsonValue::Null);
95 /// let mut value = JsonValue::Array(array);
98 /// value[0] = JsonValue::Number(0.0.into());
99 /// assert_eq!(value[0], JsonValue::Number(0.0.into()));
103 /// value[1] = JsonValue::Boolean(true);
104 /// assert_eq!(value[1], JsonValue::Boolean(true));
107 fn index_into_mut<'a>(&self, value: &'a mut JsonValue) -> &'a mut JsonValue {
108 if let JsonValue::Array(ref mut array) = value {
112 array.push(JsonValue::Null);
116 *value = JsonValue::new_array(Array::new());
120 /// Removes the element at the specified location of Array type JsonValue and returns that content.
124 /// use ylong_json::{JsonValue, Array};
129 /// let mut value: JsonValue = JsonValue::Array(array);
133 /// assert_eq!(value[0], JsonValue::Null);
136 fn index_remove(&self, value: &mut JsonValue) -> Option<JsonValue> {
137 if let JsonValue::Array(ref mut array) = value {
147 /// Uses key to visit Object type JsonValue, and returns a common reference to corresponding JsonValue.
156 /// use ylong_json::{JsonValue, Object};
159 /// assert_eq!(JsonValue::Number(0.0.into())["key"], JsonValue::Null);
163 /// object.insert(String::from("key"), JsonValue::Number(0.0.into()));
165 /// let value = JsonValue::Object(object);
168 /// assert_eq!(value["key"], JsonValue::Number(0.0.into()));
171 /// assert_eq!(value["not exist"], JsonValue::Null);
173 fn index_into<'a>(&self, value: &'a JsonValue) -> &'a JsonValue {
174 if let JsonValue::Object(ref object) = value {
180 /// Uses key to visit Object type JsonValue, and returns a mutable reference to corresponding JsonValue.
182 /// If the visited JsonValue is not Object type, the JsonValue will be
185 /// If the visited JsonValue is of object type but does not contain the key, a key-value pair of
186 /// the key and a null type will be inserted and returns a mutable reference to the JsonValue.
191 /// use ylong_json::{JsonValue, Object};
194 /// let mut value = JsonValue::Null;
196 /// object.insert(String::from("key"), JsonValue::Number(0.0.into()));
198 /// value["key"] = JsonValue::Number(0.0.into());
199 /// assert_eq!(value, JsonValue::Object(object));
203 /// object.insert(String::from("key"), JsonValue::Number(0.0.into()));
204 /// let mut value = JsonValue::Object(object);
207 /// value["key"] = JsonValue::Boolean(true);
208 /// assert_eq!(value["key"], JsonValue::Boolean(true));
212 /// value["not exist"] = JsonValue::Number(1.1.into());
213 /// assert_eq!(value["not exist"], JsonValue::Number(1.1.into()));
216 fn index_into_mut<'a>(&self, value: &'a mut JsonValue) -> &'a mut JsonValue {
217 if let JsonValue::Object(ref mut object) = value {
227 object.insert(String::from(self), JsonValue::Null);
233 object.insert(String::from(self), JsonValue::Null);
238 *value = JsonValue::Object(Object::new());
242 /// Removes the element at the specified location of Object type JsonValue and returns that content.
246 /// use ylong_json::{Object, JsonValue};
251 /// let mut value: JsonValue = object.into();
255 /// assert_eq!(value["key"], JsonValue::Null);
258 fn index_remove(&self, value: &mut JsonValue) -> Option<JsonValue> {
259 if let JsonValue::Object(ref mut object) = value {
268 fn index_into<'a>(&self, value: &'a JsonValue) -> &'a JsonValue {
273 fn index_into_mut<'a>(&self, value: &'a mut JsonValue) -> &'a mut JsonValue {
278 fn index_remove(&self, value: &mut JsonValue) -> Option<JsonValue> {
288 fn index_into<'v>(&self, value: &'v JsonValue) -> &'v JsonValue {
293 fn index_into_mut<'v>(&self, value: &'v mut JsonValue) -> &'v mut JsonValue {
298 fn index_remove(&self, value: &mut JsonValue) -> Option<JsonValue> {
318 use crate::{Array, Index, JsonValue, Object};
326 /// 1. Creates some `usize`s and some `JsonValue`s.
331 let value = JsonValue::new_boolean(true);
341 /// 1. Creates some `usize`s and some `JsonValue`s.
346 let mut value = JsonValue::new_array(array!(1));
350 let mut value = JsonValue::new_null();
361 /// 1. Creates some `usize`s and some `JsonValue`s.
366 let mut value = JsonValue::new_array(array!(1));
369 Some(JsonValue::new_number(1.into()))
380 /// 1. Creates some `str`s and some `JsonValue`s.
385 let value = JsonValue::new_boolean(true);
395 /// 1. Creates some `str`s and some `JsonValue`s.
400 let mut value = JsonValue::new_object(object!("key1" => "value1"));
404 let mut value = JsonValue::new_null();
415 /// 1. Creates some `str`s and some `JsonValue`s.
420 let mut value = JsonValue::new_object(object!("key1" => "value1"));
423 Some(JsonValue::new_string("value1"))
426 let mut value = JsonValue::new_null();
436 /// 1. Creates some `String`s and some `JsonValue`s.
441 let value = JsonValue::new_boolean(true);
451 /// 1. Creates some `String`s and some `JsonValue`s.
456 let mut value = JsonValue::new_object(object!("key1" => "value1"));
460 let mut value = JsonValue::new_null();
471 /// 1. Creates some `String`s and some `JsonValue`s.
476 let mut value = JsonValue::new_object(object!("key1" => "value1"));
479 Some(JsonValue::new_string("value1"))