fjson 0.3.1

A library for parsing and formatting json with C-style comments and trailing commas
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
//! Format `Root` values to JSONC or pretty/compact JSON.

use std::fmt::{Error, Write};

use crate::{
    ast::{ArrayValue, Comment, Metadata, ObjectValue, Root, Value, ValueToken},
    scanner::{ScanResult, Token},
    validate::ValidateIter,
};

/// Options represents the customizations that can be made when formatting.
#[derive(Debug, Copy, Clone)]
pub struct Options<'a> {
    indent: &'a str,
    line_length: usize,
    max_object_pairs_per_line: usize,
    max_array_values_per_line: usize,
}

impl Default for Options<'_> {
    fn default() -> Self {
        Self {
            indent: "  ",
            line_length: 80,
            max_object_pairs_per_line: 1,
            max_array_values_per_line: 4,
        }
    }
}

impl<'a> Options<'a> {
    /// Sets the indent to the provided string. The default is two spaces.
    pub fn with_indent(self, s: &'a str) -> Self {
        Self { indent: s, ..self }
    }

    /// Sets the line length that objects and arrays will wrap on. The default
    /// is 80 characters.
    pub fn with_line_length(self, n: usize) -> Self {
        Self {
            line_length: n,
            ..self
        }
    }

    /// Sets the maximum number of object key/value pairs that can appear on the
    /// same line. The default is 1.
    pub fn with_max_object_pairs_per_line(self, n: usize) -> Self {
        Self {
            max_object_pairs_per_line: n,
            ..self
        }
    }

    /// Sets the maximum number of array values that can appear on the same
    /// line. The default is 4.
    pub fn with_max_array_values_per_line(self, n: usize) -> Self {
        Self {
            max_array_values_per_line: n,
            ..self
        }
    }
}

/// Serializes/formats the provided JSON [Root] value to the writer as "jsonc".
///
/// The output will be formatted according to a number of rules and is intended
/// for human viewing.
pub fn write_jsonc<W: Write>(w: &mut W, root: &Root) -> Result<(), Error> {
    write_jsonc_opts(w, root, &Options::default())
}

/// Serializes/formats the provided JSON [Root] value to the writer as "jsonc"
/// using the formatting options.
///
/// The output written to `w` is intended for human viewing.
pub fn write_jsonc_opts<W: Write>(w: &mut W, root: &Root, opts: &Options) -> Result<(), Error> {
    let mut ctx = Context {
        w,
        current_line_chars: 0,
        opts: *opts,
    };
    for meta in &root.meta_above {
        ctx.write_metadata(meta)?;
        ctx.write_newline()?;
    }
    ctx.write_value(&root.value.token, 0, false)?;
    ctx.write_comments(&root.value.comments)?;
    for meta in &root.meta_below {
        ctx.write_metadata(meta)?;
        ctx.write_newline()?;
    }
    ctx.write_newline()
}

struct Context<'a, W: Write> {
    w: &'a mut W,
    current_line_chars: usize,
    opts: Options<'a>,
}

impl<'a, W: Write> Context<'a, W> {
    fn write_value(
        &mut self,
        value: &ValueToken,
        indent: usize,
        allow_sameline: bool,
    ) -> Result<(), Error> {
        match value {
            ValueToken::Object(vals) => self.write_json_object(vals, indent, allow_sameline),
            ValueToken::Array(vals) => self.write_json_array(vals, indent, allow_sameline),
            ValueToken::String(v) => self.write_json_string(v),
            ValueToken::Number(v) => self.write_str(v),
            ValueToken::Bool(v) => self.write_json_bool(*v),
            ValueToken::Null => self.write_str("null"),
        }
    }

    fn write_json_object(
        &mut self,
        vals: &[ObjectValue],
        indent: usize,
        allow_sameline: bool,
    ) -> Result<(), Error> {
        let length = vals.len();
        let same_line = allow_sameline
            && self.opts.line_length > self.current_line_chars()
            && self
                .can_fit_object(vals, self.opts.line_length - self.current_line_chars())
                .is_some();

        self.write_char('{')?;
        for (i, val) in vals.iter().enumerate() {
            if same_line {
                self.write_char(' ')?;
            } else {
                self.write_newline()?;
                match val {
                    ObjectValue::Metadata(Metadata::Newline) => {}
                    _ => self.write_indent(indent + 1)?,
                }
            }
            match val {
                ObjectValue::KeyVal(k, v) => {
                    self.write_json_string(k)?;
                    self.write_str(": ")?;
                    self.write_value(&v.token, indent + 1, true)?;
                    if i < length - 1 {
                        self.write_char(',')?;
                    }
                    self.write_comments(&v.comments)?;
                }
                ObjectValue::Metadata(meta) => self.write_metadata(meta)?,
            }
        }
        if length > 0 {
            if same_line {
                self.write_char(' ')?;
            } else {
                self.write_newline()?;
                self.write_indent(indent)?;
            }
        }
        self.write_char('}')
    }

