json-colorizer 0.1.2

A fast, lightweight JSON formatter, pretty-printer, colorizer, and query tool for Rust — with jq-style dot-path queries
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
//! # json-colorizer
//!
//! A fast, lightweight JSON formatter, pretty-printer, colorizer, and query library for Rust.
//!
//! Use it to pretty-print JSON with syntax highlighting in the terminal,
//! compact-print JSON, or query nested values with dot-path notation.
//!
//! ## Quick Start
//!
//! ```rust
//! use json_colorizer::{format_json, format_json_compact, query, FormatOptions};
//! use serde_json::json;
//!
//! let value = json!({"name": "Alice", "scores": [95, 87, 100]});
//!
//! // Pretty-print with colors
//! let output = format_json(&value, &FormatOptions::default());
//! println!("{}", output);
//!
//! // Compact output
//! let compact = format_json_compact(&value);
//! println!("{}", compact);
//!
//! // Query a nested value
//! let score = query(&value, ".scores[0]").unwrap();
//! assert_eq!(score, vec![&json!(95)]);
//! ```

use colored::{Color, Colorize};
use serde::Serialize;
use serde_json::ser::{Formatter, PrettyFormatter};
use serde_json::Value;
use std::io::{self, Write};

// ═══════════════════════════════════════════════════════════════
//  Public types
// ═══════════════════════════════════════════════════════════════

/// Options for controlling JSON output formatting.
#[derive(Debug, Clone)]
pub struct FormatOptions {
    /// Number of spaces per indentation level (default: 2).
    pub indent: usize,
    /// Whether to colorize the output (default: true).
    pub color: bool,
    /// Whether to sort object keys (default: false).
    pub sort_keys: bool,
    /// Color theme for the output.
    pub theme: Theme,
}

impl Default for FormatOptions {
    fn default() -> Self {
        Self {
            indent: 2,
            color: true,
            sort_keys: false,
            theme: Theme::default(),
        }
    }
}

/// Colors for different JSON components.
#[derive(Debug, Clone)]
pub struct Theme {
    pub key: Color,
    pub string: Color,
    pub number: Color,
    pub boolean: Color,
    pub null: Color,
}

impl Default for Theme {
    fn default() -> Self {
        Self {
            key: Color::Cyan,
            string: Color::Green,
            number: Color::Yellow,
            boolean: Color::Magenta,
            null: Color::BrightBlack, // Dimmed null
        }
    }
}

/// Error returned when a query path is invalid or does not match the JSON.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum QueryError {
    /// A key was not found in an object.
    KeyNotFound(String),
    /// An index was out of bounds in an array.
    IndexOutOfBounds(usize),
    /// The query string could not be parsed.
    InvalidQuery(String),
}

impl std::fmt::Display for QueryError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            QueryError::KeyNotFound(key) => write!(f, "key '{}' not found", key),
            QueryError::IndexOutOfBounds(idx) => write!(f, "index [{}] out of bounds", idx),
            QueryError::InvalidQuery(msg) => write!(f, "invalid query: {}", msg),
        }
    }
}

impl std::error::Error for QueryError {}

// ═══════════════════════════════════════════════════════════════
//  Core public API
// ═══════════════════════════════════════════════════════════════

pub fn format_json(value: &Value, opts: &FormatOptions) -> String {
    let mut writer = Vec::new();
    let indent_buf = vec![b' '; opts.indent];

    if opts.color {
        let formatter = ColorFormatter::new(&indent_buf, &opts.theme);
        let mut ser = serde_json::Serializer::with_formatter(&mut writer, formatter);
        value.serialize(&mut ser).unwrap();
    } else {
        let formatter = PrettyFormatter::with_indent(&indent_buf);
        let mut ser = serde_json::Serializer::with_formatter(&mut writer, formatter);
        value.serialize(&mut ser).unwrap();
    }

    String::from_utf8(writer).unwrap_or_default()
}

