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
// License: see LICENSE file at root directory of `master` branch //! # Value mod formatter; mod impls; use { alloc::{ collections::BTreeMap, string::String, vec::Vec, }, crate::Number, }; #[cfg(feature="std")] use { core::{ convert::TryFrom, str::FromStr, }, std::io::Write, crate::{Error, IoResult}, self::formatter::*, }; /// # Array /// /// ## Shortcuts /// /// <small>[`array()`], [`array_with_capacity()`], [`push()`]</small> /// /// [`array()`]: fn.array.html /// [`array_with_capacity()`]: fn.array_with_capacity.html /// [`push()`]: fn.push.html pub type Array = Vec<Value>; /// # Object /// /// ## Shortcuts /// /// <small>[`object()`], [`insert()`]</small> /// /// [`object()`]: fn.object.html /// [`insert()`]: fn.insert.html pub type Object = BTreeMap<ObjectKey, Value>; /// # Object key pub type ObjectKey = String; /// # A value /// /// ## Formatting /// /// ### Formatting as JSON string /// /// - To format as compacted JSON string, you can use [`format()`] or [`format_as_bytes()`]. /// - To format as a nice JSON string, you can use [`format_nicely()`]. /// /// ### Writing as JSON string to [`Write`][std::io/Write] /// /// Can be done via [`write()`] or [`write_nicely()`]. /// /// ## Converting Rust types to `Value` and vice versa /// /// There are some implementations: /// /// ```ignore /// impl From<...> for Value; /// impl TryFrom<&Value> for ...; /// impl TryFrom<Value> for ...; /// ``` /// /// About [`TryFrom`][core::convert/TryFrom] implementations: /// /// - For primitives, since they're cheap, they have implementations on either a borrowed or an owned value. /// - For collections such as [`String`], [`Object`], [`Array`]..., they only have implementations on an owned value. So data is moved, not /// copied. /// /// ## Shortcuts /// /// A root JSON value can be either an object or an array. For your convenience, there are some shortcuts, like below examples. /// /// - Object: /// /// ``` /// # #[cfg(feature="std")] /// # fn test() -> sj::IoResult<()> { /// use core::convert::TryFrom; /// /// let mut object = sj::object(); /// object.insert("first", true)?; /// object.insert("second", <Option<u8>>::None)?; /// object.insert(String::from("third"), "...")?; /// /// assert!(bool::try_from(object.by(&["first"])?)?); /// assert!(object.take_by(&["second"])?.try_into_or(true)?); /// assert_eq!(object.format()?, r#"{"first":true,"third":"..."}"#); /// # Ok(()) } /// # #[cfg(feature="std")] /// # test().unwrap(); /// # Ok::<_, sj::Error>(()) /// ``` /// /// - Array: /// /// ``` /// # #[cfg(feature="std")] /// # fn test() -> sj::IoResult<()> { /// use core::convert::TryFrom; /// /// let mut array = sj::array(); /// array.push(false)?; /// array.push("a string")?; /// array.push(Some(sj::object()))?; /// /// assert!(bool::try_from(array.at(&[0])?)? == false); /// assert_eq!(array.format()?, r#"[false,"a string",{}]"#); /// # Ok(()) } /// # #[cfg(feature="std")] /// # test().unwrap(); /// # Ok::<_, sj::Error>(()) /// ``` /// /// [core::convert/TryFrom]: https://doc.rust-lang.org/core/convert/trait.TryFrom.html /// [std::io/Write]: https://doc.rust-lang.org/std/io/trait.Write.html /// /// [`Array`]: #variant.Array /// [`Object`]: #variant.Object /// [`String`]: #variant.String /// [`format()`]: #method.format /// [`format_nicely()`]: #method.format_nicely /// [`format_as_bytes()`]: #method.format_as_bytes /// [`write()`]: #method.write /// [`write_nicely()`]: #method.write_nicely #[derive(Debug, Clone)] pub enum Value { /// [_Shortcuts_](#shortcuts-for-string) String(String), /// ### Shortcuts /// /// <small>[`TryFrom`][core::convert/TryFrom]</small> /// /// [core::convert/TryFrom]: https://doc.rust-lang.org/core/convert/trait.TryFrom.html Number(Number), /// ### Shortcuts /// /// <small>[`TryFrom`][core::convert/TryFrom]</small> /// /// [core::convert/TryFrom]: https://doc.rust-lang.org/core/convert/trait.TryFrom.html Boolean(bool), /// [_Shortcuts_](#shortcuts-for-null) Null, /// [_Shortcuts_](#shortcuts-for-object) Object(Object), /// [_Shortcuts_](#shortcuts-for-array) Array(Array), } impl Value { /// # Formats this value as a compacted JSON string #[cfg(feature="std")] pub fn format_as_bytes(&self) -> IoResult<Vec<u8>> { let mut buf = Vec::with_capacity(formatter::estimate_format_size(self, None, None)); self.write(&mut buf)?; buf.flush().map(|()| buf) } /// # Nicely formats this value as JSON string /// /// If you don't provide tab size, default (`4`) will be used. #[cfg(feature="std")] pub fn format_nicely_as_bytes(&self, tab: Option<u8>) -> IoResult<Vec<u8>> { let mut buf = Vec::with_capacity(formatter::estimate_format_size(self, Some(tab.unwrap_or(formatter::DEFAULT_TAB_WIDTH)), None)); self.write_nicely(tab, &mut buf)?; buf.flush().map(|()| buf) } /// # Formats this value as a compacted JSON string #[cfg(feature="std")] pub fn format(&self) -> IoResult<String> { #[allow(unsafe_code)] Ok(unsafe { String::from_utf8_unchecked(self.format_as_bytes()?) }) } /// # Nicely formats this value as JSON string /// /// If you don't provide tab size, default (`4`) will be used. #[cfg(feature="std")] pub fn format_nicely(&self, tab: Option<u8>) -> IoResult<String> { #[allow(unsafe_code)] Ok(unsafe { String::from_utf8_unchecked(self.format_nicely_as_bytes(tab)?) }) } /// # Writes this value as compacted JSON string to a stream /// /// ## Notes /// /// - The stream is used as-is. For better performance, you _should_ wrap your stream inside a [`BufWriter`][std::io/BufWriter]. /// - This function does **not** flush the stream when done. /// /// [std::io/BufWriter]: https://doc.rust-lang.org/std/io/struct.BufWriter.html #[cfg(feature="std")] pub fn write<W>(&self, stream: &mut W) -> IoResult<()> where W: Write { Formatter::new(None).format(self, stream) } /// # Writes this value as nicely formatted JSON string to a stream /// /// ## Notes /// /// - If you don't provide tab size, default (`4`) will be used. /// - The stream is used as-is. For better performance, you _should_ wrap your stream inside a [`BufWriter`][std::io/BufWriter]. /// - This function does **not** flush the stream when done. /// /// [std::io/BufWriter]: https://doc.rust-lang.org/std/io/struct.BufWriter.html #[cfg(feature="std")] pub fn write_nicely<W>(&self, tab: Option<u8>, stream: &mut W) -> IoResult<()> where W: Write { let tab = match tab { Some(_) => tab, None => Some(formatter::DEFAULT_TAB_WIDTH), }; Formatter::new(tab).format(self, stream) } } impl<T> From<Option<T>> for Value where T: Into<Value> { fn from(t: Option<T>) -> Self { match t { Some(t) => t.into(), None => Value::Null, } } } #[cfg(feature="std")] impl FromStr for Value { type Err = Error; fn from_str(s: &str) -> Result<Self, Self::Err> { crate::parse_bytes(s) } } #[cfg(feature="std")] impl TryFrom<Vec<u8>> for Value { type Error = Error; fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> { crate::parse_bytes(bytes) } } /// # Makes new object pub fn object() -> Value { Value::Object(Object::new()) } /// # Makes new array pub fn array() -> Value { Value::Array(Vec::new()) } /// # Makes new array with capacity pub fn array_with_capacity(capacity: usize) -> Value { Value::Array(Vec::with_capacity(capacity)) } /// # Pushes new item into an array pub fn push<T>(array: &mut Array, value: T) where T: Into<Value> { array.push(value.into()); } /// # Inserts new item into an object /// /// Returns previous value (if it existed). pub fn insert<K, V>(object: &mut Object, key: K, value: V) -> Option<Value> where K: Into<ObjectKey>, V: Into<Value> { object.insert(key.into(), value.into()) }