core-json 0.4.0

A non-allocating JSON deserializer
Documentation
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#![cfg_attr(docsrs, feature(doc_cfg))]
#![doc = include_str!("../README.md")]
#![deny(missing_docs)]
#![cfg_attr(not(test), no_std)]

#[cfg(feature = "alloc")]
extern crate alloc;

mod io;
mod stack;
mod string;
mod number;
mod deserializer;

pub use io::Read;
use io::PeekableRead;
pub use stack::*;
use string::*;
pub use number::{NumberSink, Number};
pub use deserializer::{Deserializer, Value};
use deserializer::*;

/// An error incurred when deserializing.
#[derive(Debug)]
pub enum JsonError<'read, R: Read<'read>, S: Stack> {
  /// An unexpected state was reached during deserialization.
  InternalError,
  /// An error from the reader.
  ReadError(R::Error),
  /// An error from the stack.
  StackError(S::Error),
  /// The deserializer was reused.
  ReusedDeserializer,
  /// The JSON had an invalid key.
  InvalidKey,
  /// The JSON had an invalid delimiter between the key and value (`:` expected).
  InvalidKeyValueDelimiter,
  /// The JSON had an invalid value.
  InvalidValue,
  /// The string represented by the JSON serialization was valid yet not UTF-8.
  NotUtf8,
  /// The JSON had a trailing comma.
  TrailingComma,
  /// The JSON had mismatched delimiters between the open and close of the structure.
  MismatchedDelimiter,
  /// Operation could not be performed given the value's type.
  TypeError,
}
impl<'read, R: Read<'read>, S: Stack> Clone for JsonError<'read, R, S> {
  #[inline(always)]
  fn clone(&self) -> Self {
    *self
  }
}
impl<'read, R: Read<'read>, S: Stack> Copy for JsonError<'read, R, S> {}

/// The type of the value.
///
/// https://datatracker.ietf.org/doc/html/rfc8259#section-3 defines all possible values.
pub enum Type {
  /// An object.
  Object,
  /// An array.
  Array,
  /// A string.
  String,
  /// A RFC-8259 number.
  Number,
  /// A boolean.
  Bool,
  /// The `null` unit value.
  Null,
}

/// Get the type of the current item.
///
/// This does not assert it's a valid instance of this class of items. It solely asserts if this
/// is a valid item, it will be of this type.
#[inline(always)]
fn kind<'read, R: Read<'read>>(reader: &PeekableRead<'read, R>) -> Type {
  match reader.peek() {
    b'{' => Type::Object,
    b'[' => Type::Array,
    b'"' => Type::String,
    b't' | b'f' => Type::Bool,
    b'n' => Type::Null,
    _ => Type::Number,
  }
}

/// A field within an object.
pub struct Field<'read, 'parent, R: Read<'read>, S: Stack> {
  key: StringKey<'read, 'parent, R, S>,
}

/// Handle a field.
///
/// This MUST be called every time a `SingleStepObjectResult::Field` is yielded, unless the
/// deserializer is immediately terminated (such as by setting its error to `Some(_)`). This
/// ideally would be yielded within `SingleStepObjectResult::Field`, yet that would cause every
/// `SingleStepObjectResult` to consume the parent's borrow for the rest of its lifetime, when we
/// only want to consume it upon `SingleStepObjectResult::Field`.
#[inline(always)]
fn handle_field<'read, 'parent, R: Read<'read>, S: Stack>(
  deserializer: &'parent mut Deserializer<'read, R, S>,
) -> Field<'read, 'parent, R, S> {
  Field { key: StringKey(Some(String::read(deserializer))) }
}

/// Handle a string value.
///
/// This MUST be called every time a `SingleStepUnknownResult::String` is yielded, unless the
/// deserializer is immediately terminated (such as by setting its error to `Some(_)`). This
/// ideally would be yielded within `SingleStepUnknownResult::String`, yet that would cause every
/// `SingleStepUnknownResult` to consume the parent's borrow for the rest of its lifetime, when we
/// only want to consume it upon `SingleStepUnknownResult::String`.
#[inline(always)]
fn handle_string_value<'read, 'parent, R: Read<'read>, S: Stack>(
  deserializer: &'parent mut Deserializer<'read, R, S>,
) -> StringValue<'read, 'parent, R, S> {
  StringValue(String::read(deserializer))
}

impl<'read, 'parent, R: Read<'read>, S: Stack> Field<'read, 'parent, R, S> {
  /// Access the iterator for the string used as the field's key.
  ///
  /// The iterator will yield the individual characters within the string represented by the JSON
  /// serialization, with all escape sequences handled.
  ///
  /// If the JSON underlying is invalid, the iterator will error, and while `Field::value` may
  /// still be called, all further attempted accesses will yield an error.
  ///
  /// If the JSON underlying is valid yet does not represent a valid UTF-8 sequence, the iterator
  /// will error, yet `Field::value` may still be called and deserialization may continue. The rest
  /// of the key will not be accessible however.
  #[inline(always)]
  pub fn key(
    &mut self,
  ) -> &mut (impl use<'read, 'parent, R, S> + Iterator<Item = Result<char, JsonError<'read, R, S>>>)
  {
    &mut self.key
  }
  /// Access the field's value.
  #[inline(always)]
  pub fn value(mut self) -> Value<'read, 'parent, R, S> {
    Value { deserializer: self.key.drop() }
  }
}