/// Format a JSON value as a compact (single-line, no whitespace) string.
///
/// ```rust
/// use json_colorizer::format_json_compact;
/// use serde_json::json;
///
/// let val = json!({"a": 1});
/// assert_eq!(format_json_compact(&val), r#"{"a":1}"#);
/// ```
pub fn format_json_compact(value: &Value) -> String {
    serde_json::to_string(value).unwrap_or_default()
}

/// Query a nested value inside `root` using a dot-path string.
///
/// Supported syntax:
/// - `.key` — object key access
/// - `[n]` — array index access
/// - Chaining: `.data.users[0].name`
///
/// Query a nested value (or values) inside `root` using a dot-path string.
///
/// Supported syntax:
/// - `.key` or `."quoted key"` — object key access
/// - `[n]` — array index access (0-based)
/// - `.*` — all values in an object
/// - `[]` — all elements in an array
/// - `[start:end]` — array slice (exclusive end)
///
/// Returns a vector of references to matching values.
///
/// ```rust
/// use json_colorizer::query;
/// use serde_json::json;
///
/// let data = json!({"users": [{"name": "Alice"}, {"name": "Bob"}]});
/// let results = query(&data, ".users[].name").unwrap();
/// assert_eq!(results.len(), 2);
/// assert_eq!(results[0], &json!("Alice"));
/// assert_eq!(results[1], &json!("Bob"));
/// ```
pub fn query<'a>(root: &'a Value, path: &str) -> Result<Vec<&'a Value>, QueryError> {
    let segments = parse_query(path)?;
    let mut current_matches = vec![root];

    for seg in &segments {
        let mut next_matches = Vec::new();
        for val in current_matches {
            match seg {
                Segment::Key(key) => {
                    if let Some(v) = val.get(key) {
                        next_matches.push(v);
                    }
                }
                Segment::Index(idx) => {
                    if let Some(v) = val.get(*idx) {
                        next_matches.push(v);
                    }
                }
                Segment::Wildcard => {
                    if let Some(obj) = val.as_object() {
                        for v in obj.values() {
                            next_matches.push(v);
                        }
                    } else if let Some(arr) = val.as_array() {
                        for v in arr {
                            next_matches.push(v);
                        }
                    }
                }
                Segment::Slice(start, end) => {
                    if let Some(arr) = val.as_array() {
                        let start = start.unwrap_or(0);
                        let end = end.unwrap_or(arr.len()).min(arr.len());
                        if start < end {
                            for v in &arr[start..end] {
                                next_matches.push(v);
                            }
                        }
                    }
                }
            }
        }
        if next_matches.is_empty() {
            // If any segment fails to match anything, it's an error for that path branch.
            // But we only return error if NO matches were found at all?
            // jq behavior: if you query .a.b and .a is null, it's an error or null.
            // Let's be strict: if a key is not found, return KeyNotFound.
            // But for wildcards, empty result is fine?
            // Let's check segments:
            match seg {
                Segment::Key(key) if !path.is_empty() => return Err(QueryError::KeyNotFound(key.clone())),
                Segment::Index(idx) => return Err(QueryError::IndexOutOfBounds(*idx)),
                _ => {} // Empty wildcard or slice is fine
            }
        }
        current_matches = next_matches;
    }

    Ok(current_matches)
}

/// Parse a raw JSON string and return the formatted (colorized) output.
///
/// This is a convenience function combining [`serde_json::from_str`] with
/// [`format_json`].
pub fn parse_and_format(json_str: &str, opts: &FormatOptions) -> Result<String, serde_json::Error> {
    let value: Value = serde_json::from_str(json_str)?;
    Ok(format_json(&value, opts))
}

// ═══════════════════════════════════════════════════════════════
//  Query parser internals
// ═══════════════════════════════════════════════════════════════

#[derive(Debug, Clone, PartialEq, Eq)]
enum Segment {
    Key(String),
    Index(usize),
    Wildcard,                   // .* or []
    Slice(Option<usize>, Option<usize>), // [start:end]
}

