1 import Base 2 import ../json.bend as Json 3 import ./yamlw.bend as W 4 import ./yamlr.bend as R 5 6 # YAML for the JSON data model: `emit` writes block style (yamlw.bend), 7 # `parse` reads the subset cswap writes plus plain block maps, lists and 8 # scalars (yamlr.bend). 9 10 def emit(j: Json.Json) -> String: 11 W.emit(j) 12 13 def flowy(+s: String) -> Bool: 14 String.starts_with(String.trim_start(s), "{") || String.starts_with(String.trim_start(s), "[") 15 16 # JSON is YAML: a document that opens with `{` or `[` is read as JSON when it 17 # is JSON (nested flow collections, quoted keys), else by the block reader. 18 def parse.json(r: Result<&2, &2, String, Json.Json>, +s: String) -> Result<&2, &2, String, Json.Json>: 19 match r: 20 case Done{j}: 21 Done{j} 22 case Fail{e}: 23 R.parse(s) 24 25 def parse.pick(flow: Bool, +s: String) -> Result<&2, &2, String, Json.Json>: 26 match flow: 27 case True{}: 28 parse.json(Json.parse(s), s) 29 case False{}: 30 R.parse(s) 31 32 def parse(+s: String) -> Result<&2, &2, String, Json.Json>: 33 parse.pick(flowy(s), s)