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
#![feature(portable_simd)]

use std::mem;
use std::string::String as StdString;
use std::vec::Vec as StdVec;

use bumpalo::collections::{String, Vec};
use bumpalo::Bump;
use hashbrown::hash_map::DefaultHashBuilder;
use hashbrown::{BumpWrapper, HashMap};
use parsers::{number::read_number, string::read_string};
use slice_iter::{CopyIter, SliceIter};

mod parsers;
mod slice_iter;

pub type JsonObject<'bump> =
    HashMap<String<'bump>, JsonValue<'bump>, DefaultHashBuilder, BumpWrapper<'bump>>;

pub type JsonResult<T> = Result<T, ParseError>;

#[derive(Debug, PartialEq)]
pub enum JsonValue<'bump> {
    Object(&'bump mut JsonObject<'bump>),
    Array(&'bump mut Vec<'bump, JsonValue<'bump>>),
    String(&'bump mut String<'bump>),
    Number(f64),
    Boolean(bool),
    Null,
}

#[derive(thiserror::Error, Debug)]
pub enum JsonError {
    #[error("File too long: len {len} longer than maximum allowed of 500 MiB")]
    FileTooLong { len: usize },
    #[error("ParseError: {0}, with this json remaining: {1}")]
    ParseError(ParseError, StdString),
}

#[derive(thiserror::Error, Debug)]
pub enum ParseError {
    #[error("Expected closing bracket or comma, found {found:?}")]
    ExpectedEndOfArray { found: Option<char> },
    #[error("Expected closing brace or comma, found {found:?}")]
    ExpectedEndOfObject { found: Option<char> },
    #[error("Expected next value, found {found:?}")]
    ExpectedNextValue { found: Option<char> },
    #[error("Expect colon after object key, found {found:?}")]
    ExpectedColon { found: Option<char> },
    #[error("Invalid number literal")]
    InvalidNumberLiteral,
    #[error("Invalid string escape")]
    InvalidStringEscape,
    #[error("JSON string was not valid UTF-8: {string}")]
    InvalidUtf8 { string: StdString },
    #[error("String must start with a quote `\"`, found {found:?}")]
    StringQuote { found: Option<char> },
    #[error("Expected the next value, found the end of the file")]
    UnexpectedEndOfFile,
}

enum ParseState {
    Value,
    Object,
    Array,
}

pub struct JsonDocument {
    root: &'static mut JsonValue<'static>,
    allocator: Bump,
}

impl JsonDocument {
    pub fn init() -> Self {
        let allocator = Bump::new();
        Self {
            root: unsafe { mem::transmute(allocator.alloc(JsonValue::Null)) },
            allocator,
        }
    }
    pub fn parse_slice<'a>(&'a mut self, slice: &[u8]) -> Result<&'a JsonValue<'a>, JsonError> {
        let json_val = parse(slice, &self.allocator)?;
        self.root = unsafe { mem::transmute(self.allocator.alloc(json_val)) };
        Ok(unsafe { mem::transmute(&*self.root) })
    }
    pub fn parse_create(slice: &[u8]) -> Result<Self, JsonError> {
        let allocator = Bump::new();
        let json_val = parse(slice, &allocator)?;
        Ok(Self {
            root: unsafe { mem::transmute(allocator.alloc(json_val)) },
            allocator,
        })
    }
    pub fn root<'a>(&'a self) -> &'a JsonValue<'a> {
        unsafe { mem::transmute(&*self.root) }
    }
    pub fn root_mut<'a>(&'a mut self) -> &'a mut JsonValue<'a> {
        *unsafe {
            mem::transmute::<&mut &'static mut JsonValue<'static>, &'a mut &'a mut JsonValue<'a>>(
                &mut self.root,
            )
        }
    }
    pub fn alloc<'a, T>(&'a self, thing: T) -> &'a mut T {
        self.allocator.alloc(thing)
    }
    pub fn str<'a>(&'a self, str: &str) -> String<'a> {
        String::from_str_in(str, &self.allocator)
    }
    pub fn bump<'a>(&'a self) -> &'a Bump {
        &self.allocator
    }
}

