From ab54e61805321eaea8f0e886531c4f5aa79773c7 Mon Sep 17 00:00:00 2001 From: Shivendra Agarwal Date: Mon, 12 Nov 2018 22:19:11 +0530 Subject: [PATCH] FlexBuffer to JSON convertor for typed and fixedTypedvectors (#4947) * FlexBuffer to JSON convertor for typed and fixedTypedvectors * moving the common implementation to template * signed unsigned comparison fix * fix a formatting ({ * changing logic to append comma in vector of elements in json --- include/flatbuffers/flexbuffers.h | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/include/flatbuffers/flexbuffers.h b/include/flatbuffers/flexbuffers.h index cea9d8eb0..de010b92f 100644 --- a/include/flatbuffers/flexbuffers.h +++ b/include/flatbuffers/flexbuffers.h @@ -337,6 +337,16 @@ class Map : public Vector { bool IsTheEmptyMap() const { return data_ == EmptyMap().data_; } }; +template +void AppendToString(std::string &s, T &&v, bool keys_quoted) { + s += "[ "; + for (size_t i = 0; i < v.size(); i++) { + if (i) s += ", "; + v[i].ToString(true, keys_quoted, s); + } + s += " ]"; +} + class Reference { public: Reference(const uint8_t *data, uint8_t parent_width, uint8_t byte_width, @@ -532,13 +542,11 @@ class Reference { } s += " }"; } else if (IsVector()) { - s += "[ "; - auto v = AsVector(); - for (size_t i = 0; i < v.size(); i++) { - v[i].ToString(true, keys_quoted, s); - if (i < v.size() - 1) s += ", "; - } - s += " ]"; + AppendToString(s, AsVector(), keys_quoted); + } else if (IsTypedVector()) { + AppendToString(s, AsTypedVector(), keys_quoted); + } else if (IsFixedTypedVector()) { + AppendToString(s, AsFixedTypedVector(), keys_quoted); } else { s += "(?)"; }