jiter 0.18.0

Fast Iterable JSON parser
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
413
414
415
416
417
use crate::errors::{DEFAULT_RECURSION_LIMIT, JiterError, JsonType, LinePosition, json_error};
use crate::number_decoder::{NumberAny, NumberFloat, NumberInt, NumberRange};
use crate::parse::{Parser, Peek};
use crate::string_decoder::{StringDecoder, StringDecoderRange, Tape};
use crate::value::{JsonValue, take_value_borrowed, take_value_owned, take_value_skip};
use crate::{JsonError, JsonErrorType, PartialMode};

pub type JiterResult<T> = Result<T, JiterError>;

/// A JSON iterator.
#[derive(Debug)]
pub struct Jiter<'j> {
    data: &'j [u8],
    parser: Parser<'j>,
    tape: Tape,
    allow_inf_nan: bool,
    allow_partial_strings: bool,
}

impl Clone for Jiter<'_> {
    /// Clone a `Jiter`. Like the default implementation, but a new empty `tape` is used.
    fn clone(&self) -> Self {
        Self {
            data: self.data,
            parser: self.parser.clone(),
            tape: Tape::default(),
            allow_inf_nan: self.allow_inf_nan,
            allow_partial_strings: self.allow_partial_strings,
        }
    }
}

impl<'j> Jiter<'j> {
    /// Constructs a new `Jiter`.
    ///
    /// # Arguments
    /// - `data`: The JSON data to be parsed.
    /// - `allow_inf_nan`: Whether to allow `NaN`, `Infinity` and `-Infinity` as numbers.
    pub fn new(data: &'j [u8]) -> Self {
        Self {
            data,
            parser: Parser::new(data),
            tape: Tape::default(),
            allow_inf_nan: false,
            allow_partial_strings: false,
        }
    }

    /// Constructs a `Jiter` that decodes escaped strings into `tape`, keeping its capacity.
    ///
    /// Pair with [`Jiter::into_tape`] to parse many documents with one buffer: a `Jiter::new`
    /// allocates a fresh tape the first time a document needs one.
    pub fn with_tape(data: &'j [u8], tape: Vec<u8>) -> Self {
        Self {
            data,
            parser: Parser::new(data),
            tape,
            allow_inf_nan: false,
            allow_partial_strings: false,
        }
    }

    /// Returns the tape, for [`Jiter::with_tape`] on the next document.
    pub fn into_tape(self) -> Vec<u8> {
        self.tape
    }

    pub fn with_allow_inf_nan(mut self) -> Self {
        self.allow_inf_nan = true;
        self
    }

    pub fn with_allow_partial_strings(mut self) -> Self {
        self.allow_partial_strings = true;
        self
    }

    /// Get the current [LinePosition] of the parser.
    pub fn current_position(&self) -> LinePosition {
        self.parser.current_position()
    }

    /// Get the current index of the parser.
    pub fn current_index(&self) -> usize {
        self.parser.index
    }

