ghodss/yaml

Newline and carriage return control characters characters are not escaped when converting YAML to JSON

dawu415 opened this issue · 1 comments

Currently, control characters such as newlines and carriage returns are not escaped and passed through.

e.g.
When one inputs the following byte data into YAMLToJSON()

{"name": "-----BEGIN CERTIFICATE-----
MIIC3aaaaaaaDAwMQswCQYDVQQGxxxxxxxsjlsdkfjsdlkfjsdlkjsdvlskdEwJB
VTENMAsGA1UECwwEZGF3dTESMBAGA1UEAwwJKi5kYXd1LmlvMB4XDTE5MDYwNjAz
MTEzNVoXDTIwMDYwNTAzMTEzNVowMDELMAkGA1UEBhMCQVUxDTALBgNVBAsMBGRh
d3UxEjAQBgNVBAMMCSouZGF3dS5pbzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC
AQoCggEBANBuhemwaaaaaaaaaaaaaaaaaaajCj+Yo20QhidabJ8SgrrSFJEz/btm
UQATAqLXOczhIqxJzIOIhW2klI4JQTkR96FZkqCOktKtQNjXrL+5L2Jb7ouHLak7
8j3HjhCmmfgh7MZ1wq7O+MUyDCEHI5ixd8eC8TEr1XgCJyhqlYestsWhvGUP8gth
DGgFHMBOx033RvVkTbJcagSspMsQZ/sCAwEAATANBgkqhkiG9w0BAQsFAAOCAQEA
bocw2bBJfb2uvK0Ej5PpTQPBnynVsJxBvCJlSAqA7Mk9NieRSbdj7Ul+nRViSwER
41bTogRS4mhl3qaLGThB4DlsS/wqRp5ocTqBMWkCssEdCWwkJVWZxPQ42Kh5uXYY
9JsEudPLv0hh+b5Oyoi7f2asljasflkasjflkGSqEJH2xauwfZnKTjeb/tmtqtoi
coL/1owTdHspg59SQYUBJUPlc0Ids6OolsmOnO9icwJ7c1AIkpKps4mL9XgxCtD7
BVRmQ5RwTOlBLAmpU7R1gg==
-----END CERTIFICATE-----", "age": 30}

the byte output is the following

{"age":30,"name":"-----BEGIN CERTIFICATE----- MIIC3aaaaaaaDAwMQswCQYDVQQGxxxxxxxsjlsdkfjsdlkfjsdlkjsdvlskdEwJB VTENMAsGA1UECwwEZGF3dTESMBAGA1UEAwwJKi5kYXd1LmlvMB4XDTE5MDYwNjAz MTEzNVoXDTIwMDYwNTAzMTEzNVowMDELMAkGA1UEBhMCQVUxDTALBgNVBAsMBGRh d3UxEjAQBgNVBAMMCSouZGF3dS5pbzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC AQoCggEBANBuhemwaaaaaaaaaaaaaaaaaaajCj+Yo20QhidabJ8SgrrSFJEz/btm UQATAqLXOczhIqxJzIOIhW2klI4JQTkR96FZkqCOktKtQNjXrL+5L2Jb7ouHLak7 8j3HjhCmmfgh7MZ1wq7O+MUyDCEHI5ixd8eC8TEr1XgCJyhqlYestsWhvGUP8gth DGgFHMBOx033RvVkTbJcagSspMsQZ/sCAwEAATANBgkqhkiG9w0BAQsFAAOCAQEA bocw2bBJfb2uvK0Ej5PpTQPBnynVsJxBvCJlSAqA7Mk9NieRSbdj7Ul+nRViSwER 41bTogRS4mhl3qaLGThB4DlsS/wqRp5ocTqBMWkCssEdCWwkJVWZxPQ42Kh5uXYY 9JsEudPLv0hh+b5Oyoi7f2asljasflkasjflkGSqEJH2xauwfZnKTjeb/tmtqtoi coL/1owTdHspg59SQYUBJUPlc0Ids6OolsmOnO9icwJ7c1AIkpKps4mL9XgxCtD7 BVRmQ5RwTOlBLAmpU7R1gg== -----END CERTIFICATE-----"}

An example of this can be found at this playground
https://play.golang.org/p/1SFyCUgmMBN

It would be nice if newline and carriage return characters can be escaped after running YAMLToJSON()
i.e.,

{"age": 30, "name": "-----BEGIN CERTIFICATE-----\nMIIC3aaaaaaaDAwMQswCQYDVQQGxxxxxxxsjlsdkfjsdlkfjsdlkjsdvlskdEwJB\nVTENMAsGA1UECwwEZGF3dTESMBAGA1UEAwwJKi5kYXd1LmlvMB4XDTE5MDYwNjAz\nMTEzNVoXDTIwMDYwNTAzMTEzNVowMDELMAkGA1UEBhMCQVUxDTALBgNVBAsMBGRh\nd3UxEjAQBgNVBAMMCSouZGF3dS5pbzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC\nAQoCggEBANBuhemwaaaaaaaaaaaaaaaaaaajCj+Yo20QhidabJ8SgrrSFJEz/btm\nUQATAqLXOczhIqxJzIOIhW2klI4JQTkR96FZkqCOktKtQNjXrL+5L2Jb7ouHLak7\n8j3HjhCmmfgh7MZ1wq7O+MUyDCEHI5ixd8eC8TEr1XgCJyhqlYestsWhvGUP8gth\nDGgFHMBOx033RvVkTbJcagSspMsQZ/sCAwEAATANBgkqhkiG9w0BAQsFAAOCAQEA\nbocw2bBJfb2uvK0Ej5PpTQPBnynVsJxBvCJlSAqA7Mk9NieRSbdj7Ul+nRViSwER\n41bTogRS4mhl3qaLGThB4DlsS/wqRp5ocTqBMWkCssEdCWwkJVWZxPQ42Kh5uXYY\n9JsEudPLv0hh+b5Oyoi7f2asljasflkasjflkGSqEJH2xauwfZnKTjeb/tmtqtoi\ncoL/1owTdHspg59SQYUBJUPlc0Ids6OolsmOnO9icwJ7c1AIkpKps4mL9XgxCtD7\nBVRmQ5RwTOlBLAmpU7R1gg==\n-----END CERTIFICATE-----"}

这个问题还没有得到解决吗?我也想保留yaml的换行和被注释的内容