Skip to content

Commit

Permalink
add a valueToQuotedString overload (#1397)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

---------

Co-authored-by: Billy Donahue <[email protected]>
Co-authored-by: Jordan Bayles <[email protected]>
  • Loading branch information
3 people committed Sep 10, 2024
1 parent e1a3c64 commit 034976a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions include/json/writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>>()
Expand Down
8 changes: 6 additions & 2 deletions src/lib_json/json_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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()) {
Expand Down

0 comments on commit 034976a

Please sign in to comment.