Skip to main content

qubit_json/
json_decode_options.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2026.
4 *    Haixing Hu, Qubit Co. Ltd.
5 *
6 *    All rights reserved.
7 *
8 ******************************************************************************/
9//! Defines the option type used to configure the lenient JSON decoder.
10//!
11//! Author: Haixing Hu
12
13/// Configuration switches for [`crate::LenientJsonDecoder`].
14///
15/// Each flag controls one normalization rule applied before parsing JSON.
16/// Defaults are intentionally conservative and cover the most common
17/// non-fully-trusted text inputs without attempting aggressive repair.
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19pub struct JsonDecodeOptions {
20    /// Controls whether leading and trailing whitespace is removed before any
21    /// other normalization step is applied.
22    pub trim_whitespace: bool,
23    /// Controls whether a leading UTF-8 byte order mark (`U+FEFF`) is removed
24    /// before parsing.
25    pub strip_utf8_bom: bool,
26    /// Controls whether one outer Markdown code fence is removed.
27    ///
28    /// Typical examples include `````json ... ````` and bare fenced blocks
29    /// starting with ````` followed by a newline.
30    pub strip_markdown_code_fence: bool,
31    /// Controls whether Markdown fence stripping requires a matching closing
32    /// fence.
33    ///
34    /// When enabled, an opening fence without a valid closing fence keeps the
35    /// input unchanged.
36    pub strip_markdown_code_fence_requires_closing: bool,
37    /// Controls whether Markdown fence stripping only accepts JSON-like
38    /// language tags (`json`, `jsonc`, or empty tag).
39    ///
40    /// When enabled, fenced blocks labeled with other languages are not
41    /// stripped.
42    pub strip_markdown_code_fence_json_only: bool,
43    /// Controls whether raw ASCII control characters inside JSON string
44    /// literals are converted into valid JSON escape sequences.
45    pub escape_control_chars_in_strings: bool,
46    /// Caps the accepted raw input size in bytes before normalization.
47    ///
48    /// When set to `Some(limit)`, any input whose byte length is greater than
49    /// `limit` is rejected before further processing. When set to `None`,
50    /// no size limit is enforced.
51    pub max_input_bytes: Option<usize>,
52}
53
54impl Default for JsonDecodeOptions {
55    fn default() -> Self {
56        Self {
57            trim_whitespace: true,
58            strip_utf8_bom: true,
59            strip_markdown_code_fence: true,
60            strip_markdown_code_fence_requires_closing: false,
61            strip_markdown_code_fence_json_only: false,
62            escape_control_chars_in_strings: true,
63            max_input_bytes: None,
64        }
65    }
66}