PureYAML/Sources/PureYAMLJSON/JSON.Parse.swift
Mihaela Mihaljevic 27cc8cbfbb
Some checks failed
rules-freshness / rules (push) Failing after 12m24s
fix(json): keep every character of a JSON string through the trip
The JSON writer escapes the characters a YAML reader mangles. PureYAML.JSON.parse and parseLocated read JSON with those characters held back and given back, so no position moves, and Convert.convert takes the format of its source.
2026-11-15 12:12:44 +01:00

22 lines
1.3 KiB
Swift

import PureYAML
public extension PureYAML.JSON {
/// Reads text that is JSON into the shared value tree.
///
/// JSON is a subset of YAML 1.2 apart from a few characters that RFC 8259 lets a string hold as they are and the YAML reader does not: it folds U+0085, U+2028 and
/// U+2029 as line breaks, it refuses U+007F, the C1 controls, U+FFFE and U+FFFF, and so a string that holds one comes back from ``PureYAML/parse(_:)`` as another
/// string or not at all. Here each such character inside a string is held back from the reader and given back in the value, so the string is the one the JSON
/// means. Everything else is read by the YAML reader as it is, and what is not JSON is refused by it as before.
static func parse(_ text: String) throws -> PureYAML.Model.Value {
let source = try Source(text)
return try source.restoring(PureYAML.parse(source.masked))
}
/// As ``parse(_:)``, with the span of every node. The spans are those of the text as it is: a held back character is one scalar in the text the reader sees, as it is
/// in `text`, so no position moves.
static func parseLocated(_ text: String) throws -> PureYAML.Model.LocatedValue {
let source = try Source(text)
return try source.restoring(PureYAML.parseLocated(source.masked))
}
}