    fn write_json_array(
        &mut self,
        vals: &[ArrayValue],
        indent: usize,
        allow_sameline: bool,
    ) -> Result<(), Error> {
        let length = vals.len();
        let same_line = allow_sameline
            && self.opts.line_length > self.current_line_chars()
            && self
                .can_fit_array(vals, self.opts.line_length - self.current_line_chars())
                .is_some();

        self.write_char('[')?;
        for (i, val) in vals.iter().enumerate() {
            if same_line {
                if i > 0 {
                    self.write_char(' ')?;
                }
            } else {
                self.write_newline()?;
                match val {
                    ArrayValue::Metadata(Metadata::Newline) => {}
                    _ => self.write_indent(indent + 1)?,
                }
            }
            match val {
                ArrayValue::ArrayVal(v) => {
                    self.write_value(&v.token, indent + 1, true)?;
                    if i < length - 1 {
                        self.write_char(',')?;
                    }
                    self.write_comments(&v.comments)?;
                }
                ArrayValue::Metadata(meta) => self.write_metadata(meta)?,
            }
        }
        if length > 0 && !same_line {
            self.write_newline()?;
            self.write_indent(indent)?;
        }
        self.write_char(']')
    }

    fn write_json_bool(&mut self, v: bool) -> Result<(), Error> {
        if v {
            self.write_str("true")
        } else {
            self.write_str("false")
        }
    }

    fn current_line_chars(&self) -> usize {
        self.current_line_chars
    }

    fn write_metadata(&mut self, meta: &Metadata) -> Result<(), Error> {
        if let Metadata::Comment(c) = meta {
            self.write_comment(c)?;
        }
        Ok(())
    }

    fn write_comments(&mut self, cs: &[Comment]) -> Result<(), Error> {
        for comment in cs {
            self.write_char(' ')?;
            self.write_comment(comment)?;
        }
        Ok(())
    }

    fn write_comment(&mut self, comment: &Comment) -> Result<(), Error> {
        match comment {
            Comment::Block(c) => {
                self.write_str("/*")?;
                self.write_str(c)?;
                if let Some(i) = c.rfind('\n') {
                    // If the block comment contains newlines, adjust the
                    // internal value of chars written for the current line.
                    self.current_line_chars = c[(i + 1)..].chars().count();
                }
                self.write_str("*/")
            }
            Comment::Line(c) => {
                self.write_str("//")?;
                self.write_str(c)
            }
        }
    }

    fn write_json_string(&mut self, s: &str) -> Result<(), Error> {
        self.write_char('"')?;
        self.write_str(s)?;
        self.write_char('"')
    }

    fn write_indent(&mut self, n: usize) -> Result<(), Error> {
        for _ in 0..n {
            self.write_str(self.opts.indent)?;
        }
        Ok(())
    }

    fn write_str(&mut self, s: &str) -> Result<(), Error> {
        self.w.write_str(s)?;
        self.current_line_chars += s.chars().count();
        Ok(())
    }

    fn write_newline(&mut self) -> Result<(), Error> {
        self.write_char('\n')?;
        self.current_line_chars = 0;
        Ok(())
    }

    fn write_char(&mut self, c: char) -> Result<(), Error> {
        self.w.write_char(c)?;
        self.current_line_chars += 1;
        Ok(())
    }

    fn can_fit_value(&self, val: &ValueToken, space: usize) -> Option<usize> {
        let remaining = space as i64;
        let remaining = match val {
            ValueToken::Object(v) => return self.can_fit_object(v, space),
            ValueToken::Array(v) => return self.can_fit_array(v, space),
            ValueToken::String(v) => remaining - (2 + v.chars().count() as i64),
            ValueToken::Number(v) => remaining - v.len() as i64,
            ValueToken::Bool(v) => {
                if *v {
                    remaining - 4
                } else {
                    remaining - 5
                }
            }
            ValueToken::Null => remaining - 4,
        };
        if remaining >= 0 {
            Some(remaining as usize)
        } else {
            None
        }
    }

