Skip to content

Commit

Permalink
flexbuffers_binary_to_json outputs regular utf8 instead of escaped ve…
Browse files Browse the repository at this point in the history
…rsions
  • Loading branch information
aardappel committed Oct 12, 2024
1 parent c6ce8ec commit 02fd3e6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
25 changes: 15 additions & 10 deletions dev/include/flatbuffers/flexbuffers.h
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@ inline void IndentString(std::string &s, int indent,

template<typename T>
void AppendToString(std::string &s, T &&v, bool keys_quoted, bool indented,
int cur_indent, const char *indent_string) {
int cur_indent, const char *indent_string,
bool natural_utf8) {
s += "[";
s += indented ? "\n" : " ";
for (size_t i = 0; i < v.size(); i++) {
Expand All @@ -377,7 +378,7 @@ void AppendToString(std::string &s, T &&v, bool keys_quoted, bool indented,
}
if (indented) IndentString(s, cur_indent, indent_string);
v[i].ToString(true, keys_quoted, s, indented, cur_indent,
indent_string);
indent_string, natural_utf8);
}
if (indented) {
s += "\n";
Expand Down Expand Up @@ -567,23 +568,24 @@ class Reference {
// string values at the top level receive "" quotes (inside other values
// they always do). keys_quoted determines if keys are quoted, at any level.
void ToString(bool strings_quoted, bool keys_quoted, std::string &s) const {
ToString(strings_quoted, keys_quoted, s, false, 0, "");
ToString(strings_quoted, keys_quoted, s, false, 0, "", false);
}

// This version additionally allow you to specify if you want indentation.
void ToString(bool strings_quoted, bool keys_quoted, std::string &s,
bool indented, int cur_indent, const char *indent_string) const {
bool indented, int cur_indent, const char *indent_string,
bool natural_utf8 = false) const {
if (type_ == FBT_STRING) {
String str(Indirect(), byte_width_);
if (strings_quoted) {
flatbuffers::EscapeString(str.c_str(), str.length(), &s, true, false);
flatbuffers::EscapeString(str.c_str(), str.length(), &s, true, natural_utf8);
} else {
s.append(str.c_str(), str.length());
}
} else if (IsKey()) {
auto str = AsKey();
if (keys_quoted) {
flatbuffers::EscapeString(str, strlen(str), &s, true, false);
flatbuffers::EscapeString(str, strlen(str), &s, true, natural_utf8);
} else {
s += str;
}
Expand Down Expand Up @@ -623,7 +625,8 @@ class Reference {
if (indented) IndentString(s, cur_indent + 1, indent_string);
keys[i].ToString(true, kq, s);
s += ": ";
vals[i].ToString(true, keys_quoted, s, indented, cur_indent + 1, indent_string);
vals[i].ToString(true, keys_quoted, s, indented, cur_indent + 1, indent_string,
natural_utf8);
if (i < keys.size() - 1) {
s += ",";
if (!indented) s += " ";
Expand All @@ -635,13 +638,15 @@ class Reference {
s += "}";
} else if (IsVector()) {
AppendToString<Vector>(s, AsVector(), keys_quoted, indented,
cur_indent + 1, indent_string);
cur_indent + 1, indent_string, natural_utf8);
} else if (IsTypedVector()) {
AppendToString<TypedVector>(s, AsTypedVector(), keys_quoted, indented,
cur_indent + 1, indent_string);
cur_indent + 1, indent_string,
natural_utf8);
} else if (IsFixedTypedVector()) {
AppendToString<FixedTypedVector>(s, AsFixedTypedVector(), keys_quoted,
indented, cur_indent + 1, indent_string);
indented, cur_indent + 1, indent_string,
natural_utf8);
} else if (IsBlob()) {
auto blob = AsBlob();
flatbuffers::EscapeString(reinterpret_cast<const char *>(blob.data()),
Expand Down
2 changes: 1 addition & 1 deletion dev/src/lobsterreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ nfr("flexbuffers_binary_to_json", "flex,field_quotes,indent_string", "SBS", "S?S
if (flexbuffers::VerifyBuffer((const uint8_t *)fsv.data(), fsv.size(), &reuse_buffer)) {
auto root = flexbuffers::GetRoot((const uint8_t *)fsv.data(), fsv.size());
string json;
root.ToString(true, quoted, json, indent_string.size() != 0, 0, indent_string.c_str());
root.ToString(true, quoted, json, indent_string.size() != 0, 0, indent_string.c_str(), true);
auto s = vm.NewString(json);
Push(sp, s);
Push(sp, NilVal());
Expand Down

0 comments on commit 02fd3e6

Please sign in to comment.