Skip to main content

oak_json/language/
mod.rs

1#![doc = include_str!("readme.md")]
2#[cfg(feature = "serde")]
3use ::serde::{Deserialize, Serialize};
4use oak_core::language::{Language, LanguageCategory};
5#[cfg(feature = "serde")]
6pub mod serde_impl;
7#[cfg(feature = "serde")]
8pub use serde_impl::{from_value, to_value};
9
10/// JSON language implementation
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
13pub struct JsonLanguage {
14    /// Whether to allow trailing commas in objects and arrays
15    pub trailing_comma: bool,
16    /// Whether to allow bare keys (unquoted keys) in objects
17    pub bare_keys: bool,
18    /// Whether to allow single-quoted strings
19    pub single_quotes: bool,
20    /// Whether to allow comments (both line and block)
21    pub comments: bool,
22    /// Whether to allow hexadecimal numbers (e.g., 0xDEADBEEF)
23    pub hex_numbers: bool,
24    /// Whether to allow Infinity, -Infinity, and NaN
25    pub infinity_and_nan: bool,
26}
27
28impl JsonLanguage {
29    /// Creates a new JSON language instance with default settings.
30    pub fn new() -> Self {
31        Self::default()
32    }
33
34    /// Creates a standard JSON language instance (no extensions).
35    pub fn standard() -> Self {
36        Self::default()
37    }
38
39    /// Creates a JSON5 language instance with all extensions enabled.
40    pub fn json5() -> Self {
41        Self { trailing_comma: true, bare_keys: true, single_quotes: true, comments: true, hex_numbers: true, infinity_and_nan: true }
42    }
43
44    /// Creates a relaxed JSON language instance with all extensions enabled.
45    pub fn relaxed() -> Self {
46        Self { trailing_comma: true, bare_keys: true, single_quotes: true, comments: true, hex_numbers: true, infinity_and_nan: true }
47    }
48}
49
50impl Default for JsonLanguage {
51    fn default() -> Self {
52        Self { trailing_comma: false, bare_keys: false, single_quotes: false, comments: false, hex_numbers: false, infinity_and_nan: false }
53    }
54}
55
56impl Language for JsonLanguage {
57    const NAME: &'static str = "json";
58    const CATEGORY: LanguageCategory = LanguageCategory::Config;
59
60    type TokenType = crate::lexer::token_type::JsonTokenType;
61    type ElementType = crate::parser::element_type::JsonElementType;
62    type TypedRoot = crate::ast::JsonRoot;
63}