core_json/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![doc = include_str!("../README.md")]
3#![deny(missing_docs)]
4#![no_std]
5
6#[cfg(feature = "alloc")]
7extern crate alloc;
8
9mod io;
10mod stack;
11mod string;
12mod number;
13
14pub(crate) use io::*;
15pub use io::BytesLike;
16pub use stack::*;
17use string::*;
18pub use string::UnescapeString;
19
20/// An error incurred when deserializing.
21#[derive(Debug)]
22pub enum JsonError<'bytes, B: BytesLike<'bytes>, S: Stack> {
23  /// An unexpected state was reached during deserialization.
24  InternalError,
25  /// An error from the bytes.
26  BytesError(B::Error),
27  /// An error from the stack.
28  StackError(S::Error),
29  /// The deserializer was reused.
30  ReusedDeserializer,
31  /// The JSON had an invalid key.
32  InvalidKey,
33  /// The JSON had an invalid delimiter between the key and value (`:` expected).
34  InvalidKeyValueDelimiter,
35  /// The JSON had an invalid value.
36  InvalidValue,
37  /// The JSON had a trailing comma.
38  TrailingComma,
39  /// The JSON had mismatched delimiters between the open and close of the structure.
40  MismatchedDelimiter,
41  /// Operation could not be performed given the value's type.
42  TypeError,
43}
44impl<'bytes, B: BytesLike<'bytes>, S: Stack> Clone for JsonError<'bytes, B, S> {
45  #[inline(always)]
46  fn clone(&self) -> Self {
47    *self
48  }
49}
50impl<'bytes, B: BytesLike<'bytes>, S: Stack> Copy for JsonError<'bytes, B, S> {}
51
52/// Interpret the immediate value within the bytes as a `bool`.
53#[inline(always)]
54pub fn as_bool<'bytes, B: BytesLike<'bytes>, S: Stack>(
55  bytes: &B,
56) -> Result<bool, JsonError<'bytes, B, S>> {
57  let first = bytes.peek(0).ok();
58  let second = bytes.peek(1).ok();
59  let third = bytes.peek(2).ok();
60  let fourth = bytes.peek(3).ok();
61  let fifth = bytes.peek(4).ok();
62
63  let is_true = (first, second, third, fourth) == (Some(b't'), Some(b'r'), Some(b'u'), Some(b'e'));
64  let is_false = (first, second, third, fourth, fifth) ==
65    (Some(b'f'), Some(b'a'), Some(b'l'), Some(b's'), Some(b'e'));
66
67  if !(is_true | is_false) {
68    Err(JsonError::TypeError)?;
69  }
70
71  Ok(is_true)
72}
73
74/// Check if the immediate value within the bytes is `null`.
75#[inline(always)]
76pub fn is_null<'bytes, B: BytesLike<'bytes>, S: Stack>(
77  bytes: &B,
78) -> Result<bool, JsonError<'bytes, B, S>> {
79  let first = bytes.peek(0).ok();
80  let second = bytes.peek(1).ok();
81  let third = bytes.peek(2).ok();
82  let fourth = bytes.peek(3).ok();
83
84  Ok((first, second, third, fourth) == (Some(b'n'), Some(b'u'), Some(b'l'), Some(b'l')))
85}
86
87/// Advance the bytes until there's a non-whitespace character.
88#[inline(always)]
89fn advance_whitespace<'bytes, B: BytesLike<'bytes>, S: Stack>(
90  bytes: &mut B,
91) -> Result<(), JsonError<'bytes, B, S>> {
92  loop {
93    let next = bytes.peek(0).map_err(JsonError::BytesError)?;
94    // https://datatracker.ietf.org/doc/html/rfc8259#section-2 defines whitespace as follows
95    if !matches!(next, b'\x20' | b'\x09' | b'\x0A' | b'\x0D') {
96      break;
97    }
98    bytes.advance(1).map_err(JsonError::BytesError)?;
99  }
100  Ok(())
101}
102
103/// Advance past a comma, or to the close of the structure.
104fn advance_past_comma_or_to_close<'bytes, B: BytesLike<'bytes>, S: Stack>(
105  bytes: &mut B,
106) -> Result<(), JsonError<'bytes, B, S>> {
107  advance_whitespace(bytes)?;
108  match bytes.peek(0).map_err(JsonError::BytesError)? {
109    b',' => {
110      bytes.advance(1).map_err(JsonError::BytesError)?;
111      advance_whitespace(bytes)?;
112      if matches!(bytes.peek(0).map_err(JsonError::BytesError)?, b']' | b'}') {
113        Err(JsonError::TrailingComma)?;
114      }
115    }
116    b']' | b'}' => {}
117    _ => Err(JsonError::InvalidValue)?,
118  }
119  Ok(())
120}
121
122/// The result from a single step of the deserialized, if within an object.
123enum SingleStepObjectResult<'bytes, B: BytesLike<'bytes>> {
124  /// A field within the object was advanced to.
125  Field {
126    /// The key for this field.
127    key: String<'bytes, B>,
128  },
129  /// The object was closed.
130  Closed,
131}
132
133/// The result from a single step of the deserialized, if within an array.
134enum SingleStepArrayResult {
135  /// A value within the array was advanced to.
136  Value,
137  /// The array was closed.
138  Closed,
139}
140
141/// The result from a single step of the deserializer, if handling an unknown value.
142enum SingleStepUnknownResult<'bytes, B: BytesLike<'bytes>> {
143  /// An object was opened.
144  ObjectOpened,
145  /// An array was opened.
146  ArrayOpened,
147  /// A string was read.
148  String(String<'bytes, B>),
149  /// A unit value was advanced past.
150  Advanced,
151}
152
153/// The result from a single step of the deserializer.
154enum SingleStepResult<'bytes, B: BytesLike<'bytes>> {
155  /// The result if within an object.
156  Object(SingleStepObjectResult<'bytes, B>),
157  /// The result if within an array.
158  Array(SingleStepArrayResult),
159  /// The result if handling an unknown value.
160  Unknown(SingleStepUnknownResult<'bytes, B>),
161}
162
163/// Step the deserializer forwards.
164///
165/// This assumes there is no leading whitespace present in `bytes` and will advance past any
166/// whitespace present before the next logical unit.
167fn single_step<'bytes, 'parent, B: BytesLike<'bytes>, S: Stack>(
168  bytes: &'parent mut B,
169  stack: &'parent mut S,
170) -> Result<SingleStepResult<'bytes, B>, JsonError<'bytes, B, S>> {
171  match stack.peek().ok_or(JsonError::InternalError)? {
172    State::Object => {
173      let next = bytes.read_byte().map_err(JsonError::BytesError)?;
174
175      // Check if the object terminates
176      if next == b'}' {
177        stack.pop().ok_or(JsonError::InternalError)?;
178
179        // If this isn't the outer object, advance past the comma after
180        if stack.depth() != 0 {
181          advance_past_comma_or_to_close(bytes)?;
182        }
183
184        return Ok(SingleStepResult::Object(SingleStepObjectResult::Closed));
185      }
186
187      // Read the name of this field
188      if next != b'"' {
189        Err(JsonError::InvalidKey)?;
190      }
191      let key = read_string(bytes)?;
192
193      // Read the colon delimiter
194      advance_whitespace::<_, S>(bytes)?;
195      if bytes.read_byte().map_err(JsonError::BytesError)? != b':' {
196        Err(JsonError::InvalidKeyValueDelimiter)?;
197      }
198
199      // Push how we're reading a value of an unknown type onto the stack
200      advance_whitespace::<_, S>(bytes)?;
201      stack.push(State::Unknown).map_err(JsonError::StackError)?;
202      Ok(SingleStepResult::Object(SingleStepObjectResult::Field { key }))
203    }
204    State::Array => {
205      // Check if the array terminates
206      if bytes.peek(0).map_err(JsonError::BytesError)? == b']' {
207        stack.pop().ok_or(JsonError::InternalError)?;
208        bytes.advance(1).map_err(JsonError::BytesError)?;
209
210        // If this isn't the outer object, advance past the comma after
211        if stack.depth() != 0 {
212          advance_past_comma_or_to_close(bytes)?;
213        }
214
215        return Ok(SingleStepResult::Array(SingleStepArrayResult::Closed));
216      }
217
218      // Since the array doesn't terminate, read the next value
219      stack.push(State::Unknown).map_err(JsonError::StackError)?;
220      Ok(SingleStepResult::Array(SingleStepArrayResult::Value))
221    }
222    State::Unknown => {
223      stack.pop().ok_or(JsonError::InternalError)?;
224
225      let mut result = SingleStepResult::Unknown(SingleStepUnknownResult::Advanced);
226      match bytes.peek(0).map_err(JsonError::BytesError)? {
227        // Handle if this opens an object
228        b'{' => {
229          bytes.advance(1).map_err(JsonError::BytesError)?;
230          advance_whitespace(bytes)?;
231          stack.push(State::Object).map_err(JsonError::StackError)?;
232          return Ok(SingleStepResult::Unknown(SingleStepUnknownResult::ObjectOpened));
233        }
234        // Handle if this opens an array
235        b'[' => {
236          bytes.advance(1).map_err(JsonError::BytesError)?;
237          advance_whitespace(bytes)?;
238          stack.push(State::Array).map_err(JsonError::StackError)?;
239          return Ok(SingleStepResult::Unknown(SingleStepUnknownResult::ArrayOpened));
240        }
241        // Handle if this opens an string
242        b'"' => {
243          bytes.advance(1).map_err(JsonError::BytesError)?;
244          // Read past the string
245          result = SingleStepResult::Unknown(SingleStepUnknownResult::String(read_string(bytes)?));
246        }
247        // This is a distinct unit value
248        _ => {
249          // https://datatracker.ietf.org/doc/html/rfc8259#section-3 defines all possible values
250          let is_number = match number::as_number(bytes) {
251            Ok((len, _)) => Some(len),
252            Err(JsonError::TypeError) => None,
253            Err(e) => Err(e)?,
254          };
255          let is_bool = match as_bool(bytes) {
256            Ok(value) => Some(if value { 4 } else { 5 }),
257            Err(JsonError::TypeError) => None,
258            Err(e) => Err(e)?,
259          };
260          let is_null = match is_null(bytes) {
261            Ok(is_null) => {
262              if is_null {
263                Some(4)
264              } else {
265                None
266              }
267            }
268            Err(e) => Err(e)?,
269          };
270
271          if let Some(len) = is_number.or(is_bool).or(is_null) {
272            bytes.advance(len).map_err(JsonError::BytesError)?;
273          } else {
274            Err(JsonError::InvalidValue)?;
275          }
276        }
277      }
278
279      // We now have to read past the next comma, or to the next closing of a structure
280      advance_past_comma_or_to_close(bytes)?;
281
282      Ok(result)
283    }
284  }
285}
286
287/// A deserializer for a JSON-encoded structure.
288pub struct Deserializer<'bytes, B: BytesLike<'bytes>, S: Stack> {
289  bytes: B,
290  stack: S,
291  /*
292    We advance the deserializer within `Drop` which cannot return an error. If an error is raised
293    within drop, we store it here to be consumed upon the next call to a method which can return an
294    error (if one is ever called).
295  */
296  error: Option<JsonError<'bytes, B, S>>,
297}
298
299/// A JSON value.
300// Internally, we assume whenever this is held, the top item on the stack is `State::Unknown`
301pub struct Value<'bytes, 'parent, B: BytesLike<'bytes>, S: Stack> {
302  deserializer: Option<&'parent mut Deserializer<'bytes, B, S>>,
303}
304
305impl<'bytes, 'parent, B: BytesLike<'bytes>, S: Stack> Drop for Value<'bytes, 'parent, B, S> {
306  fn drop(&mut self) {
307    /*
308      When this value is dropped, we advance the deserializer past it if it hasn't already been
309      converted into a `FieldIterator` or `ArrayIterator` (which each have their own `Drop`
310      implementations).
311    */
312    if let Some(deserializer) = self.deserializer.take() {
313      if deserializer.error.is_some() {
314        return;
315      }
316
317      let Some(current) = deserializer.stack.peek() else {
318        deserializer.error = Some(JsonError::InternalError);
319        return;
320      };
321
322      let mut depth = match current {
323        State::Object | State::Array => 1,
324        State::Unknown => {
325          let step = match single_step(&mut deserializer.bytes, &mut deserializer.stack) {
326            Ok(SingleStepResult::Unknown(step)) => step,
327            Ok(_) => {
328              deserializer.error = Some(JsonError::InternalError);
329              return;
330            }
331            Err(e) => {
332              deserializer.error = Some(e);
333              return;
334            }
335          };
336          match step {
337            // We successfully advanced past this item
338            SingleStepUnknownResult::String(_) | SingleStepUnknownResult::Advanced => return,
339            // We opened an object/array we now have to advance past
340            SingleStepUnknownResult::ObjectOpened | SingleStepUnknownResult::ArrayOpened => 1,
341          }
342        }
343      };
344
345      // Since our object isn't a unit, step the deserializer until it's advanced past
346      while depth != 0 {
347        let step = match single_step(&mut deserializer.bytes, &mut deserializer.stack) {
348          Ok(step) => step,
349          Err(e) => {
350            deserializer.error = Some(e);
351            return;
352          }
353        };
354        match step {
355          SingleStepResult::Object(SingleStepObjectResult::Closed) |
356          SingleStepResult::Array(SingleStepArrayResult::Closed) => depth -= 1,
357          SingleStepResult::Unknown(
358            SingleStepUnknownResult::ObjectOpened | SingleStepUnknownResult::ArrayOpened,
359          ) => depth += 1,
360          _ => {}
361        }
362      }
363    }
364  }
365}
366
367impl<'bytes, B: BytesLike<'bytes>, S: Stack> Deserializer<'bytes, B, S> {
368  /// Create a new deserializer.
369  pub fn new(mut bytes: B) -> Result<Self, JsonError<'bytes, B, S>> {
370    advance_whitespace(&mut bytes)?;
371
372    let mut stack = S::empty();
373    stack.push(State::Unknown).map_err(JsonError::StackError)?;
374
375    Ok(Deserializer { bytes, stack, error: None })
376  }
377
378  /// Obtain the `Value` representing the serialized structure.
379  ///
380  /// This takes a mutable reference as `Deserializer` is the owned object representing the
381  /// deserializer's state. However, this is not eligible to be called more than once, even after
382  /// the initial mutable borrow is dropped. Multiple calls to this function will cause an error to
383  /// be returned.
384  #[inline(always)]
385  pub fn value(&mut self) -> Result<Value<'bytes, '_, B, S>, JsonError<'bytes, B, S>> {
386    if self.stack.depth() != 1 {
387      Err(JsonError::ReusedDeserializer)?;
388    }
389    let result = Value { deserializer: Some(self) };
390    if !(result.is_object()? || result.is_array()?) {
391      Err(JsonError::TypeError)?;
392    }
393    Ok(result)
394  }
395}
396
397/// An iterator over fields.
398pub struct FieldIterator<'bytes, 'parent, B: BytesLike<'bytes>, S: Stack> {
399  deserializer: &'parent mut Deserializer<'bytes, B, S>,
400  done: bool,
401}
402
403// When this object is dropped, advance the decoder past the unread items
404impl<'bytes, 'parent, B: BytesLike<'bytes>, S: Stack> Drop
405  for FieldIterator<'bytes, 'parent, B, S>
406{
407  #[inline(always)]
408  fn drop(&mut self) {
409    if self.deserializer.error.is_some() {
410      return;
411    }
412
413    loop {
414      let Some(next) = self.next() else { break };
415      let next = next.map(|_| ());
416      match next {
417        Ok(()) => {}
418        Err(e) => {
419          self.deserializer.error = Some(e);
420          break;
421        }
422      }
423    }
424  }
425}
426
427#[rustfmt::skip]
428impl<'bytes, 'parent, B: BytesLike<'bytes>, S: Stack>
429  FieldIterator<'bytes, 'parent, B, S>
430{
431  /// The next entry (key, value) within the object.
432  ///
433  /// This is approximate to `Iterator::next` yet each item maintains a mutable reference to the
434  /// iterator. Accordingly, we cannot use `Iterator::next` which requires items not borrow from
435  /// the iterator.
436  ///
437  /// [polonius-the-crab](https://docs.rs/polonius-the-crab) details a frequent limitation of
438  /// Rust's borrow checker which users of this function may incur. It also details potential
439  /// solutions (primarily using inlined code instead of functions, callbacks) before presenting
440  /// itself as a complete solution. Please refer to it if you have difficulties calling this
441  /// method for context.
442  #[allow(clippy::type_complexity, clippy::should_implement_trait)]
443  pub fn next(
444    &mut self,
445  ) -> Option<Result<(String<'bytes, B>, Value<'bytes, '_, B, S>), JsonError<'bytes, B, S>>>
446  {
447    if let Some(err) = self.deserializer.error {
448      return Some(Err(err));
449    }
450
451    if self.done {
452      None?;
453    }
454
455    loop {
456      let result = match single_step(&mut self.deserializer.bytes, &mut self.deserializer.stack) {
457        Ok(SingleStepResult::Object(result)) => result,
458        Ok(_) => break Some(Err(JsonError::InternalError)),
459        Err(e) => break Some(Err(e)),
460      };
461      match result {
462        SingleStepObjectResult::Field { key } => {
463          break Some(Ok((key, Value { deserializer: Some(self.deserializer) })))
464        }
465        SingleStepObjectResult::Closed => {
466          self.done = true;
467          None?
468        }
469      }
470    }
471  }
472}
473
474/// An iterator over an array.
475pub struct ArrayIterator<'bytes, 'parent, B: BytesLike<'bytes>, S: Stack> {
476  deserializer: &'parent mut Deserializer<'bytes, B, S>,
477  done: bool,
478}
479
480// When this array is dropped, advance the decoder past the unread items
481impl<'bytes, 'parent, B: BytesLike<'bytes>, S: Stack> Drop
482  for ArrayIterator<'bytes, 'parent, B, S>
483{
484  #[inline(always)]
485  fn drop(&mut self) {
486    if self.deserializer.error.is_some() {
487      return;
488    }
489
490    loop {
491      let Some(next) = self.next() else { break };
492      let next = next.map(|_| ());
493      match next {
494        Ok(()) => {}
495        Err(e) => {
496          self.deserializer.error = Some(e);
497          break;
498        }
499      }
500    }
501  }
502}
503
504impl<'bytes, 'parent, B: BytesLike<'bytes>, S: Stack> ArrayIterator<'bytes, 'parent, B, S> {
505  /// The next item within the array.
506  ///
507  /// This is approximate to `Iterator::next` yet each item maintains a mutable reference to the
508  /// iterator. Accordingly, we cannot use `Iterator::next` which requires items not borrow from
509  /// the iterator.
510  ///
511  /// [polonius-the-crab](https://docs.rs/polonius-the-crab) details a frequent limitation of
512  /// Rust's borrow checker which users of this function may incur. It also details potential
513  /// solutions (primarily using inlined code instead of functions, callbacks) before presenting
514  /// itself as a complete solution. Please refer to it if you have difficulties calling this
515  /// method for context.
516  #[allow(clippy::should_implement_trait)]
517  pub fn next(&mut self) -> Option<Result<Value<'bytes, '_, B, S>, JsonError<'bytes, B, S>>> {
518    if let Some(err) = self.deserializer.error {
519      return Some(Err(err));
520    }
521
522    if self.done {
523      None?;
524    }
525
526    loop {
527      let result = match single_step(&mut self.deserializer.bytes, &mut self.deserializer.stack) {
528        Ok(SingleStepResult::Array(result)) => result,
529        Ok(_) => break Some(Err(JsonError::InternalError)),
530        Err(e) => break Some(Err(e)),
531      };
532      match result {
533        SingleStepArrayResult::Value => {
534          break Some(Ok(Value { deserializer: Some(self.deserializer) }))
535        }
536        SingleStepArrayResult::Closed => {
537          self.done = true;
538          None?
539        }
540      }
541    }
542  }
543}
544
545impl<'bytes, 'parent, B: BytesLike<'bytes>, S: Stack> Value<'bytes, 'parent, B, S> {
546  /// Check if the current item is an object.
547  #[inline(always)]
548  pub fn is_object(&self) -> Result<bool, JsonError<'bytes, B, S>> {
549    Ok(
550      self
551        .deserializer
552        .as_ref()
553        .ok_or(JsonError::InternalError)?
554        .bytes
555        .peek(0)
556        .map_err(JsonError::BytesError)? ==
557        b'{',
558    )
559  }
560
561  /// Iterate over the fields within this object.
562  ///
563  /// If a field is present multiple times, this will yield each instance.
564  pub fn fields(mut self) -> Result<FieldIterator<'bytes, 'parent, B, S>, JsonError<'bytes, B, S>> {
565    let deserializer = self.deserializer.take().ok_or(JsonError::InternalError)?;
566    if let Some(err) = deserializer.error {
567      Err(err)?;
568    }
569
570    match single_step(&mut deserializer.bytes, &mut deserializer.stack)? {
571      SingleStepResult::Unknown(SingleStepUnknownResult::ObjectOpened) => {
572        Ok(FieldIterator { deserializer, done: false })
573      }
574      _ => Err(JsonError::TypeError),
575    }
576  }
577
578  /// Check if the current item is an array.
579  #[inline(always)]
580  pub fn is_array(&self) -> Result<bool, JsonError<'bytes, B, S>> {
581    Ok(
582      self
583        .deserializer
584        .as_ref()
585        .ok_or(JsonError::InternalError)?
586        .bytes
587        .peek(0)
588        .map_err(JsonError::BytesError)? ==
589        b'[',
590    )
591  }
592
593  /// Iterate over all items within this container.
594  pub fn iterate(
595    mut self,
596  ) -> Result<ArrayIterator<'bytes, 'parent, B, S>, JsonError<'bytes, B, S>> {
597    let deserializer = self.deserializer.take().ok_or(JsonError::InternalError)?;
598    if let Some(err) = deserializer.error {
599      Err(err)?;
600    }
601
602    match single_step(&mut deserializer.bytes, &mut deserializer.stack)? {
603      SingleStepResult::Unknown(SingleStepUnknownResult::ArrayOpened) => {
604        Ok(ArrayIterator { deserializer, done: false })
605      }
606      _ => Err(JsonError::TypeError),
607    }
608  }
609
610  /// Check if the current item is a string.
611  #[inline(always)]
612  pub fn is_str(&self) -> Result<bool, JsonError<'bytes, B, S>> {
613    Ok(
614      self
615        .deserializer
616        .as_ref()
617        .ok_or(JsonError::InternalError)?
618        .bytes
619        .peek(0)
620        .map_err(JsonError::BytesError)? ==
621        b'"',
622    )
623  }
624
625  /// Get the current item as a 'string' (represented as a `B`).
626  ///
627  /// This will NOT unescape the string in any way, returning a view of the bytes underlying the
628  /// serialization. If you want the actual string represented, please pass this to
629  /// [`UnescapeString`] which will yield an iterator for the serialized `char`s.
630  #[inline(always)]
631  pub fn to_str(mut self) -> Result<String<'bytes, B>, JsonError<'bytes, B, S>> {
632    let deserializer = self.deserializer.take().ok_or(JsonError::InternalError)?;
633    match single_step(&mut deserializer.bytes, &mut deserializer.stack)? {
634      SingleStepResult::Unknown(SingleStepUnknownResult::String(str)) => Ok(str),
635      _ => Err(JsonError::TypeError),
636    }
637  }
638
639  /// Get the current item as an `i64`.
640  ///
641  /// This uses the definition of a number defined in RFC 8259, then constrains it to having no
642  /// fractional, exponent parts. Then, it's yielded if it's representable within an `i64`.
643  ///
644  /// This is _exact_. It does not go through `f64` and does not experience its approximations.
645  #[inline(always)]
646  pub fn as_i64(&self) -> Result<i64, JsonError<'bytes, B, S>> {
647    let bytes = &self.deserializer.as_ref().ok_or(JsonError::InternalError)?.bytes;
648
649    let (i, str) = number::as_number_str(bytes)?;
650    let str = core::str::from_utf8(&str[.. i]).map_err(|_| JsonError::InternalError)?;
651    if str.contains('.') || str.contains('e') || str.contains('E') {
652      Err(JsonError::TypeError)?;
653    }
654    <i64 as core::str::FromStr>::from_str(str).map_err(|_| JsonError::TypeError)
655  }
656
657  /// Get the current item as an `f64`.
658  #[inline(always)]
659  pub fn as_f64(&self) -> Result<f64, JsonError<'bytes, B, S>> {
660    Ok(number::as_number(&self.deserializer.as_ref().ok_or(JsonError::InternalError)?.bytes)?.1)
661  }
662
663  /// Get the current item as a `bool`.
664  #[inline(always)]
665  pub fn as_bool(&self) -> Result<bool, JsonError<'bytes, B, S>> {
666    as_bool(&self.deserializer.as_ref().ok_or(JsonError::InternalError)?.bytes)
667  }
668
669  /// Check if the current item is `null`.
670  #[inline(always)]
671  pub fn is_null(&self) -> Result<bool, JsonError<'bytes, B, S>> {
672    is_null(&self.deserializer.as_ref().ok_or(JsonError::InternalError)?.bytes)
673  }
674}