    fn can_fit_object(&self, vals: &[ObjectValue], space: usize) -> Option<usize> {
        let num_vals = vals.len();
        if num_vals > self.opts.max_object_pairs_per_line {
            return None;
        }

        let mut remaining = (space as i64) - 2; // For object start/close.
        if !vals.is_empty() {
            // Object padding + (key quotes + colon + padding) * values + (comma + padding) * (values - 1).
            remaining -= 2 + 4 * num_vals as i64 + 2 * (num_vals as i64 - 1);
        }
        if remaining < 0 {
            return None;
        }
        for val in vals {
            match val {
                ObjectValue::Metadata(_) => return None,
                ObjectValue::KeyVal(k, v) => {
                    if !v.comments.is_empty() {
                        return None;
                    }
                    remaining -= k.chars().count() as i64;
                    if remaining < 0 {
                        return None;
                    }
                    match v.token {
                        ValueToken::Array(_) => return None,
                        ValueToken::Object(_) => return None,
                        _ => {}
                    }
                    match self.can_fit_value(&v.token, remaining as usize) {
                        None => return None,
                        Some(size) => {
                            remaining = size as i64;
                        }
                    }
                }
            }
        }

        if remaining >= 0 {
            Some(remaining as usize)
        } else {
            None
        }
    }

    fn can_fit_array(&self, vals: &[ArrayValue], space: usize) -> Option<usize> {
        let num_vals = vals.len();
        if num_vals > self.opts.max_array_values_per_line {
            return None;
        }

        let mut remaining = (space as i64) - 2; // For array start/close.
        if !vals.is_empty() {
            // (comma + padding) * (values - 1).
            remaining -= 2 * (num_vals as i64 - 1);
        }
        if remaining < 0 {
            return None;
        }
        for val in vals {
            match val {
                ArrayValue::Metadata(_) => return None,
                ArrayValue::ArrayVal(v) => {
                    if !v.comments.is_empty() {
                        return None;
                    }
                    match v.token {
                        ValueToken::Array(_) => return None,
                        ValueToken::Object(_) => return None,
                        _ => {}
                    }
                    match self.can_fit_value(&v.token, remaining as usize) {
                        None => return None,
                        Some(size) => {
                            remaining = size as i64;
                        }
                    }
                }
            }
        }

        if remaining >= 0 {
            Some(remaining as usize)
        } else {
            None
        }
    }
}

/// Serializes/formats the provided `Iterator` of [ScanResult]s to the writer.
///
/// This function will ensure that the provided input is validate JSON(C),
/// returning any error encountered.
///
/// Note: It's more efficient to use this function to serialize compact JSON
/// from an input than parsing a [Root] struct and using the
/// [write_json_compact] function.
pub fn write_json_compact_iter<'a, W, I>(w: &mut W, iter: I) -> Result<(), crate::Error>
where
    W: Write,
    I: Iterator<Item = ScanResult<'a>>,
{
    for result in iter.validate() {
        let event = match result {
            Ok(event) => event,
            Err(err) => return Err(err),
        };
        match event.token {
            Token::ObjectStart => w.write_char('{')?,
            Token::ObjectEnd => w.write_char('}')?,
            Token::ArrayStart => w.write_char('[')?,
            Token::ArrayEnd => w.write_char(']')?,
            Token::Comma => w.write_char(',')?,
            Token::Colon => w.write_char(':')?,
            Token::Null => w.write_str("null")?,
            Token::String(v) => {
                w.write_char('"')?;
                w.write_str(v)?;
                w.write_char('"')?;
            }
            Token::Number(v) => w.write_str(v)?,
            Token::Bool(v) => w.write_str(if v { "true" } else { "false" })?,
            _ => {}
        }
    }
    Ok(())
}

/// Serializes/formats the provided JSON [Root] value to the writer as valid
/// JSON.
///
/// The output will be formatted as valid, compact JSON; intended for
/// consumption by computers.
///
/// Note: It's more efficient to use the [write_json_compact_iter] function to
/// serialize compact JSON from an input than parsing a [Root] struct and using
/// this function.
pub fn write_json_compact<W: Write>(w: &mut W, root: &Root) -> Result<(), Error> {
    write_json_value_compact(w, &root.value)
}