fn parse_query(query: &str) -> Result<Vec<Segment>, QueryError> {
    let mut segments = Vec::new();
    let q = query.strip_prefix('.').unwrap_or(query);

    if q.is_empty() {
        return Ok(segments);
    }

    let mut chars = q.chars().peekable();
    let mut buf = String::new();

    while let Some(&ch) = chars.peek() {
        match ch {
            '.' => {
                if !buf.is_empty() {
                    segments.push(Segment::Key(buf.clone()));
                    buf.clear();
                }
                chars.next();
                if let Some(&'*') = chars.peek() {
                    segments.push(Segment::Wildcard);
                    chars.next();
                }
            }
            '*' => {
                segments.push(Segment::Wildcard);
                chars.next();
            }
            '[' => {
                if !buf.is_empty() {
                    segments.push(Segment::Key(buf.clone()));
                    buf.clear();
                }
                chars.next(); // consume '['

                let mut idx_buf = String::new();
                let mut found_bracket = false;
                while let Some(&c) = chars.peek() {
                    if c == ']' {
                        chars.next();
                        found_bracket = true;
                        break;
                    }
                    idx_buf.push(c);
                    chars.next();
                }

                if !found_bracket {
                    return Err(QueryError::InvalidQuery("unclosed bracket".to_string()));
                }

                if idx_buf.is_empty() {
                    segments.push(Segment::Wildcard);
                } else if idx_buf.contains(':') {
                    // Slice [start:end]
                    let parts: Vec<&str> = idx_buf.split(':').collect();
                    let start = if parts[0].is_empty() {
                        None
                    } else {
                        Some(parts[0].parse().map_err(|_| {
                            QueryError::InvalidQuery(format!("invalid slice start: {}", parts[0]))
                        })?)
                    };
                    let end = if parts.len() < 2 || parts[1].is_empty() {
                        None
                    } else {
                        Some(parts[1].parse().map_err(|_| {
                            QueryError::InvalidQuery(format!("invalid slice end: {}", parts[1]))
                        })?)
                    };
                    segments.push(Segment::Slice(start, end));
                } else if let Ok(idx) = idx_buf.parse::<usize>() {
                    segments.push(Segment::Index(idx));
                } else {
                    // Quoted key or raw key in brackets
                    let key = idx_buf.trim_matches('"').trim_matches('\'').to_string();
                    segments.push(Segment::Key(key));
                }
            }
            '"' => {
                // Quoted key in dot notation: ."quoted key"
                chars.next(); // consume '"'
                let mut key_buf = String::new();
                let mut found_quote = false;
                while let Some(&c) = chars.peek() {
                    if c == '"' {
                        chars.next();
                        found_quote = true;
                        break;
                    }
                    key_buf.push(c);
                    chars.next();
                }
                if !found_quote {
                    return Err(QueryError::InvalidQuery("unclosed quote".to_string()));
                }
                segments.push(Segment::Key(key_buf));
            }
            _ => {
                buf.push(ch);
                chars.next();
            }
        }
    }

    if !buf.is_empty() {
        segments.push(Segment::Key(buf));
    }

    Ok(segments)
}

// ═══════════════════════════════════════════════════════════════
//  ColorFormatter implementation
// ═══════════════════════════════════════════════════════════════

struct ColorFormatter<'a> {
    pretty: PrettyFormatter<'a>,
    is_key: bool,
    theme: &'a Theme,
}

impl<'a> ColorFormatter<'a> {
    fn new(indent: &'a [u8], theme: &'a Theme) -> Self {
        Self {
            pretty: PrettyFormatter::with_indent(indent),
            is_key: false,
            theme,
        }
    }
}

impl<'a> Formatter for ColorFormatter<'a> {
    #[inline]
    fn begin_array<W: ?Sized + Write>(&mut self, writer: &mut W) -> io::Result<()> {
        self.pretty.begin_array(writer)
    }

    #[inline]
    fn end_array<W: ?Sized + Write>(&mut self, writer: &mut W) -> io::Result<()> {
        self.pretty.end_array(writer)
    }

    #[inline]
    fn begin_array_value<W: ?Sized + Write>(&mut self, writer: &mut W, first: bool) -> io::Result<()> {
        self.pretty.begin_array_value(writer, first)
    }

