@@ -77,35 +77,69 @@ object Platform {
7777 Error .fail(" Unsupported YAML node type: " + node.getClass.getSimpleName)
7878 }
7979
80- private val docSplitPattern = Pattern .compile(" (?m)^---\\ s*$" )
81-
8280 def yamlToJson (s : String ): ujson.Value = {
83- val docs = docSplitPattern.split(s, - 1 )
84- docs.length match {
81+ // Split YAML multi-document stream manually, similar to SnakeYAML's loadAll
82+ // since parseManyYamls doesn't handle all cases correctly
83+ val documents = splitYamlDocuments(s)
84+
85+ documents.size match {
8586 case 0 => ujson.Null
86- case 1 =>
87- docs.head.asNode match {
88- case Right (n) =>
89- nodeToJson(n)
90- case Left (e) if docs.head.trim.isEmpty =>
91- ujson.Null
92- case Left (e) =>
93- Error .fail(" Error converting YAML to JSON: " + e.getMessage)
94- }
87+ case 1 => parseSingleDocument(documents.head)
9588 case _ =>
96- val buf = new mutable.ArrayBuffer [ujson.Value ](docs.length)
97- for (doc <- docs) {
98- doc.asNode match {
99- case Right (n) => buf += nodeToJson(n)
100- case Left (e) if doc.isEmpty =>
101- case Left (e) =>
102- Error .fail(" Error converting YAML to JSON: " + e.getMessage)
103- }
89+ val buf = new mutable.ArrayBuffer [ujson.Value ](documents.size)
90+ for (doc <- documents) {
91+ buf += parseSingleDocument(doc)
10492 }
10593 ujson.Arr (buf)
10694 }
10795 }
10896
97+ private def splitYamlDocuments (s : String ): List [String ] = {
98+ if (s.trim.isEmpty) return Nil
99+
100+ // Split on document separator "---" at line start
101+ // But only if it's followed by whitespace or end of line
102+ val lines = s.split(" \n " , - 1 ).toList
103+ val documents = mutable.ArrayBuffer [String ]()
104+ val currentDoc = mutable.ArrayBuffer [String ]()
105+ var isFirstDoc = true
106+
107+ for (line <- lines) {
108+ val trimmed = line.trim
109+ // Check if this line starts with "---" and is followed by whitespace or end
110+ if (trimmed.startsWith(" ---" ) && (trimmed.length == 3 || trimmed.charAt(3 ).isWhitespace)) {
111+ // Save previous document if not empty
112+ if (currentDoc.nonEmpty || ! isFirstDoc) {
113+ documents += currentDoc.mkString(" \n " )
114+ }
115+ currentDoc.clear()
116+ isFirstDoc = false
117+ } else {
118+ currentDoc += line
119+ }
120+ }
121+
122+ // Add last document
123+ if (currentDoc.nonEmpty || documents.nonEmpty) {
124+ documents += currentDoc.mkString(" \n " )
125+ }
126+
127+ documents.toList
128+ }
129+
130+ private def parseSingleDocument (doc : String ): ujson.Value = {
131+ val trimmed = doc.trim
132+ if (trimmed.isEmpty) {
133+ ujson.Null
134+ } else {
135+ // Use parseYaml for single document
136+ parseYaml(trimmed) match {
137+ case Right (node) => nodeToJson(node)
138+ case Left (e) => Error .fail(" Error converting YAML to JSON: " + e.getMessage)
139+ }
140+ }
141+ }
142+
109143 private def computeHash (algorithm : String , s : String ) = {
110144 java.security.MessageDigest
111145 .getInstance(algorithm)
0 commit comments