pub fn parse<'a, 'bump>(
    json_buf: &'a [u8],
    allocator: &'bump Bump,
) -> Result<JsonValue<'bump>, JsonError> {
    if json_buf.len() >= 0x20000000 {
        return Err(JsonError::FileTooLong {
            len: json_buf.len(),
        });
    }

    let mut json = SliceIter::new(json_buf);
    ignore_ws(&mut json);

    match parse_next(&mut json, allocator, ParseState::Value) {
        Ok(v) => return Ok(v),
        Err(e) => {
            let remaining = StdString::from_utf8_lossy(&json.collect::<StdVec<u8>>()).to_string();
            return Err(JsonError::ParseError(
                e,
                if remaining.len() >= 500 {
                    format!("{}…", &remaining[0..500])
                } else {
                    remaining
                },
            ));
        }
    };
}

fn parse_next<'a, 'bump>(
    json: &mut SliceIter<'a, u8>,
    alloc: &'bump Bump,
    state: ParseState,
) -> JsonResult<JsonValue<'bump>> {
    ignore_ws(json);
    if let Some(char) = json.peek_copy() {
        match state {
            ParseState::Value => {
                if is_string(char) {
                    return Ok(JsonValue::String(alloc.alloc(read_string(json, alloc)?)));
                }
                if is_number(char) {
                    return Ok(JsonValue::Number(read_number(json)?));
                }
                if is_array(char) {
                    json.ignore_next();
                    return parse_next(json, alloc, ParseState::Array);
                }
                if is_object(char) {
                    json.ignore_next();
                    return parse_next(json, alloc, ParseState::Object);
                }
                let next_4: [u8; 4] = json
                    .peek_many()
                    .map_or(Err(ParseError::UnexpectedEndOfFile), |v| Ok(v))?;
                if &next_4 == b"true" {
                    json.ignore_many(4);
                    return Ok(JsonValue::Boolean(true));
                }
                if &next_4 == b"null" {
                    json.ignore_many(4);
                    return Ok(JsonValue::Null);
                }
                if json.peek_many_ref(5) == Some(b"false") {
                    json.ignore_many(5);
                    return Ok(JsonValue::Boolean(false));
                }
                return Err(ParseError::ExpectedNextValue {
                    found: json.next().map(|i| i as char),
                });
            }
            ParseState::Array => {
                let mut contents = Vec::new_in(alloc);
                if b']' == char {
                    _ = json.next().unwrap();
                    return Ok(JsonValue::Array(alloc.alloc(contents)));
                }
                loop {
                    ignore_ws(json);
                    contents.push(parse_next(json, alloc, ParseState::Value)?);
                    ignore_ws(json);

                    match json.next() {
                        Some(b']') => break,
                        Some(b',') => continue,
                        v => {
                            return Err(ParseError::ExpectedEndOfArray {
                                found: v.map(|v| v as char),
                            })
                        }
                    }
                }
                contents.shrink_to_fit();
                return Ok(JsonValue::Array(alloc.alloc(contents)));
            }
            ParseState::Object => {
                let mut contents: HashMap<String<'bump>, JsonValue<'bump>, _, BumpWrapper> =
                    HashMap::new_in(BumpWrapper(alloc));
                if char == b'}' {
                    _ = json.next().unwrap();
                    return Ok(JsonValue::Object(alloc.alloc(contents)));
                }
                loop {
                    ignore_ws(json);
                    let key = read_string(json, alloc)?;
                    ignore_ws(json);
                    let c = json.next();
                    if c != Some(b':') {
                        return Err(ParseError::ExpectedColon {
                            found: c.map(|i| i as char),
                        });
                    }
                    ignore_ws(json);
                    let value = parse_next(json, alloc, ParseState::Value)?;
                    contents.insert(key, value);
                    ignore_ws(json);
                    match json.next() {
                        Some(b'}') => break,
                        Some(b',') => continue,
                        v => {
                            return Err(ParseError::ExpectedEndOfObject {
                                found: v.map(|i| i as char),
                            })
                        }
                    }
                }
                return Ok(JsonValue::Object(alloc.alloc(contents)));
            }
        }
    }
    return Ok(JsonValue::Null);
}

fn ignore_ws<'a, I: CopyIter<'a, Item = u8>>(json: &mut I) {
    while json.peek_copy().map_or(false, is_whitespace) {
        json.ignore_next();
    }
}

fn is_whitespace(char: u8) -> bool {
    char == 0x0020 || char == 0x000A || char == 0x000D || char == 0x0009
}

fn is_string(char: u8) -> bool {
    char == b'"'
}

fn is_object(char: u8) -> bool {
    char == b'{'
}

