@@ -362,8 +362,25 @@ func writeJSONScalar(ctx *jsonWriteContext, node *yaml.Node) error {
362362 ctx .write (quoteJSONString (node .Value ))
363363 return nil
364364
365- case "!!int" , "!!float" :
366- // Numbers - write as-is
365+ case "!!int" :
366+ // Check for invalid JSON number formats (e.g., leading zeros)
367+ // In YAML, "009911" might be parsed as int, but JSON doesn't allow leading zeros
368+ // Treat such values as strings to preserve them correctly
369+ if hasInvalidJSONNumberFormat (node .Value ) {
370+ ctx .write (quoteJSONString (node .Value ))
371+ return nil
372+ }
373+ ctx .write (node .Value )
374+ return nil
375+
376+ case "!!float" :
377+ // Check for invalid JSON number formats (e.g., leading zeros)
378+ // YAML may tag values like "009911" as float, but they're invalid in JSON
379+ // Treat such values as strings to preserve them correctly
380+ if hasInvalidJSONNumberFormat (node .Value ) {
381+ ctx .write (quoteJSONString (node .Value ))
382+ return nil
383+ }
367384 ctx .write (node .Value )
368385 return nil
369386
@@ -384,6 +401,31 @@ func writeJSONScalar(ctx *jsonWriteContext, node *yaml.Node) error {
384401 }
385402}
386403
404+ // hasInvalidJSONNumberFormat checks if a numeric string would be invalid in JSON.
405+ // JSON numbers cannot have leading zeros (except for "0" itself or "0.x" floats).
406+ func hasInvalidJSONNumberFormat (value string ) bool {
407+ if len (value ) < 2 {
408+ return false
409+ }
410+
411+ // Handle negative numbers
412+ start := 0
413+ if value [0 ] == '-' || value [0 ] == '+' {
414+ start = 1
415+ if len (value ) < 2 {
416+ return false
417+ }
418+ }
419+
420+ // Check for leading zero followed by more digits (invalid in JSON)
421+ // "0" alone is valid, "0.x" is valid, but "00", "01", "007" etc. are not
422+ if value [start ] == '0' && len (value ) > start + 1 && value [start + 1 ] >= '0' && value [start + 1 ] <= '9' {
423+ return true
424+ }
425+
426+ return false
427+ }
428+
387429// resolveMergeKeys processes YAML merge keys (<<) and returns content with merged values
388430func resolveMergeKeys (content []* yaml.Node ) []* yaml.Node {
389431 if len (content ) == 0 {
0 commit comments