    #[inline]
    fn end_array_value<W: ?Sized + Write>(&mut self, writer: &mut W) -> io::Result<()> {
        self.pretty.end_array_value(writer)
    }

    #[inline]
    fn begin_object<W: ?Sized + Write>(&mut self, writer: &mut W) -> io::Result<()> {
        self.pretty.begin_object(writer)
    }

    #[inline]
    fn end_object<W: ?Sized + Write>(&mut self, writer: &mut W) -> io::Result<()> {
        self.pretty.end_object(writer)
    }

    #[inline]
    fn begin_object_key<W: ?Sized + Write>(&mut self, writer: &mut W, first: bool) -> io::Result<()> {
        self.is_key = true;
        self.pretty.begin_object_key(writer, first)
    }

    #[inline]
    fn end_object_key<W: ?Sized + Write>(&mut self, writer: &mut W) -> io::Result<()> {
        self.is_key = false;
        self.pretty.end_object_key(writer)
    }

    #[inline]
    fn begin_object_value<W: ?Sized + Write>(&mut self, writer: &mut W) -> io::Result<()> {
        self.pretty.begin_object_value(writer)
    }

    #[inline]
    fn end_object_value<W: ?Sized + Write>(&mut self, writer: &mut W) -> io::Result<()> {
        self.pretty.end_object_value(writer)
    }

    #[inline]
    fn write_null<W: ?Sized + Write>(&mut self, writer: &mut W) -> io::Result<()> {
        writer.write_all("null".color(self.theme.null).to_string().as_bytes())
    }

    #[inline]
    fn write_bool<W: ?Sized + Write>(&mut self, writer: &mut W, value: bool) -> io::Result<()> {
        let s = if value { "true" } else { "false" };
        writer.write_all(s.color(self.theme.boolean).bold().to_string().as_bytes())
    }

    #[inline]
    fn write_i64<W: ?Sized + Write>(&mut self, writer: &mut W, value: i64) -> io::Result<()> {
        writer.write_all(value.to_string().color(self.theme.number).to_string().as_bytes())
    }

    #[inline]
    fn write_u64<W: ?Sized + Write>(&mut self, writer: &mut W, value: u64) -> io::Result<()> {
        writer.write_all(value.to_string().color(self.theme.number).to_string().as_bytes())
    }

    #[inline]
    fn write_f64<W: ?Sized + Write>(&mut self, writer: &mut W, value: f64) -> io::Result<()> {
        writer.write_all(value.to_string().color(self.theme.number).to_string().as_bytes())
    }

    #[inline]
    fn write_string_fragment<W: ?Sized + Write>(&mut self, writer: &mut W, fragment: &str) -> io::Result<()> {
        if self.is_key {
            writer.write_all(fragment.color(self.theme.key).bold().to_string().as_bytes())
        } else {
            writer.write_all(fragment.color(self.theme.string).to_string().as_bytes())
        }
    }

    #[inline]
    fn begin_string<W: ?Sized + Write>(&mut self, writer: &mut W) -> io::Result<()> {
        if self.is_key {
            writer.write_all("\"".color(self.theme.key).bold().to_string().as_bytes())
        } else {
            writer.write_all("\"".color(self.theme.string).to_string().as_bytes())
        }
    }

    #[inline]
    fn end_string<W: ?Sized + Write>(&mut self, writer: &mut W) -> io::Result<()> {
        if self.is_key {
            writer.write_all("\"".color(self.theme.key).bold().to_string().as_bytes())
        } else {
            writer.write_all("\"".color(self.theme.string).to_string().as_bytes())
        }
    }

    #[inline]
    fn write_raw_fragment<W: ?Sized + Write>(&mut self, writer: &mut W, fragment: &str) -> io::Result<()> {
        writer.write_all(fragment.as_bytes())
    }
}