impl<'read, 'parent, R: Read<'read>, S: Stack> Drop for Field<'read, 'parent, R, S> {
  #[inline(always)]
  fn drop(&mut self) {
    drop(Value { deserializer: self.key.drop() });
  }
}

/// An iterator over fields.
pub struct FieldIterator<'read, 'parent, R: Read<'read>, S: Stack> {
  deserializer: &'parent mut Deserializer<'read, R, S>,
  done: bool,
}

// When this object is dropped, advance the decoder past the unread items
impl<'read, 'parent, R: Read<'read>, S: Stack> Drop for FieldIterator<'read, 'parent, R, S> {
  #[inline(always)]
  fn drop(&mut self) {
    if self.deserializer.error.is_some() {
      return;
    }

    loop {
      let Some(next) = self.next() else { break };
      let next = next.map(|_| ());
      match next {
        Ok(()) => {}
        Err(e) => {
          self.deserializer.error = Some(e);
          break;
        }
      }
    }
  }
}

impl<'read, 'parent, R: Read<'read>, S: Stack> FieldIterator<'read, 'parent, R, S> {
  /// The next field within the object.
  ///
  /// This is approximate to `Iterator::next` yet each item maintains a mutable reference to the
  /// iterator. Accordingly, we cannot use `Iterator::next` which requires items not borrow from
  /// the iterator.
  ///
  /// [polonius-the-crab](https://docs.rs/polonius-the-crab) details a frequent limitation of
  /// Rust's borrow checker which users of this function may incur. It also details potential
  /// solutions (primarily using inlined code instead of functions, callbacks) before presenting
  /// itself as a complete solution. Please refer to it if you have difficulties calling this
  /// method for context.
  #[allow(clippy::type_complexity, clippy::should_implement_trait)]
  pub fn next(&mut self) -> Option<Result<Field<'read, '_, R, S>, JsonError<'read, R, S>>> {
    if let Some(err) = self.deserializer.error {
      return Some(Err(err));
    }

    if self.done {
      None?;
    }

    loop {
      let result = match self.deserializer.single_step() {
        Ok(SingleStepResult::Object(result)) => result,
        Ok(_) => break Some(Err(JsonError::InternalError)),
        Err(e) => break Some(Err(e)),
      };
      match result {
        SingleStepObjectResult::Field => break Some(Ok(handle_field(self.deserializer))),
        SingleStepObjectResult::Closed => {
          self.done = true;
          None?
        }
      }
    }
  }
}

/// An iterator over an array.
pub struct ArrayIterator<'read, 'parent, R: Read<'read>, S: Stack> {
  deserializer: &'parent mut Deserializer<'read, R, S>,
  done: bool,
}

// When this array is dropped, advance the decoder past the unread items
impl<'read, 'parent, R: Read<'read>, S: Stack> Drop for ArrayIterator<'read, 'parent, R, S> {
  #[inline(always)]
  fn drop(&mut self) {
    if self.deserializer.error.is_some() {
      return;
    }

    loop {
      let Some(next) = self.next() else { break };
      let next = next.map(|_| ());
      match next {
        Ok(()) => {}
        Err(e) => {
          self.deserializer.error = Some(e);
          break;
        }
      }
    }
  }
}

impl<'read, 'parent, R: Read<'read>, S: Stack> ArrayIterator<'read, 'parent, R, S> {
  /// The next item within the array.
  ///
  /// This is approximate to `Iterator::next` yet each item maintains a mutable reference to the
  /// iterator. Accordingly, we cannot use `Iterator::next` which requires items not borrow from
  /// the iterator.
  ///
  /// [polonius-the-crab](https://docs.rs/polonius-the-crab) details a frequent limitation of
  /// Rust's borrow checker which users of this function may incur. It also details potential
  /// solutions (primarily using inlined code instead of functions, callbacks) before presenting
  /// itself as a complete solution. Please refer to it if you have difficulties calling this
  /// method for context.
  #[allow(clippy::should_implement_trait)]
  pub fn next(&mut self) -> Option<Result<Value<'read, '_, R, S>, JsonError<'read, R, S>>> {
    if let Some(err) = self.deserializer.error {
      return Some(Err(err));
    }

    if self.done {
      None?;
    }

    loop {
      let result = match self.deserializer.single_step() {
        Ok(SingleStepResult::Array(result)) => result,
        Ok(_) => break Some(Err(JsonError::InternalError)),
        Err(e) => break Some(Err(e)),
      };
      match result {
        SingleStepArrayResult::Value => {
          break Some(Ok(Value { deserializer: Some(self.deserializer) }))
        }
        SingleStepArrayResult::Closed => {
          self.done = true;
          None?
        }
      }
    }
  }
}