fn write_json_value_compact<W: Write>(w: &mut W, value: &Value) -> Result<(), Error> {
    match &value.token {
        ValueToken::Object(vals) => {
            w.write_char('{')?;
            let mut first = true;
            for val in vals {
                if let ObjectValue::KeyVal(k, v) = val {
                    if first {
                        first = false;
                    } else {
                        w.write_char(',')?;
                    }
                    w.write_char('"')?;
                    w.write_str(k)?;
                    w.write_str("\":")?;
                    write_json_value_compact(w, v)?;
                }
            }
            w.write_char('}')?;
        }
        ValueToken::Array(vals) => {
            w.write_char('[')?;
            let mut first = true;
            for val in vals {
                if let ArrayValue::ArrayVal(v) = val {
                    if first {
                        first = false;
                    } else {
                        w.write_char(',')?;
                    }
                    write_json_value_compact(w, v)?;
                }
            }
            w.write_char(']')?;
        }
        ValueToken::String(v) => {
            w.write_char('"')?;
            w.write_str(v)?;
            w.write_char('"')?;
        }
        ValueToken::Number(v) => w.write_str(v)?,
        ValueToken::Bool(v) => {
            if *v {
                w.write_str("true")?;
            } else {
                w.write_str("false")?;
            }
        }
        ValueToken::Null => w.write_str("null")?,
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{ast::parse, scanner::Scanner};

    #[test]
    fn test_format() {
        let input = r#"
        // This is a comment.
        // Second line.

        // Break, than third.

        { // Object start.

            "key1": "val1", // Same line comment.
            "k": "v",
            // Next line comment.
            "arr_key": [ // Array start.

                "val1"
                ,
                100 // Before comma
                ,

                // True.
                true,
            ],

            // And another.
        "key2": { "nested": // And another one.
        100, "value": true, "third": "this"

        // Weird comment before comma.
        , "is": "a", "v":{"another" :"object",}  },
        } // Trailing comment."#;

        let expected_jsonc = r#"// This is a comment.
// Second line.

// Break, than third.

{
  // Object start.

  "key1": "val1", // Same line comment.
  "k": "v",
  // Next line comment.
  "arr_key": [
    // Array start.

    "val1",
    100, // Before comma

    // True.
    true
  ],

  // And another.
  "key2": {
    // And another one.
    "nested": 100,
    "value": true,
    "third": "this",

    // Weird comment before comma.
    "is": "a",
    "v": { "another": "object" }
  }
} // Trailing comment.
"#;
        let root = parse(input).unwrap();

        let mut jsonc = String::new();
        write_jsonc(&mut jsonc, &root).unwrap();
        assert_eq!(&jsonc, expected_jsonc);

        // Parse and reformat the jsonc output. The reformatted output should
        // match the original output.
        let root2 = parse(&jsonc).unwrap();
        let mut jsonc2 = String::new();
        write_jsonc(&mut jsonc2, &root2).unwrap();
        assert_eq!(&jsonc2, &jsonc);

        let expected_json_compact = r#"{"key1":"val1","k":"v","arr_key":["val1",100,true],"key2":{"nested":100,"value":true,"third":"this","is":"a","v":{"another":"object"}}}"#;
        let mut json_compact = String::new();
        write_json_compact(&mut json_compact, &root).unwrap();
        assert_eq!(&json_compact, expected_json_compact);

        // Parse and reformat the json compact output. The reformatted output
        // should match the original output.
        let root2 = parse(&json_compact).unwrap();
        let mut json_compact2 = String::new();
        write_json_compact(&mut json_compact2, &root2).unwrap();
        assert_eq!(&json_compact2, &json_compact);

        let mut json_compact_iter = String::new();
        write_json_compact_iter(&mut json_compact_iter, Scanner::new(input)).unwrap();
        assert_eq!(&json_compact_iter, expected_json_compact);

        let mut json_compact_iter2 = String::new();
        write_json_compact_iter(&mut json_compact_iter2, Scanner::new(&json_compact_iter)).unwrap();
        assert_eq!(&json_compact_iter2, &json_compact_iter);
    }
}