1use std::fmt;
16use std::hash::{Hash, Hasher};
17
18use super::*;
19use bytes::Buf;
20use serde_json::Number;
21
22#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
24pub enum ValueRef<'a> {
25 Null,
30 String(StringRef<'a>),
32 Number(NumberRef<'a>),
34 Bool(bool),
36 Array(ArrayRef<'a>),
38 Object(ObjectRef<'a>),
40}
41
42impl<'a> ValueRef<'a> {
43 pub fn from_bytes(bytes: &[u8]) -> ValueRef<'_> {
45 let entry = Entry::from(&bytes[bytes.len() - 4..]);
46 ValueRef::from_slice(bytes, entry)
47 }
48
49 pub fn is_null(self) -> bool {
51 matches!(self, Self::Null)
52 }
53
54 pub fn is_boolean(self) -> bool {
56 matches!(self, Self::Bool(_))
57 }
58
59 pub fn is_number(self) -> bool {
61 matches!(self, Self::Number(_))
62 }
63
64 pub fn is_u64(self) -> bool {
66 matches!(self, Self::Number(n) if n.is_u64())
67 }
68
69 pub fn is_i64(self) -> bool {
71 matches!(self, Self::Number(n) if n.is_i64())
72 }
73
74 pub fn is_f64(self) -> bool {
76 matches!(self, Self::Number(n) if n.is_f64())
77 }
78
79 pub fn is_string(self) -> bool {
81 matches!(self, Self::String(_))
82 }
83
84 pub fn is_array(self) -> bool {
86 matches!(self, Self::Array(_))
87 }
88
89 pub fn is_object(self) -> bool {
91 matches!(self, Self::Object(_))
92 }
93
94 pub fn as_null(self) -> Option<()> {
96 match self {
97 Self::Null => Some(()),
98 _ => None,
99 }
100 }
101
102 pub fn as_bool(self) -> Option<bool> {
104 match self {
105 Self::Bool(b) => Some(b),
106 _ => None,
107 }
108 }
109
110 pub fn as_number(self) -> Option<NumberRef<'a>> {
112 match self {
113 Self::Number(n) => Some(n),
114 _ => None,
115 }
116 }
117
118 pub fn as_u64(self) -> Option<u64> {
120 match self {
121 Self::Number(n) => n.as_u64(),
122 _ => None,
123 }
124 }
125
126 pub fn as_i64(self) -> Option<i64> {
128 match self {
129 Self::Number(n) => n.as_i64(),
130 _ => None,
131 }
132 }
133
134 pub fn as_f64(self) -> Option<f64> {
136 match self {
137 Self::Number(n) => n.as_f64(),
138 _ => None,
139 }
140 }
141
142 pub fn as_str(self) -> Option<&'a str> {
144 match self {
145 Self::String(s) => Some(s.as_str()),
146 _ => None,
147 }
148 }
149
150 pub fn as_array(self) -> Option<ArrayRef<'a>> {
152 match self {
153 Self::Array(a) => Some(a),
154 _ => None,
155 }
156 }
157
158 pub fn as_object(self) -> Option<ObjectRef<'a>> {
160 match self {
161 Self::Object(o) => Some(o),
162 _ => None,
163 }
164 }
165
166 pub fn to_owned(self) -> Value {
168 self.into()
169 }
170
171 pub(crate) fn from_slice(data: &'a [u8], entry: Entry) -> Self {
172 match entry.tag() {
173 Entry::NULL_TAG => Self::Null,
174 Entry::FALSE_TAG => Self::Bool(false),
175 Entry::TRUE_TAG => Self::Bool(true),
176 Entry::NUMBER_TAG => {
177 let ptr = entry.offset();
178 let data = &data[ptr..ptr + 1 + number_size(data[ptr])];
179 Self::Number(NumberRef { data })
180 }
181 Entry::STRING_TAG => {
182 let ptr = entry.offset();
183 let len = (&data[ptr..]).get_u32_ne() as usize;
184 Self::String(StringRef::from_bytes(&data[ptr..ptr + 4 + len]))
185 }
186 Entry::ARRAY_TAG => {
187 let ptr = entry.offset();
188 Self::Array(ArrayRef::from_slice(data, ptr))
189 }
190 Entry::OBJECT_TAG => {
191 let ptr = entry.offset();
192 Self::Object(ObjectRef::from_slice(data, ptr))
193 }
194 _ => panic!("invalid entry"),
195 }
196 }
197
198 pub(crate) fn as_slice(self) -> &'a [u8] {
200 match self {
201 Self::Null => &[],
202 Self::Bool(_) => &[],
203 Self::Number(n) => n.data,
204 Self::String(s) => s.as_slice(),
205 Self::Array(a) => a.as_slice(),
206 Self::Object(o) => o.as_slice(),
207 }
208 }
209
210 pub(crate) fn make_entry(self, offset: usize) -> Entry {
212 match self {
213 Self::Null => Entry::null(),
214 Self::Bool(b) => Entry::bool(b),
215 Self::Number(_) => Entry::number(offset),
216 Self::String(_) => Entry::string(offset),
217 Self::Array(a) => Entry::array(offset + a.as_slice().len()),
218 Self::Object(o) => Entry::object(offset + o.as_slice().len()),
219 }
220 }
221
222 pub fn to_raw_parts(self) -> (Entry, &'a [u8]) {
224 (self.make_entry(0), self.as_slice())
225 }
226
227 pub fn from_raw_parts(entry: Entry, data: &'a [u8]) -> Self {
229 Self::from_slice(data, entry)
230 }
231
232 pub fn capacity(self) -> usize {
234 self.as_slice().len()
235 }
236
237 pub fn get(self, index: impl Index) -> Option<ValueRef<'a>> {
241 index.index_into(self)
242 }
243
244 pub fn pointer(self, pointer: &str) -> Option<Self> {
246 if pointer.is_empty() {
247 return Some(self);
248 }
249 if !pointer.starts_with('/') {
250 return None;
251 }
252
253 fn parse_index(s: &str) -> Option<usize> {
254 if s.starts_with('+') || (s.starts_with('0') && s.len() != 1) {
255 return None;
256 }
257 s.parse().ok()
258 }
259
260 pointer
261 .split('/')
262 .skip(1)
263 .map(|x| x.replace("~1", "/").replace("~0", "~"))
264 .try_fold(self, |target, token| match target {
265 Self::Object(map) => map.get(&token),
266 Self::Array(list) => parse_index(&token).and_then(|x| list.get(x)),
267 _ => None,
268 })
269 }
270}
271
272impl fmt::Debug for ValueRef<'_> {
273 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
274 match self {
275 Self::Null => f.write_str("null"),
276 Self::Bool(b) => b.fmt(f),
277 Self::Number(n) => n.fmt(f),
278 Self::String(s) => s.as_str().fmt(f),
279 Self::Array(a) => a.fmt(f),
280 Self::Object(o) => o.fmt(f),
281 }
282 }
283}
284
285impl fmt::Display for ValueRef<'_> {
287 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
288 serialize_in_json(self, f)
289 }
290}
291
292impl From<ValueRef<'_>> for serde_json::Value {
294 fn from(value: ValueRef<'_>) -> Self {
295 match value {
296 ValueRef::Null => Self::Null,
297 ValueRef::Bool(b) => Self::Bool(b),
298 ValueRef::Number(n) => Self::Number(n.to_number()),
299 ValueRef::String(s) => Self::String(s.as_str().to_owned()),
300 ValueRef::Array(a) => Self::Array(a.iter().map(Self::from).collect()),
301 ValueRef::Object(o) => Self::Object(
302 o.iter()
303 .map(|(k, v)| (k.to_owned(), Self::from(v)))
304 .collect(),
305 ),
306 }
307 }
308}
309
310#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
312pub struct StringRef<'a> {
313 data: &'a [u8],
317}
318
319impl<'a> StringRef<'a> {
320 pub(crate) fn from_bytes(data: &'a [u8]) -> Self {
322 Self { data }
323 }
324
325 pub fn as_str(&self) -> &'a str {
327 let len = (&self.data[..4]).get_u32_ne() as usize;
328 unsafe { std::str::from_utf8_unchecked(&self.data[4..4 + len]) }
330 }
331
332 pub(crate) fn as_slice(&self) -> &'a [u8] {
334 self.data
335 }
336}
337
338#[derive(Clone, Copy)]
340pub struct NumberRef<'a> {
341 data: &'a [u8],
345}
346
347impl NumberRef<'_> {
348 pub fn to_number(self) -> Number {
350 let mut data = self.data;
351 match data.get_u8() {
352 NUMBER_ZERO => Number::from(0),
353 NUMBER_I8 => Number::from(data.get_i8()),
354 NUMBER_I16 => Number::from(data.get_i16_ne()),
355 NUMBER_I32 => Number::from(data.get_i32_ne()),
356 NUMBER_I64 => Number::from(data.get_i64_ne()),
357 NUMBER_U64 => Number::from(data.get_u64_ne()),
358 NUMBER_F64 => Number::from_f64(data.get_f64_ne()).unwrap(),
359 t => panic!("invalid number tag: {t}"),
360 }
361 }
362
363 pub fn as_u64(self) -> Option<u64> {
365 self.to_number().as_u64()
366 }
367
368 pub fn as_i64(self) -> Option<i64> {
370 self.to_number().as_i64()
371 }
372
373 pub fn as_f64(self) -> Option<f64> {
375 self.to_number().as_f64()
376 }
377
378 pub(crate) fn as_f32(&self) -> Option<f32> {
380 let mut data = self.data;
381 Some(match data.get_u8() {
382 NUMBER_ZERO => 0 as f32,
383 NUMBER_I8 => data.get_i8() as f32,
384 NUMBER_I16 => data.get_i16_ne() as f32,
385 NUMBER_I32 => data.get_i32_ne() as f32,
386 NUMBER_I64 => data.get_i64_ne() as f32,
387 NUMBER_U64 => data.get_u64_ne() as f32,
388 NUMBER_F64 => data.get_f64_ne() as f32,
389 t => panic!("invalid number tag: {t}"),
390 })
391 }
392
393 pub fn is_u64(self) -> bool {
395 self.to_number().is_u64()
396 }
397
398 pub fn is_i64(self) -> bool {
400 self.to_number().is_i64()
401 }
402
403 pub fn is_f64(self) -> bool {
405 self.to_number().is_f64()
406 }
407}
408
409impl fmt::Debug for NumberRef<'_> {
410 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
411 self.to_number().fmt(f)
412 }
413}
414
415impl fmt::Display for NumberRef<'_> {
416 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
417 self.to_number().fmt(f)
418 }
419}
420
421impl PartialEq for NumberRef<'_> {
422 fn eq(&self, other: &Self) -> bool {
423 let a = self.to_number();
424 let b = other.to_number();
425 match (a.as_u64(), b.as_u64()) {
426 (Some(a), Some(b)) => return a == b, (Some(_), None) if b.is_i64() => return false, (None, Some(_)) if a.is_i64() => return false, (None, None) => {
430 if let (Some(a), Some(b)) = (a.as_i64(), b.as_i64()) {
431 return a == b; }
433 }
434 _ => {}
435 }
436 let a = a.as_f64().unwrap();
438 let b = b.as_f64().unwrap();
439 a == b
440 }
441}
442
443impl Eq for NumberRef<'_> {}
444
445impl PartialOrd for NumberRef<'_> {
446 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
447 Some(self.cmp(other))
448 }
449}
450
451impl Ord for NumberRef<'_> {
452 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
453 let a = self.to_number();
454 let b = other.to_number();
455 match (a.as_u64(), b.as_u64()) {
456 (Some(a), Some(b)) => return a.cmp(&b), (Some(_), None) if b.is_i64() => return std::cmp::Ordering::Greater, (None, Some(_)) if a.is_i64() => return std::cmp::Ordering::Less, (None, None) => {
460 if let (Some(a), Some(b)) = (a.as_i64(), b.as_i64()) {
461 return a.cmp(&b); }
463 }
464 _ => {}
465 }
466 let a = a.as_f64().unwrap();
468 let b = b.as_f64().unwrap();
469 a.partial_cmp(&b).expect("NaN or Inf in JSON number")
470 }
471}
472
473impl Hash for NumberRef<'_> {
474 fn hash<H: Hasher>(&self, state: &mut H) {
475 self.to_number().hash(state);
476 }
477}
478
479#[derive(Clone, Copy)]
481pub struct ArrayRef<'a> {
482 data: &'a [u8],
488}
489
490impl<'a> ArrayRef<'a> {
491 pub fn get(self, index: usize) -> Option<ValueRef<'a>> {
493 let len = self.len();
494 if index >= len {
495 return None;
496 }
497 let offset = self.data.len() - 8 - 4 * (len - index);
498 let entry = Entry::from(&self.data[offset..offset + 4]);
499 Some(ValueRef::from_slice(self.data, entry))
500 }
501
502 pub fn len(self) -> usize {
504 (&self.data[self.data.len() - 8..]).get_u32_ne() as usize
505 }
506
507 pub fn is_empty(self) -> bool {
509 self.len() == 0
510 }
511
512 pub fn iter(self) -> impl ExactSizeIterator<Item = ValueRef<'a>> {
514 let len = self.len();
515 let offset = self.data.len() - 8 - 4 * len;
516 self.data[offset..offset + 4 * len]
517 .chunks_exact(4)
518 .map(|slice| ValueRef::from_slice(self.data, Entry::from(slice)))
519 }
520
521 pub(crate) fn as_slice(self) -> &'a [u8] {
523 self.data
524 }
525
526 fn from_slice(data: &'a [u8], end: usize) -> Self {
528 let size = (&data[end - 4..end]).get_u32_ne() as usize;
529 Self {
530 data: &data[end - size..end],
531 }
532 }
533}
534
535impl fmt::Debug for ArrayRef<'_> {
536 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
537 f.debug_list().entries(self.iter()).finish()
538 }
539}
540
541impl fmt::Display for ArrayRef<'_> {
543 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
544 serialize_in_json(self, f)
545 }
546}
547
548impl PartialEq for ArrayRef<'_> {
549 fn eq(&self, other: &Self) -> bool {
550 if self.len() != other.len() {
551 return false;
552 }
553 self.iter().eq(other.iter())
554 }
555}
556
557impl Eq for ArrayRef<'_> {}
558
559impl PartialOrd for ArrayRef<'_> {
560 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
561 Some(self.cmp(other))
562 }
563}
564
565impl Ord for ArrayRef<'_> {
566 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
567 match self.len().cmp(&other.len()) {
569 std::cmp::Ordering::Equal => self.iter().cmp(other.iter()),
570 ord => ord,
571 }
572 }
573}
574
575impl Hash for ArrayRef<'_> {
576 fn hash<H: Hasher>(&self, state: &mut H) {
577 for v in self.iter() {
578 v.hash(state);
579 }
580 }
581}
582
583#[derive(Clone, Copy)]
585pub struct ObjectRef<'a> {
586 data: &'a [u8],
594}
595
596impl<'a> ObjectRef<'a> {
597 pub fn get(self, key: &str) -> Option<ValueRef<'a>> {
607 let entries = self.entries();
609 let idx = entries
610 .binary_search_by_key(&key, |&(kentry, _)| {
611 ValueRef::from_slice(self.data, kentry)
612 .as_str()
613 .expect("key must be string")
614 })
615 .ok()?;
616 let (_, ventry) = entries[idx];
617 Some(ValueRef::from_slice(self.data, ventry))
618 }
619
620 pub fn contains_key(self, key: &str) -> bool {
630 let entries = self.entries();
632 entries
633 .binary_search_by_key(&key, |&(kentry, _)| {
634 ValueRef::from_slice(self.data, kentry)
635 .as_str()
636 .expect("key must be string")
637 })
638 .is_ok()
639 }
640
641 pub fn len(self) -> usize {
650 (&self.data[self.data.len() - 8..]).get_u32_ne() as usize
651 }
652
653 pub fn is_empty(self) -> bool {
662 self.len() == 0
663 }
664
665 pub fn iter(self) -> impl ExactSizeIterator<Item = (&'a str, ValueRef<'a>)> {
674 self.entries().iter().map(move |&(kentry, ventry)| {
675 let k = ValueRef::from_slice(self.data, kentry);
676 let v = ValueRef::from_slice(self.data, ventry);
677 (k.as_str().expect("key must be string"), v)
678 })
679 }
680
681 pub fn keys(self) -> impl ExactSizeIterator<Item = &'a str> {
690 self.iter().map(|(k, _)| k)
691 }
692
693 pub fn values(self) -> impl ExactSizeIterator<Item = ValueRef<'a>> {
702 self.iter().map(|(_, v)| v)
703 }
704
705 pub(crate) fn as_slice(self) -> &'a [u8] {
707 self.data
708 }
709
710 fn from_slice(data: &'a [u8], end: usize) -> Self {
712 let size = (&data[end - 4..end]).get_u32_ne() as usize;
713 Self {
714 data: &data[end - size..end],
715 }
716 }
717
718 fn entries(self) -> &'a [(Entry, Entry)] {
720 let len = self.len();
721 let base = self.data.len() - 8 - 8 * len;
722 let slice = &self.data[base..base + 8 * len];
723 unsafe { std::slice::from_raw_parts(slice.as_ptr() as _, len) }
724 }
725}
726
727impl fmt::Debug for ObjectRef<'_> {
728 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
729 f.debug_map().entries(self.iter()).finish()
730 }
731}
732
733impl fmt::Display for ObjectRef<'_> {
735 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
736 serialize_in_json(self, f)
737 }
738}
739
740impl PartialEq for ObjectRef<'_> {
741 fn eq(&self, other: &Self) -> bool {
742 if self.len() != other.len() {
743 return false;
744 }
745 self.iter().eq(other.iter())
746 }
747}
748
749impl Eq for ObjectRef<'_> {}
750
751impl PartialOrd for ObjectRef<'_> {
752 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
753 Some(self.cmp(other))
754 }
755}
756
757impl Ord for ObjectRef<'_> {
758 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
759 match self.len().cmp(&other.len()) {
761 std::cmp::Ordering::Equal => self.iter().cmp(other.iter()),
762 ord => ord,
763 }
764 }
765}
766
767impl Hash for ObjectRef<'_> {
768 fn hash<H: Hasher>(&self, state: &mut H) {
769 for (k, v) in self.iter() {
770 k.hash(state);
771 v.hash(state);
772 }
773 }
774}
775
776fn serialize_in_json(value: &impl ::serde::Serialize, f: &mut fmt::Formatter<'_>) -> fmt::Result {
778 use std::io;
779
780 struct WriterFormatter<'a, 'b: 'a> {
781 inner: &'a mut fmt::Formatter<'b>,
782 }
783
784 impl<'a, 'b> io::Write for WriterFormatter<'a, 'b> {
785 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
786 let s = unsafe { std::str::from_utf8_unchecked(buf) };
789 self.inner.write_str(s).map_err(io_error)?;
790 Ok(buf.len())
791 }
792
793 fn flush(&mut self) -> io::Result<()> {
794 Ok(())
795 }
796 }
797
798 fn io_error(_: fmt::Error) -> io::Error {
799 io::Error::other("fmt error")
802 }
803
804 let alternate = f.alternate();
805 let mut wr = WriterFormatter { inner: f };
806 if alternate {
807 value
809 .serialize(&mut serde_json::Serializer::pretty(&mut wr))
810 .map_err(|_| fmt::Error)
811 } else {
812 value
814 .serialize(&mut serde_json::Serializer::new(&mut wr))
815 .map_err(|_| fmt::Error)
816 }
817}
818
819pub trait Index: private::Sealed {
821 #[doc(hidden)]
823 fn index_into<'v>(&self, v: ValueRef<'v>) -> Option<ValueRef<'v>>;
824}
825
826impl Index for usize {
827 fn index_into<'v>(&self, v: ValueRef<'v>) -> Option<ValueRef<'v>> {
828 match v {
829 ValueRef::Array(a) => a.get(*self),
830 _ => None,
831 }
832 }
833}
834
835impl Index for str {
836 fn index_into<'v>(&self, v: ValueRef<'v>) -> Option<ValueRef<'v>> {
837 match v {
838 ValueRef::Object(o) => o.get(self),
839 _ => None,
840 }
841 }
842}
843
844impl Index for String {
845 fn index_into<'v>(&self, v: ValueRef<'v>) -> Option<ValueRef<'v>> {
846 match v {
847 ValueRef::Object(o) => o.get(self),
848 _ => None,
849 }
850 }
851}
852
853impl<T> Index for &T
854where
855 T: ?Sized + Index,
856{
857 fn index_into<'v>(&self, v: ValueRef<'v>) -> Option<ValueRef<'v>> {
858 (**self).index_into(v)
859 }
860}
861
862mod private {
864 pub trait Sealed {}
865 impl Sealed for usize {}
866 impl Sealed for str {}
867 impl Sealed for String {}
868 impl<T> Sealed for &T where T: ?Sized + Sealed {}
869}