json/lib.rs
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
//! Json parser
//!
//! # Example
//! ```
//! use json::Json;
//!
//! let j = Json::deserialize(r#"{
//! "array" : [ 1, 2, "3", null ],
//! "true" : true,
//! "nested" : {
//! "inner" : []
//! }
//! }"#).unwrap();
//!
//! let Json::Object(map) = j else { panic!() };
//! assert!(
//! matches!(
//! map.get("true"),
//! Some(Json::True)));
//! ```
#![warn(clippy::pedantic)]
#![allow(
clippy::missing_errors_doc,
clippy::must_use_candidate
)]
#![cfg_attr(not(feature = "std"), no_std)]
#[macro_use]
extern crate alloc;
mod prelude {
pub use alloc::string::ToString;
pub use core::fmt::{self,Display,Write};
pub use alloc::vec::Vec;
pub use alloc::borrow::Cow;
pub use alloc::boxed::Box;
#[cfg(feature = "std")]
pub type Map<K,V> = std::collections::HashMap<K,V>;
#[cfg(not(feature = "std"))]
pub type Map<K,V> = alloc::collections::BTreeMap<K,V>;
}
use prelude::*;
mod lexer;
mod parser;
#[cfg(feature = "bindings")]
pub mod export;
mod error;
type Result<T> = core::result::Result<T,error::Error>;
/// Represents a JSON object
#[derive(Debug,PartialEq)]
pub enum Json {
Array(Box<[Json]>),
Object(Map<Box<str>,Json>),
String(Box<str>),
Number(f64),
True, False, Null,
}
/// Configures the JSON parser
#[repr(C)]
pub struct JsonConfig {
/// Max depth for nested objects
pub max_depth: u32,
/// Recover from errors.
/// For example, trailing commas on objects
/// are not allowed, but this flag makes
/// the parser skip them.
pub recover_from_errors: bool,
}
/// Default config used by [`Json::deserialize`]
const DEFAULT_CONFIG: JsonConfig = JsonConfig {
max_depth: u32::MAX,
recover_from_errors: false,
};
impl Default for JsonConfig {
fn default() -> Self { DEFAULT_CONFIG }
}
macro_rules! deserialize {
($text:ident, $conf:ident) => {
{
let txt = $text.as_ref();
let mut tokens = lexer::tokenize(txt)?;
parser::parse(txt, &mut tokens, $conf)
}
};
}
impl Json {
/// Deserializes the given string into a [Json] object
///
/// ## Configuration used
/// [`max_depth`](JsonConfig::max_depth) = [`u32::MAX`]
///
/// [`recover_from_errors`](JsonConfig::recover_from_errors) = false
pub fn deserialize(text: impl AsRef<str>) -> Result<Json> {
deserialize!(text, DEFAULT_CONFIG)
}
/// Deserializes the given string into a [Json] object
/// using the given [`JsonConfig`]
pub fn deserialize_with_config(text: impl AsRef<str>, conf: JsonConfig) -> Result<Json> {
deserialize!(text, conf)
}
/// Serializes the JSON object into a `fmt::Write`
pub fn serialize(&self, out: &mut dyn Write) -> core::fmt::Result {
match self {
Json::Array(elements) => {
out.write_char('[')?;
for i in 0..elements.len() {
elements[i].serialize(out)?;
if i < elements.len() -1 {
out.write_char(',')?;
}
}
out.write_char(']')?;
},
Json::Object(obj) => {
out.write_char('{')?;
let mut first = true;
for (k,v) in obj {
if !first {
out.write_char(',')?;
}
first = false;
write!(out, "\"{k}\":")?;
v.serialize(out)?;
}
out.write_char('}')?;
},
Json::String(s) => { write!(out, "\"{s}\"")?; },
Json::Number(n) => { write!(out, "{n}")?; },
Json::True => { out.write_str("true")? },
Json::False => { out.write_str("false")? },
Json::Null => { out.write_str("null")? },
}
Ok(())
}
/// Attempts to get a value of the given json object.
/// If the json enum is not an Object variant, or if
/// it doesn't contain the key, returns None
pub fn get(&self, key: impl AsRef<str>) -> Option<&Json> {
if let Json::Object(o) = self {
o.get(key.as_ref())
} else {
None
}
}
/// Same as [get](Self::get), but with a mutable reference
pub fn get_mut(&mut self, key: impl AsRef<str>) -> Option<&mut Json> {
if let Json::Object(o) = self {
o.get_mut(key.as_ref())
} else {
None
}
}
/// Attempts to get a value of the given json array.
/// If the json enum is not an Array variant, or if
/// it doesn't contain the key, returns None
pub fn nth(&self, i: usize) -> Option<&Json> {
if let Json::Array(arr) = self {
arr.get(i)
} else {
None
}
}
/// Same as [nth](Self::nth), but with a mutable reference
pub fn nth_mut(&mut self, i: usize) -> Option<&mut Json> {
if let Json::Array(arr) = self {
arr.get_mut(i)
} else {
None
}
}
/// Attempts to get the inner f64 of the json object, if
/// it is a Number variant
pub fn number(&self) -> Option<f64> {
if let Json::Number(n) = self {
Some(*n)
} else { None }
}
/// Attempts to get the inner String of the json object, if
/// it is a String variant
pub fn string(&self) -> Option<&str> {
if let Json::String(s) = self {
Some(s)
} else { None }
}
/// Attempts to get the inner Object of the json object, if
/// it is an Object variant
pub fn object(&self) -> Option<&Map<Box<str>,Json>> {
if let Json::Object(o) = self {
Some(o)
} else { None }
}
/// Attempts to get the inner Array of the json object, if
/// it is an Array variant
pub fn array(&self) -> Option<&[Json]> {
if let Json::Array(o) = self {
Some(o)
} else { None }
}
/// Attempts to get the inner boolean value of the json object, if
/// it is a True or False variant
pub fn boolean(&self) -> Option<bool> {
if let Json::True = self {
Some(true)
} else if let Json::False = self {
Some(false)
} else { None }
}
/// Returns true if the json is a Nil variant
pub fn is_null(&self) -> bool {
matches!(self,Json::Null)
}
}
impl Display for Json {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.serialize(f)
}
}
macro_rules! from_num {
( $( $nty:ty ),* ) => {
$(
impl From<$nty> for Json {
fn from(value: $nty) -> Self {
Self::Number(value.into())
}
}
)*
};
}
from_num!(f64,f32,i32,i16,u16,u8);
impl From<Box<str>> for Json {
fn from(value: Box<str>) -> Self {
Self::String(value)
}
}
impl<'a> From<&'a str> for Json {
fn from(value: &'a str) -> Self {
Self::String(value.into())
}
}
impl From<Vec<Json>> for Json {
fn from(value: Vec<Json>) -> Self {
Self::Array(value.into())
}
}
impl From<Map<Box<str>,Json>> for Json {
fn from(value: Map<Box<str>,Json>) -> Self {
Self::Object(value)
}
}
impl From<bool> for Json {
fn from(value: bool) -> Self {
if value { Json::True } else { Json::False }
}
}
#[doc(hidden)]
pub use prelude::Map;
/// Builds a [Json] object
///
/// # Example
/// ```
/// use json::json;
///
/// let j = json!({
/// "hello" : ["w", 0, "r", "ld"],
/// "array" : [
/// { "key" : "val" },
/// 12.21,
/// null,
/// true,
/// false
/// ]
/// });
/// ```
#[macro_export]
macro_rules! json {
( $lit:literal ) => {
$crate::Json::from( $lit )
};
( [ $( $e:tt ),* ] ) => {
$crate::Json::from(
vec![
$(
json!($e)
),*
]
)
};
( { $( $key:literal : $val:tt ),* } ) => {
{
let mut map = $crate::Map::new();
$( map.insert($key .into(), json!($val) ); )*
$crate::Json::from ( map )
}
};
( null ) => {
$crate::Json::Null
}
}