From 034976a19dbd7ffe37aa1c3821855d4cde868fcb Mon Sep 17 00:00:00 2001 From: Philip Top Date: Mon, 9 Sep 2024 17:38:22 -0700 Subject: [PATCH] add a valueToQuotedString overload (#1397) * add a valueToQuotedString overload to take a string length to support things like a string_view more directly. * Apply suggestions from code review Co-authored-by: Billy Donahue --------- Co-authored-by: Billy Donahue Co-authored-by: Jordan Bayles --- include/json/writer.h | 1 + src/lib_json/json_writer.cpp | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/include/json/writer.h b/include/json/writer.h index 655ebff..7c56a21 100644 --- a/include/json/writer.h +++ b/include/json/writer.h @@ -351,6 +351,7 @@ String JSON_API valueToString( PrecisionType precisionType = PrecisionType::significantDigits); String JSON_API valueToString(bool value); String JSON_API valueToQuotedString(const char* value); +String JSON_API valueToQuotedString(const char* value, size_t length); /// \brief Output using the StyledStreamWriter. /// \see Json::operator>>() diff --git a/src/lib_json/json_writer.cpp b/src/lib_json/json_writer.cpp index 239c429..5bb5dd1 100644 --- a/src/lib_json/json_writer.cpp +++ b/src/lib_json/json_writer.cpp @@ -354,6 +354,10 @@ String valueToQuotedString(const char* value) { return valueToQuotedStringN(value, strlen(value)); } +String valueToQuotedString(const char* value, size_t length) { + return valueToQuotedStringN(value, length); +} + // Class Writer // ////////////////////////////////////////////////////////////////// Writer::~Writer() = default; @@ -491,7 +495,7 @@ void StyledWriter::writeValue(const Value& value) { const String& name = *it; const Value& childValue = value[name]; writeCommentBeforeValue(childValue); - writeWithIndent(valueToQuotedString(name.c_str())); + writeWithIndent(valueToQuotedString(name.c_str(), name.size())); document_ += " : "; writeValue(childValue); if (++it == members.end()) { @@ -709,7 +713,7 @@ void StyledStreamWriter::writeValue(const Value& value) { const String& name = *it; const Value& childValue = value[name]; writeCommentBeforeValue(childValue); - writeWithIndent(valueToQuotedString(name.c_str())); + writeWithIndent(valueToQuotedString(name.c_str(), name.size())); *document_ << " : "; writeValue(childValue); if (++it == members.end()) {