serde_json_bytes/
lib.rs

1#[cfg(not(feature = "std"))]
2extern crate alloc;
3
4/// A facade around all the types we need from the `std`, `core`, and `alloc`
5/// crates. This avoids elaborate import wrangling having to happen in every
6/// module.
7mod lib {
8    mod core {
9        #[cfg(not(feature = "std"))]
10        pub use core::*;
11        #[cfg(feature = "std")]
12        pub use std::*;
13    }
14
15    pub use self::core::cell::{Cell, RefCell};
16    pub use self::core::clone::{self, Clone};
17    pub use self::core::convert::{self, From, Into};
18    pub use self::core::default::{self, Default};
19    pub use self::core::fmt::{self, Debug, Display};
20    pub use self::core::hash::{self, Hash, Hasher};
21    pub use self::core::iter::FusedIterator;
22    pub use self::core::marker::{self, PhantomData};
23    pub use self::core::ops::{Bound, RangeBounds};
24    pub use self::core::result::{self, Result};
25    pub use self::core::{borrow, char, cmp, iter, mem, num, ops, slice, str};
26
27    #[cfg(not(feature = "std"))]
28    pub use alloc::borrow::{Cow, ToOwned};
29    #[cfg(feature = "std")]
30    pub use std::borrow::{Cow, ToOwned};
31
32    #[cfg(not(feature = "std"))]
33    pub use alloc::string::{String, ToString};
34    #[cfg(feature = "std")]
35    pub use std::string::{String, ToString};
36
37    #[cfg(not(feature = "std"))]
38    pub use alloc::vec::{self, Vec};
39    #[cfg(feature = "std")]
40    pub use std::vec::{self, Vec};
41
42    #[cfg(not(feature = "std"))]
43    pub use alloc::boxed::Box;
44    #[cfg(feature = "std")]
45    pub use std::boxed::Box;
46
47    #[cfg(not(feature = "std"))]
48    pub use alloc::collections::{btree_map, BTreeMap};
49    #[cfg(feature = "std")]
50    pub use std::collections::{btree_map, BTreeMap};
51
52    #[cfg(feature = "std")]
53    pub use std::error;
54}
55
56// We only use our own error type; no need for From conversions provided by the
57// standard library's try! macro. This reduces lines of LLVM IR by 4%.
58macro_rules! tri {
59    ($e:expr) => {
60        match $e {
61            crate::lib::Result::Ok(val) => val,
62            crate::lib::Result::Err(err) => return crate::lib::Result::Err(err),
63        }
64    };
65    ($e:expr,) => {
66        tri!($e)
67    };
68}
69
70mod bytestring;
71pub mod map;
72#[cfg(feature = "arbitrary_precision")]
73mod number;
74pub mod value;
75
76pub use bytestring::ByteString;
77pub use map::*;
78pub use value::{from_value, to_value, Value};
79pub mod path;
80
81impl From<serde_json::Value> for Value {
82    fn from(value: serde_json::Value) -> Self {
83        match value {
84            serde_json::Value::Null => Value::Null,
85            serde_json::Value::Bool(b) => Value::Bool(b),
86            serde_json::Value::Number(n) => Value::Number(n),
87            serde_json::Value::String(s) => Value::String(s.into()),
88            serde_json::Value::Array(v) => Value::Array(v.into_iter().map(Into::into).collect()),
89            serde_json::Value::Object(o) => {
90                Value::Object(o.into_iter().map(|(k, v)| (k.into(), v.into())).collect())
91            }
92        }
93    }
94}
95
96pub use serde_json;
97
98#[macro_export]
99macro_rules! json {
100    ($($json:tt)+) => {
101        {
102            let value: $crate::Value = $crate::serde_json::json!($($json)+).into();
103            value
104        }
105    };
106}