    /// Get a slice of the underlying JSON data from `start` to `current_index`.
    pub fn slice_to_current(&self, start: usize) -> &'j [u8] {
        &self.data[start..self.current_index()]
    }

    /// Convert an error index to a [LinePosition].
    ///
    /// # Arguments
    /// - `index`: The index of the error to find the position of.
    pub fn error_position(&self, index: usize) -> LinePosition {
        LinePosition::find(self.data, index)
    }

    /// Peek at the next JSON value without consuming it.
    pub fn peek(&mut self) -> JiterResult<Peek> {
        self.parser.peek().map_err(Into::into)
    }

    /// Assuming the next value is `null`, consume it. Error if it is not `null`, or is invalid JSON.
    pub fn next_null(&mut self) -> JiterResult<()> {
        let peek = self.peek()?;
        match peek {
            Peek::Null => self.known_null(),
            _ => Err(self.wrong_type(JsonType::Null, peek)),
        }
    }

    /// Knowing the next value is `null`, consume it.
    pub fn known_null(&mut self) -> JiterResult<()> {
        self.parser.consume_null()?;
        Ok(())
    }

    /// Assuming the next value is `true` or `false`, consume it. Error if it is not a boolean, or is invalid JSON.
    ///
    /// # Returns
    /// The boolean value.
    pub fn next_bool(&mut self) -> JiterResult<bool> {
        let peek = self.peek()?;
        self.known_bool(peek)
    }

    /// Knowing the next value is `true` or `false`, parse it.
    pub fn known_bool(&mut self, peek: Peek) -> JiterResult<bool> {
        match peek {
            Peek::True => {
                self.parser.consume_true()?;
                Ok(true)
            }
            Peek::False => {
                self.parser.consume_false()?;
                Ok(false)
            }
            _ => Err(self.wrong_type(JsonType::Bool, peek)),
        }
    }

    /// Assuming the next value is a number, consume it. Error if it is not a number, or is invalid JSON.
    ///
    /// # Returns
    /// A [NumberAny] representing the number.
    pub fn next_number(&mut self) -> JiterResult<NumberAny> {
        let peek = self.peek()?;
        self.known_number(peek)
    }

    /// Knowing the next value is a number, parse it.
    pub fn known_number(&mut self, peek: Peek) -> JiterResult<NumberAny> {
        self.parser
            .consume_number::<NumberAny>(peek.into_inner(), self.allow_inf_nan)
            .map_err(|e| self.maybe_number_error(e, JsonType::Int, peek))
    }

    /// Assuming the next value is an integer, consume it. Error if it is not an integer, or is invalid JSON.
    pub fn next_int(&mut self) -> JiterResult<NumberInt> {
        let peek = self.peek()?;
        self.known_int(peek)
    }

    /// Knowing the next value is an integer, parse it.
    pub fn known_int(&mut self, peek: Peek) -> JiterResult<NumberInt> {
        self.parser
            .consume_number::<NumberInt>(peek.into_inner(), self.allow_inf_nan)
            .map_err(|e| {
                if e.error_type == JsonErrorType::FloatExpectingInt {
                    JiterError::wrong_type(JsonType::Int, JsonType::Float, self.parser.index)
                } else {
                    self.maybe_number_error(e, JsonType::Int, peek)
                }
            })
    }

    /// Assuming the next value is a float, consume it. Error if it is not a float, or is invalid JSON.
    pub fn next_float(&mut self) -> JiterResult<f64> {
        let peek = self.peek()?;
        self.known_float(peek)
    }

    /// Knowing the next value is a float, parse it.
    pub fn known_float(&mut self, peek: Peek) -> JiterResult<f64> {
        self.parser
            .consume_number::<NumberFloat>(peek.into_inner(), self.allow_inf_nan)
            .map(|f| f.0)
            .map_err(|e| self.maybe_number_error(e, JsonType::Float, peek))
    }

    /// Assuming the next value is a number, consume it and return bytes from the original JSON data.
    pub fn next_number_bytes(&mut self) -> JiterResult<&[u8]> {
        let peek = self.peek()?;
        self.known_number_bytes(peek)
    }

    /// Knowing the next value is a number, parse it and return bytes from the original JSON data.
    pub fn known_number_bytes(&mut self, peek: Peek) -> JiterResult<&[u8]> {
        match self
            .parser
            .consume_number::<NumberRange>(peek.into_inner(), self.allow_inf_nan)
        {
            Ok(numbe_range) => Ok(&self.data[numbe_range.range]),
            Err(e) => Err(self.maybe_number_error(e, JsonType::Float, peek)),
        }
    }

    /// Assuming the next value is a string, consume it. Error if it is not a string, or is invalid JSON.
    pub fn next_str(&mut self) -> JiterResult<&str> {
        let peek = self.peek()?;
        match peek {
            Peek::String => self.known_str(),
            _ => Err(self.wrong_type(JsonType::String, peek)),
        }
    }

    /// Knowing the next value is a string, parse it.
    pub fn known_str(&mut self) -> JiterResult<&str> {
        match self
            .parser
            .consume_string::<StringDecoder>(&mut self.tape, self.allow_partial_strings)
        {
            Ok(output) => Ok(output.as_str()),
            Err(e) => Err(e.into()),
        }
    }

    /// Assuming the next value is a string, consume it and return bytes from the original JSON data.
    pub fn next_bytes(&mut self) -> JiterResult<&[u8]> {
        let peek = self.peek()?;
        match peek {
            Peek::String => self.known_bytes(),
            _ => Err(self.wrong_type(JsonType::String, peek)),
        }
    }

    /// Knowing the next value is a string, parse it and return bytes from the original JSON data.
    pub fn known_bytes(&mut self) -> JiterResult<&[u8]> {
        let range = self
            .parser
            .consume_string::<StringDecoderRange>(&mut self.tape, self.allow_partial_strings)?;
        Ok(&self.data[range])
    }

    /// Parse the next JSON value and return it as a [JsonValue]. Error if it is invalid JSON.
    pub fn next_value(&mut self) -> JiterResult<JsonValue<'j>> {
        let peek = self.peek()?;
        self.known_value(peek)
    }

    /// Parse the next JSON value and return it as a [JsonValue]. Error if it is invalid JSON.
    ///
    /// # Arguments
    /// - `peek`: The [Peek] of the next JSON value.
    pub fn known_value(&mut self, peek: Peek) -> JiterResult<JsonValue<'j>> {
        take_value_borrowed(
            peek,
            &mut self.parser,
            &mut self.tape,
            DEFAULT_RECURSION_LIMIT,
            self.allow_inf_nan,
            PartialMode::Off,
        )
        .map_err(Into::into)
    }

    /// Parse the next JSON value, but don't return it.
    /// This should be faster than returning the value, useful when you don't care about this value.
    /// Error if it is invalid JSON.
    ///
    /// *WARNING:* For performance reasons, this method does not check that strings would be valid UTF-8.
    pub fn next_skip(&mut self) -> JiterResult<()> {
        let peek = self.peek()?;
        self.known_skip(peek)
    }

    /// Parse the next JSON value, but don't return it. Error if it is invalid JSON.
    ///
    /// # Arguments
    /// - `peek`: The [Peek] of the next JSON value.
    pub fn known_skip(&mut self, peek: Peek) -> JiterResult<()> {
        take_value_skip(
            peek,
            &mut self.parser,
            &mut self.tape,
            DEFAULT_RECURSION_LIMIT,
            self.allow_inf_nan,
        )
        .map_err(Into::into)
    }

    /// Parse the next JSON value and return it as a [JsonValue] with static lifetime. Error if it is invalid JSON.
    pub fn next_value_owned(&mut self) -> JiterResult<JsonValue<'static>> {
        let peek = self.peek()?;
        self.known_value_owned(peek)
    }

    /// Parse the next JSON value and return it as a [JsonValue] with static lifetime. Error if it is invalid JSON.
    ///
    /// # Arguments
    /// - `peek`: The [Peek] of the next JSON value.
    pub fn known_value_owned(&mut self, peek: Peek) -> JiterResult<JsonValue<'static>> {
        take_value_owned(
            peek,
            &mut self.parser,
            &mut self.tape,
            DEFAULT_RECURSION_LIMIT,
            self.allow_inf_nan,
            PartialMode::Off,
        )
        .map_err(Into::into)
    }

    /// Assuming the next value is an array, peek at the first value.
    /// Error if it is not an array, or is invalid JSON.
    ///
    /// # Returns
    /// The `Some(peek)` of the first value in the array is not empty, `None` if it is empty.
    pub fn next_array(&mut self) -> JiterResult<Option<Peek>> {
        let peek = self.peek()?;
        match peek {
            Peek::Array => self.known_array(),
            _ => Err(self.wrong_type(JsonType::Array, peek)),
        }
    }

    /// Assuming the next value is an array, peat at the first value.
    pub fn known_array(&mut self) -> JiterResult<Option<Peek>> {
        self.parser.array_first().map_err(Into::into)
    }

    /// Peek at the next value in an array.
    pub fn array_step(&mut self) -> JiterResult<Option<Peek>> {
        self.parser.array_step().map_err(Into::into)
    }

    /// Assuming the next value is an object, consume the first key.
    /// Error if it is not an object, or is invalid JSON.
    ///
    /// # Returns
    /// The `Some(key)` of the first key in the object is not empty, `None` if it is empty.
    pub fn next_object(&mut self) -> JiterResult<Option<&str>> {
        let peek = self.peek()?;
        match peek {
            Peek::Object => self.known_object(),
            _ => Err(self.wrong_type(JsonType::Object, peek)),
        }
    }

    /// Assuming the next value is an object, conssume the first key and return bytes from the original JSON data.
    pub fn known_object(&mut self) -> JiterResult<Option<&str>> {
        let op_str = self.parser.object_first::<StringDecoder>(&mut self.tape)?;
        Ok(op_str.map(|s| s.as_str()))
    }

    /// Assuming the next value is an object, peek at the first key.
    pub fn next_object_bytes(&mut self) -> JiterResult<Option<&[u8]>> {
        let peek = self.peek()?;
        match peek {
            Peek::Object => {
                let op_range = self.parser.object_first::<StringDecoderRange>(&mut self.tape)?;
                Ok(op_range.map(|r| &self.data[r]))
            }
            _ => Err(self.wrong_type(JsonType::Object, peek)),
        }
    }

    /// Get the next key in an object, or `None` if there are no more keys.
    pub fn next_key(&mut self) -> JiterResult<Option<&str>> {
        let strs = self.parser.object_step::<StringDecoder>(&mut self.tape)?;
        Ok(strs.map(|s| s.as_str()))
    }

    /// Get the next key in an object as bytes, or `None` if there are no more keys.
    pub fn next_key_bytes(&mut self) -> JiterResult<Option<&[u8]>> {
        let op_range = self.parser.object_step::<StringDecoderRange>(&mut self.tape)?;
        Ok(op_range.map(|r| &self.data[r]))
    }

    /// Finish parsing the JSON data. Error if there is more data to be parsed.
    pub fn finish(&mut self) -> JiterResult<()> {
        self.parser.finish().map_err(Into::into)
    }

    fn wrong_type(&self, expected: JsonType, peek: Peek) -> JiterError {
        match peek {
            Peek::True | Peek::False => JiterError::wrong_type(expected, JsonType::Bool, self.parser.index),
            Peek::Null => JiterError::wrong_type(expected, JsonType::Null, self.parser.index),
            Peek::String => JiterError::wrong_type(expected, JsonType::String, self.parser.index),
            Peek::Array => JiterError::wrong_type(expected, JsonType::Array, self.parser.index),
            Peek::Object => JiterError::wrong_type(expected, JsonType::Object, self.parser.index),
            _ if peek.is_num() => self.wrong_num(peek.into_inner(), expected),
            _ => json_error!(ExpectedSomeValue, self.parser.index).into(),
        }
    }

    fn wrong_num(&self, first: u8, expected: JsonType) -> JiterError {
        let mut parser2 = self.parser.clone();
        let actual = match parser2.consume_number::<NumberAny>(first, self.allow_inf_nan) {
            Ok(NumberAny::Int { .. }) => JsonType::Int,
            Ok(NumberAny::Float { .. }) => JsonType::Float,
            Err(e) => return e.into(),
        };
        JiterError::wrong_type(expected, actual, self.parser.index)
    }

    fn maybe_number_error(&self, e: JsonError, expected: JsonType, peek: Peek) -> JiterError {
        if peek.is_num() {
            e.into()
        } else {
            self.wrong_type(expected, peek)
        }
    }
}