@@ -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)
@@ -454,39 +416,24 @@ SSLMultiCertMarshaller::to_yaml(SSLMultiCertConfig const &config)
454416std::string
455417SSLMultiCertMarshaller::to_json (SSLMultiCertConfig const &config)
456418{
457- std::ostringstream out;
458- out << " {\n \" ssl_multicert\" : [\n " ;
419+ YAML::Emitter json;
420+ json << YAML::DoubleQuoted << YAML::Flow;
421+ json << YAML::BeginMap;
422+ json << YAML::Key << KEY_SSL_MULTICERT << YAML::Value << YAML::BeginSeq;
459423
460- bool first_entry = true ;
461424 for (auto const &entry : config) {
462- if (!first_entry) {
463- out << " ,\n " ;
464- }
465- first_entry = false ;
466-
467- out << " {" ;
468- bool first_field = true ;
425+ json << YAML::BeginMap;
469426
470427 auto write_field = [&](char const *key, std::string const &value) {
471- if (value.empty ()) {
472- return ;
473- }
474- if (!first_field) {
475- out << " , " ;
428+ if (!value.empty ()) {
429+ json << YAML::Key << key << YAML::Value << value;
476430 }
477- first_field = false ;
478- out << json_escape (key) << " : " << json_escape (value);
479431 };
480432
481433 auto write_int_field = [&](char const *key, std::optional<int > const &value) {
482- if (! value.has_value ()) {
483- return ;
434+ if (value.has_value ()) {
435+ json << YAML::Key << key << YAML::Value << value. value () ;
484436 }
485- if (!first_field) {
486- out << " , " ;
487- }
488- first_field = false ;
489- out << json_escape (key) << " : " << value.value ();
490437 };
491438
492439 write_field (KEY_SSL_CERT_NAME, entry.ssl_cert_name );
@@ -500,11 +447,11 @@ SSLMultiCertMarshaller::to_json(SSLMultiCertConfig const &config)
500447 write_int_field (KEY_SSL_TICKET_ENABLED, entry.ssl_ticket_enabled );
501448 write_int_field (KEY_SSL_TICKET_NUMBER, entry.ssl_ticket_number );
502449
503- out << " } " ;
450+ json << YAML::EndMap ;
504451 }
505452
506- out << " \n ] \n } \n " ;
507- return out. str ();
453+ json << YAML::EndSeq << YAML::EndMap ;
454+ return json. c_str ();
508455}
509456
510457} // namespace config
0 commit comments