fn is_array(char: u8) -> bool {
    char == b'['
}

fn is_number(char: u8) -> bool {
    (char >= b'0' && char <= b'9') || char == b'-'
}

#[cfg(test)]
mod tests {
    use std::mem;

    use super::*;

    #[test]
    fn sizes() {
        assert_eq!(mem::size_of::<JsonValue>(), 16);
    }

    #[test]
    fn string() {
        // string, "string", string—🎸🦕㻘\x03\x0C
        let bump = Bump::new();
        let val = parse(
            "\"string, \\\"string\\\", string—🎸\\uD83E\\uDD95\\u3ED8\\u0003\\f\"".as_bytes(),
            &bump,
        )
        .unwrap();
        assert_eq!(
            val,
            JsonValue::String(bump.alloc(String::from_str_in(
                "string, \"string\", string—🎸🦕㻘\x03\x0C",
                &bump
            )))
        )
    }

    #[test]
    fn json_array() {
        let mut string = "[5   ,\n\n".repeat(100);
        string.push_str("[\"algo muy interesante. Ay si, ya tu sabes. ¡Imagínate!\", 3.1415926535, 5.2e+50, \"\",null,true,false,[],[],[],[[[[[[[[[[[[[[]]]]]]]]]]]]]]]");
        string.push_str(&"]".repeat(100));
        let bump = Bump::new();
        let ret = parse(string.as_bytes(), &bump).unwrap();
        eprintln!("{:?}", ret);
    }

    #[test]
    fn json_atoms() {
        let string = "[null, true,false,null,  true, false]";
        let bump = Bump::new();
        let ret = parse(string.as_bytes(), &bump).unwrap();
        assert_eq!(
            ret,
            JsonValue::Array(bump.alloc(bumpalo::vec![
                in &bump;
                JsonValue::Null,
                JsonValue::Boolean(true),
                JsonValue::Boolean(false),
                JsonValue::Null,
                JsonValue::Boolean(true),
                JsonValue::Boolean(false)
            ]))
        );
    }

    #[test]
    fn json_object() {
        let string = "{\n\t\t\"name\":\"Steve\"\n\t}";
        let bump = Bump::new();
        let ret = parse(string.as_bytes(), &bump).unwrap();
        match ret {
            JsonValue::Object(obj) => match obj.get("name") {
                Some(JsonValue::String(str)) => {
                    assert_eq!(str.as_str(), "Steve");
                }
                _ => panic!("Expect name field to be string"),
            },
            _ => panic!("Expected object"),
        }
    }

    #[test]
    fn json_object_document() {
        let string = b"{\n\t\t\"name\":\"Steve\"\n\t}";
        let ret = JsonDocument::parse_create(&string[..]).unwrap();
        let root = ret.root();
        match &root {
            JsonValue::Object(obj) => match obj.get("name") {
                Some(JsonValue::String(str)) => {
                    assert_eq!(str.as_str(), "Steve");
                }
                _ => panic!("Expect name field to be string"),
            },
            _ => panic!("Expected object"),
        }
    }

    #[test]
    fn mutable_json_doc() {
        let string = br#"
{
    "name": "Steve",
    "nickname": "Cementhead"
}"#;
        let mut ret = JsonDocument::parse_create(&string[..]).unwrap();
        let bump = Bump::new();
        let root = ret.root_mut();
        match root {
            JsonValue::Object(obj) => {
                match obj.get("name").expect("Expected to have name property") {
                    JsonValue::String(str) => assert_eq!(*str, "Steve"),
                    _ => panic!("Expected string"),
                }
                assert_eq!(
                    Some(&JsonValue::String(
                        bump.alloc(String::from_str_in("Cementhead", &bump))
                    )),
                    obj.get("nickname")
                );
                obj.insert(
                    String::from_str_in("nickname", &bump),
                    JsonValue::String(
                        bump.alloc(String::from_utf8_lossy_in(&[0x61; 50000], &bump)),
                    ),
                );
            }
            _ => panic!("Expected object"),
        }
        // drop(root);
        let root = ret.root();
        assert_eq!(
            Some(&JsonValue::String(
                bump.alloc(String::from_utf8_lossy_in(&[0x61; 50000], &bump))
            )),
            match &root {
                JsonValue::Object(obj) => obj.get("nickname"),
                _ => panic!("Expected object"),
            }
        );
    }
}