Qubit JSON
Lenient JSON decoder for Rust, designed for non-fully-trusted text inputs.
Overview
Qubit JSON provides a small and predictable decoding layer on top of
serde_json. Its core type, LenientJsonDecoder, normalizes a limited set of
common input issues before parsing and deserializing JSON values.
The crate is intended for cases where JSON text may come from sources such as:
- Markdown-wrapped text
- Markdown code blocks
- copied snippets
- CLI output streams
- other text channels that may wrap otherwise valid JSON
It is intentionally narrow. The crate does not try to be a general JSON repair engine, and it does not attempt to guess missing quotes, commas, or braces.
Design Goals
- Lenient but predictable: only handle a small set of well-defined input problems
- Object-oriented API: use a reusable
LenientJsonDecoderinstance instead of a loose bag of helper functions - Serde-first: delegate actual parsing and deserialization to
serde_json - Clear errors: report stable error kinds with enough context for callers
- Low overhead: avoid unnecessary allocation when normalization can borrow the original input
Features
LenientJsonDecoder
- Reusable decoder object that holds immutable decoding options
decode<T>(): decodes any JSON top-level value intoTdecode_value(): decodes intoserde_json::Valuedecode_object<T>(): requires a top-level JSON objectdecode_array<T>(): requires a top-level JSON array
JsonDecodeOptions
trim_whitespace: trims leading and trailing whitespacestrip_utf8_bom: strips a leading UTF-8 BOMstrip_markdown_code_fence: strips one outer Markdown code fenceescape_control_chars_in_strings: escapes ASCII control characters inside JSON string literals
Explicit Error Model
EmptyInput: input becomes empty after normalizationInvalidJson: normalized text is not valid JSON syntaxUnexpectedTopLevel: top-level JSON kind does not match the requested methodDeserialize: JSON is valid but cannot be deserialized into the target type
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Quick Start
Decode a JSON Object from a Markdown Code Fence
use Deserialize;
use LenientJsonDecoder;
Decode JSON Containing Raw Control Characters in Strings
use LenientJsonDecoder;
Customize Decoder Options
use ;
Normalization Rules
When enabled, the decoder applies the following pipeline before parsing:
- validate that the input is not empty
- trim surrounding whitespace
- strip a leading UTF-8 BOM
- strip one outer Markdown code fence
- escape ASCII control characters inside JSON string literals
The decoder does not:
- add missing quotes
- add missing commas
- add missing braces or brackets
- rewrite arbitrary malformed JSON into guessed valid JSON
When to Use
Qubit JSON is a good fit when:
- you need a reusable, configurable JSON decoder object
- your inputs are mostly valid JSON but may be wrapped or slightly noisy
- you want stable error categories around
serde_json
It is not a good fit when:
- you need aggressive repair for heavily malformed JSON
- your inputs are not actually JSON
- a plain
serde_json::from_str()call is already sufficient
License
This project is licensed under the Apache 2.0 License. See LICENSE for details.
Alignment Notes
This README reflects the current object model:
LenientJsonDecoderowns an internalLenientJsonNormalizer.- Public decoding APIs are
decode,decode_object,decode_array,decode_value. - Normalization and error handling are implemented in
src/lenient_json_normalizer.rsandsrc/json_decode_error.rs, which are covered by tests intests/. - Product requirements and implementation behavior are aligned with
doc/json_prd.zh_CN.mdanddoc/json_design.zh_CN.md.