English
YAML and JSON
Code: libs/yaml (ours-yaml), libs/json (ours-json).
The tree
The common format for all parts of the configuration is the ours_json tree:
rust
pub struct Node { pub value: Value, pub span: Span }
pub enum Value { Null, Bool(bool), Number(Number), String(String), Array(Vec<Node>), Object(Vec<Member>) }
pub struct Member { pub key: String, pub key_span: Span, pub value: Node }
pub struct Span { pub start: usize, pub end: usize } // bytes of the source text- A map is a
Vec<Member>, the key order is preserved: the order of entities and rules depends on it. Numberstores the literal (from_literal,as_str): neither precision nor notation is lost until decoding into a concrete type.- Every value and every key has a span;
Span::line_column(source)turns it into a line and column.Node::new(value)gives a zero span — for values that are not in the text (the canonical form, synthetic nodes).
The same tree is the canonical form Decoded::canonical (only Value, without spans) and the codec's input. There is no separate "native configuration" type.
YAML: ours_yaml::parse
Parsing with saphyr-parser (YAML 1.2, events) and turning the result into a Node. Restrictions — a refusal with a position:
| What | Message |
|---|---|
an anchor &a | anchors are not allowed: use `defaults` for repeated values |
an alias *a | aliases are not allowed: use `defaults` for repeated values |
the merge key << | merge keys are not allowed: use `defaults` ... |
a tag !!str, !x | tags are not allowed |
| a non-string key | a key must be a string |
| a repeated key | repeated key `x` + (first at line:column) |
| several documents | a configuration is one YAML document |
.inf, .nan | infinity and NaN are not valid values |
Why: the dashboard and check --view show configuration objects, while anchors and merges disappear during parsing; repetition is handled by outbounds.defaults, which is visible as an object. A repeated key would be merged silently in a Go-compatible codec — here it is forbidden at the parsing level.
Scalars follow the YAML 1.2 core schema, and only plain ones are typed: null/~/empty — Null, true/false (three cases) — Bool, decimal, 0x, 0o, exponent — Number; everything else, and any quoted or block scalar, is a String. An empty document is Null (an empty configuration).
Spans: the parser counts characters, CharOffsets turns them into byte offsets.
ours_yaml::Error { span, first, message } is the loader's common error type, not only for parsing (ours_native_config::Error is its re-export); see errors.
JSON: ours_json
libs/json is JSON the way sing-box reads and writes it:
parse— accepts//,#,/* */comments and trailing commas; every value with a span. The product does not read a configuration in JSON; JSON means rule-set files in sing-box's source format, subscription responses (Xray JSON) and compatibility tests.to_string_pretty— the same text that Go'sencoding/jsongives with a two-space indent. It serializes the canonical form for the reload fingerprint,check --view, the Admin API.