Skip to main content

oak_json/language/
mod.rs

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