impl<'read, 'parent, R: Read<'read>, S: Stack> Value<'read, 'parent, R, S> {
  /// Get the type of the current item.
  ///
  /// This does not assert it's a valid instance of this class of items. It solely asserts if this
  /// is a valid item, it will be of this type.
  #[inline(always)]
  pub fn kind(&mut self) -> Result<Type, JsonError<'read, R, S>> {
    Ok(kind(&self.deserializer.as_ref().ok_or(JsonError::InternalError)?.reader))
  }

  /// Iterate over the fields within this object.
  ///
  /// If a field is present multiple times, this will yield each instance.
  #[inline(always)]
  pub fn fields(mut self) -> Result<FieldIterator<'read, 'parent, R, S>, JsonError<'read, R, S>> {
    if !matches!(self.kind()?, Type::Object) {
      Err(JsonError::TypeError)?
    }

    let deserializer = self.deserializer.take().ok_or(JsonError::InternalError)?;
    match deserializer.single_step()? {
      SingleStepResult::Unknown(SingleStepUnknownResult::ObjectOpened) => {
        Ok(FieldIterator { deserializer, done: false })
      }
      _ => Err(JsonError::InternalError),
    }
  }

  /// Iterate over all items within this container.
  #[inline(always)]
  pub fn iterate(mut self) -> Result<ArrayIterator<'read, 'parent, R, S>, JsonError<'read, R, S>> {
    if !matches!(self.kind()?, Type::Array) {
      Err(JsonError::TypeError)?
    }

    let deserializer = self.deserializer.take().ok_or(JsonError::InternalError)?;
    match deserializer.single_step()? {
      SingleStepResult::Unknown(SingleStepUnknownResult::ArrayOpened) => {
        Ok(ArrayIterator { deserializer, done: false })
      }
      _ => Err(JsonError::InternalError),
    }
  }

  /// Get the current item as a 'string'.
  ///
  /// As we cannot perform allocations, we do not yield a [`alloc::string::String`] but rather an
  /// iterator for the contents of the serialized string (with its escape sequences handled). This
  /// may be converted to an `String` with `.collect::<Result<String, _>>()?`.
  ///
  /// RFC 8259 allows strings to specify invalid UTF-8 codepoints. This library supports working
  /// with such values, as required to be compliant with RFC 8259, but this function's return value
  /// will error when attempting to return a non-UTF-8 value. If the underlying JSON is valid, the
  /// deserializer will remain usable afterwards however, even though the rest of the non-UTF-8
  /// string will be inaccesible. Please keep this detail in mind.
  #[inline(always)]
  pub fn to_str(
    mut self,
  ) -> Result<
    impl use<'read, 'parent, R, S> + Iterator<Item = Result<char, JsonError<'read, R, S>>>,
    JsonError<'read, R, S>,
  > {
    if !matches!(self.kind()?, Type::String) {
      Err(JsonError::TypeError)?
    }

    let deserializer = self.deserializer.take().ok_or(JsonError::InternalError)?;
    match deserializer.single_step()? {
      SingleStepResult::Unknown(SingleStepUnknownResult::String) => {
        Ok(handle_string_value(deserializer))
      }
      _ => Err(JsonError::InternalError),
    }
  }

  /// Get the current item as a number.
  #[inline(always)]
  pub fn to_number(mut self) -> Result<Number, JsonError<'read, R, S>> {
    if !matches!(self.kind()?, Type::Number) {
      Err(JsonError::TypeError)?
    }

    let deserializer = self.deserializer.take().ok_or(JsonError::InternalError)?;
    match deserializer.single_step()? {
      SingleStepResult::Unknown(SingleStepUnknownResult::Number(number)) => Ok(number),
      _ => Err(JsonError::InternalError),
    }
  }

  /// Get the current item as a `bool`.
  #[inline(always)]
  pub fn to_bool(mut self) -> Result<bool, JsonError<'read, R, S>> {
    if !matches!(self.kind()?, Type::Bool) {
      Err(JsonError::TypeError)?
    }

    let deserializer = self.deserializer.take().ok_or(JsonError::InternalError)?;
    match deserializer.single_step()? {
      SingleStepResult::Unknown(SingleStepUnknownResult::Bool(bool)) => Ok(bool),
      _ => Err(JsonError::InternalError),
    }
  }

  /// Get the current item as `null`.
  ///
  /// The point of this method is to assert the value is `null` _and valid_. `kind` only tells the
  /// caller if it's a valid value, it will be `null`.
  #[inline(always)]
  pub fn to_null(mut self) -> Result<(), JsonError<'read, R, S>> {
    if !matches!(self.kind()?, Type::Null) {
      Err(JsonError::TypeError)?
    }

    let deserializer = self.deserializer.take().ok_or(JsonError::InternalError)?;
    match deserializer.single_step()? {
      SingleStepResult::Unknown(SingleStepUnknownResult::Null) => Ok(()),
      _ => Err(JsonError::InternalError),
    }
  }
}