Skip to main content

oak_json/language/
mod.rs

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