@@ -129,44 +129,6 @@ parse_legacy_line(std::string_view line)
129129 return result;
130130}
131131
132- // / Escape a string for JSON output.
133- std::string
134- json_escape (std::string const &s)
135- {
136- std::string result;
137- result.reserve (s.size () + 2 );
138- result += ' "' ;
139- for (char c : s) {
140- switch (c) {
141- case ' "' :
142- result += " \\\" " ;
143- break ;
144- case ' \\ ' :
145- result += " \\\\ " ;
146- break ;
147- case ' \b ' :
148- result += " \\ b" ;
149- break ;
150- case ' \f ' :
151- result += " \\ f" ;
152- break ;
153- case ' \n ' :
154- result += " \\ n" ;
155- break ;
156- case ' \r ' :
157- result += " \\ r" ;
158- break ;
159- case ' \t ' :
160- result += " \\ t" ;
161- break ;
162- default :
163- result += c;
164- }
165- }
166- result += ' "' ;
167- return result;
168- }
169-
170132// / Escape a string for YAML output if needed.
171133std::string
172134yaml_escape (std::string const &value)
@@ -456,39 +418,24 @@ SSLMultiCertMarshaller::to_yaml(SSLMultiCertConfig const &config)
456418std::string
457419SSLMultiCertMarshaller::to_json (SSLMultiCertConfig const &config)
458420{
459- std::ostringstream out;
460- out << " {\n \" ssl_multicert\" : [\n " ;
421+ YAML::Emitter json;
422+ json << YAML::DoubleQuoted << YAML::Flow;
423+ json << YAML::BeginMap;
424+ json << YAML::Key << KEY_SSL_MULTICERT << YAML::Value << YAML::BeginSeq;
461425
462- bool first_entry = true ;
463426 for (auto const &entry : config) {
464- if (!first_entry) {
465- out << " ,\n " ;
466- }
467- first_entry = false ;
468-
469- out << " {" ;
470- bool first_field = true ;
427+ json << YAML::BeginMap;
471428
472429 auto write_field = [&](char const *key, std::string const &value) {
473- if (value.empty ()) {
474- return ;
475- }
476- if (!first_field) {
477- out << " , " ;
430+ if (!value.empty ()) {
431+ json << YAML::Key << key << YAML::Value << value;
478432 }
479- first_field = false ;
480- out << json_escape (key) << " : " << json_escape (value);
481433 };
482434
483435 auto write_int_field = [&](char const *key, std::optional<int > const &value) {
484- if (! value.has_value ()) {
485- return ;
436+ if (value.has_value ()) {
437+ json << YAML::Key << key << YAML::Value << value. value () ;
486438 }
487- if (!first_field) {
488- out << " , " ;
489- }
490- first_field = false ;
491- out << json_escape (key) << " : " << value.value ();
492439 };
493440
494441 write_field (KEY_SSL_CERT_NAME, entry.ssl_cert_name );
@@ -502,11 +449,11 @@ SSLMultiCertMarshaller::to_json(SSLMultiCertConfig const &config)
502449 write_int_field (KEY_SSL_TICKET_ENABLED, entry.ssl_ticket_enabled );
503450 write_int_field (KEY_SSL_TICKET_NUMBER, entry.ssl_ticket_number );
504451
505- out << " } " ;
452+ json << YAML::EndMap ;
506453 }
507454
508- out << " \n ] \n } \n " ;
509- return out. str ();
455+ json << YAML::EndSeq << YAML::EndMap ;
456+ return json. c_str ();
510457}
511458
512459} // namespace config
0 commit comments