Skip to main content

yaml_to_json

Function yaml_to_json 

Source
pub fn yaml_to_json(yaml: &str) -> Result<String, Box<dyn Error>>
Expand description

Converts a YAML sequence of mappings into a JSON array of objects.

Non-string YAML keys (e.g. 1:) are converted to their string form ("1"), matching the convention of every mainstream YAML-to-JSON converter. Collisions are not detected; if the source YAML contains both 1: and "1":, one value wins.

This function parses YAML with a strict schema to avoid the “Norway problem”: yes, no, on, off, y, n are treated as strings under YAML 1.2 semantics. Nested sequences or mappings cause an error because they cannot round-trip losslessly through flat CSV/TSV.

§Errors

Returns an error if the YAML is not a sequence of mappings, contains nested values, or if JSON serialization fails.

§Examples

use sheeter_engine::yaml_to_json;

let yaml = "- name: Alice\n  age: '30'\n- name: Bob\n  age: '25'\n";
let json = yaml_to_json(yaml).unwrap();
assert_eq!(json, r#"[{"name":"Alice","age":"30"},{"name":"Bob","age":"25"}]"#);