summaryrefslogtreecommitdiff
path: root/src/idl_gen_text.cpp
diff options
context:
space:
mode:
authorWouter van Oortmerssen <aardappel@gmail.com>2017-06-05 17:45:44 -0700
committerWouter van Oortmerssen <aardappel@gmail.com>2017-06-12 16:40:47 -0700
commit8f864aad7b875d645236be59ab1037051b12ba10 (patch)
tree07560dac67ea5f9e0c03d7d830ea08e5285c7cdc /src/idl_gen_text.cpp
parentdddd0865cb161a081afbdfa11919622a49f4141a (diff)
downloadflatbuffers-8f864aad7b875d645236be59ab1037051b12ba10.tar.gz
flatbuffers-8f864aad7b875d645236be59ab1037051b12ba10.tar.bz2
flatbuffers-8f864aad7b875d645236be59ab1037051b12ba10.zip
Added (nested) FlexBuffer JSON parsing and output.
FlexBuffer parser is just 40 lines of code (on top of existing parser!). Change-Id: Idebebadafb661ca5333f5621139031f6df3c3e1a Tested: on Linux.
Diffstat (limited to 'src/idl_gen_text.cpp')
-rw-r--r--src/idl_gen_text.cpp74
1 files changed, 8 insertions, 66 deletions
diff --git a/src/idl_gen_text.cpp b/src/idl_gen_text.cpp
index dc445e5c..9d741e0e 100644
--- a/src/idl_gen_text.cpp
+++ b/src/idl_gen_text.cpp
@@ -19,6 +19,7 @@
#include "flatbuffers/flatbuffers.h"
#include "flatbuffers/idl.h"
#include "flatbuffers/util.h"
+#include "flatbuffers/flexbuffers.h"
namespace flatbuffers {
@@ -101,71 +102,6 @@ template<typename T> bool PrintVector(const Vector<T> &v, Type type,
return true;
}
-static bool EscapeString(const String &s, std::string *_text, const IDLOptions& opts) {
- std::string &text = *_text;
- text += "\"";
- for (uoffset_t i = 0; i < s.size(); i++) {
- char c = s[i];
- switch (c) {
- case '\n': text += "\\n"; break;
- case '\t': text += "\\t"; break;
- case '\r': text += "\\r"; break;
- case '\b': text += "\\b"; break;
- case '\f': text += "\\f"; break;
- case '\"': text += "\\\""; break;
- case '\\': text += "\\\\"; break;
- default:
- if (c >= ' ' && c <= '~') {
- text += c;
- } else {
- // Not printable ASCII data. Let's see if it's valid UTF-8 first:
- const char *utf8 = s.c_str() + i;
- int ucc = FromUTF8(&utf8);
- if (ucc < 0) {
- if (opts.allow_non_utf8) {
- text += "\\x";
- text += IntToStringHex(static_cast<uint8_t>(c), 2);
- } else {
- // There are two cases here:
- //
- // 1) We reached here by parsing an IDL file. In that case,
- // we previously checked for non-UTF-8, so we shouldn't reach
- // here.
- //
- // 2) We reached here by someone calling GenerateText()
- // on a previously-serialized flatbuffer. The data might have
- // non-UTF-8 Strings, or might be corrupt.
- //
- // In both cases, we have to give up and inform the caller
- // they have no JSON.
- return false;
- }
- } else {
- if (ucc <= 0xFFFF) {
- // Parses as Unicode within JSON's \uXXXX range, so use that.
- text += "\\u";
- text += IntToStringHex(ucc, 4);
- } else if (ucc <= 0x10FFFF) {
- // Encode Unicode SMP values to a surrogate pair using two \u escapes.
- uint32_t base = ucc - 0x10000;
- auto high_surrogate = (base >> 10) + 0xD800;
- auto low_surrogate = (base & 0x03FF) + 0xDC00;
- text += "\\u";
- text += IntToStringHex(high_surrogate, 4);
- text += "\\u";
- text += IntToStringHex(low_surrogate, 4);
- }
- // Skip past characters recognized.
- i = static_cast<uoffset_t>(utf8 - s.c_str() - 1);
- }
- }
- break;
- }
- }
- text += "\"";
- return true;
-}
-
// Specialization of Print above for pointer types.
template<> bool Print<const void *>(const void *val,
Type type, int indent,
@@ -189,7 +125,8 @@ template<> bool Print<const void *>(const void *val,
}
break;
case BASE_TYPE_STRING: {
- if (!EscapeString(*reinterpret_cast<const String *>(val), _text, opts)) {
+ auto s = reinterpret_cast<const String *>(val);
+ if (!EscapeString(s->c_str(), s->Length(), _text, opts.allow_non_utf8)) {
return false;
}
break;
@@ -238,6 +175,11 @@ static bool GenFieldOffset(const FieldDef &fd, const Table *table, bool fixed,
assert(IsStruct(fd.value.type));
val = reinterpret_cast<const Struct *>(table)->
GetStruct<const void *>(fd.value.offset);
+ } else if (fd.flexbuffer) {
+ auto vec = table->GetPointer<const Vector<uint8_t> *>(fd.value.offset);
+ auto root = flexbuffers::GetRoot(vec->data(), vec->size());
+ root.ToString(true, false, *_text);
+ return true;
} else {
val = IsStruct(fd.value.type)
? table->GetStruct<const void *>(fd.value.offset)