// ═══════════════════════════════════════════════════════════════
//  Tests
// ═══════════════════════════════════════════════════════════════

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn test_compact_output() {
        let val = json!({"a": 1, "b": [2, 3]});
        let out = format_json_compact(&val);
        assert!(out.contains("\"a\":1"));
        assert!(!out.contains('\n'));
    }

    #[test]
    fn test_pretty_plain_output() {
        let val = json!({"name": "Alice"});
        let opts = FormatOptions { indent: 2, color: false, ..FormatOptions::default() };
        let out = format_json(&val, &opts);
        assert!(out.contains("\"name\""));
        assert!(out.contains("\"Alice\""));
        assert!(out.contains('\n'));
    }

    #[test]
    fn test_query_simple_key() {
        let val = json!({"greeting": "hello"});
        let result = query(&val, ".greeting").unwrap();
        assert_eq!(result, vec![&json!("hello")]);
    }

    #[test]
    fn test_query_nested() {
        let val = json!({"a": {"b": {"c": 42}}});
        let result = query(&val, ".a.b.c").unwrap();
        assert_eq!(result, vec![&json!(42)]);
    }

    #[test]
    fn test_query_array_index() {
        let val = json!({"items": [10, 20, 30]});
        let result = query(&val, ".items[1]").unwrap();
        assert_eq!(result, vec![&json!(20)]);
    }

    #[test]
    fn test_query_mixed() {
        let val = json!({"users": [{"name": "Alice"}, {"name": "Bob"}]});
        let result = query(&val, ".users[1].name").unwrap();
        assert_eq!(result, vec![&json!("Bob")]);
    }

    #[test]
    fn test_query_root() {
        let val = json!({"a": 1});
        let result = query(&val, ".").unwrap();
        assert_eq!(result, vec![&val]);
    }

    #[test]
    fn test_query_wildcard_array() {
        let val = json!([1, 2, 3]);
        let result = query(&val, "[]").unwrap();
        assert_eq!(result.len(), 3);
        assert_eq!(result[0], &json!(1));
    }

    #[test]
    fn test_query_wildcard_object() {
        let val = json!({"a": 1, "b": 2});
        let result = query(&val, ".*").unwrap();
        assert_eq!(result.len(), 2);
    }

    #[test]
    fn test_query_slice() {
        let val = json!([0, 1, 2, 3, 4]);
        let result = query(&val, "[1:4]").unwrap();
        assert_eq!(result.len(), 3);
        assert_eq!(result[0], &json!(1));
        assert_eq!(result[2], &json!(3));
    }

    #[test]
    fn test_query_quoted_key() {
        let val = json!({"key with spaces": "val"});
        let result = query(&val, ".\"key with spaces\"").unwrap();
        assert_eq!(result, vec![&json!("val")]);

        let result2 = query(&val, "[\"key with spaces\"]").unwrap();
        assert_eq!(result2, vec![&json!("val")]);
    }

    #[test]
    fn test_query_key_not_found() {
        let val = json!({"a": 1});
        let err = query(&val, ".b").unwrap_err();
        assert!(matches!(err, QueryError::KeyNotFound(_)));
    }

    #[test]
    fn test_query_index_out_of_bounds() {
        let val = json!([1, 2]);
        let err = query(&val, "[5]").unwrap_err();
        assert!(matches!(err, QueryError::IndexOutOfBounds(5)));
    }

    #[test]
    fn test_parse_and_format() {
        let json_str = r#"{"key": "value"}"#;
        let opts = FormatOptions { indent: 2, color: false, ..FormatOptions::default() };
        let result = parse_and_format(json_str, &opts).unwrap();
        assert!(result.contains("\"key\""));
    }

    #[test]
    fn test_parse_and_format_invalid() {
        let opts = FormatOptions::default();
        assert!(parse_and_format("not json", &opts).is_err());
    }

    #[test]
    fn test_empty_object_and_array() {
        let opts = FormatOptions { indent: 2, color: false, ..FormatOptions::default() };
        assert_eq!(format_json(&json!({}), &opts), "{}");
        assert_eq!(format_json(&json!([]), &opts), "[]");
    }

    #[test]
    fn test_escape_special_characters() {
        let val = json!({"msg": "line1\nline2\ttab"});
        let opts = FormatOptions { indent: 2, color: false, ..FormatOptions::default() };
        let out = format_json(&val, &opts);
        assert!(out.contains("\\n"));
        assert!(out.contains("\\t"));
    }
}