mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 12:05:50 +00:00
Added a "strict JSON" mode to the text generator and compiler
This will add quotes around field names, as required by the official standard. By default it will leave quotes out, as it is more readable, more compact, and is accepted by almost all JSON parsers. The -S switch to flatc turns on strict mode. As per rfc 7159. Change-Id: Ibabe9c8162c47339d00ec581d18721a2ba40c6d0 Tested: on Windows.
This commit is contained in:
@@ -25,7 +25,8 @@ namespace flatbuffers {
|
||||
|
||||
bool GenerateBinary(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name) {
|
||||
const std::string &file_name,
|
||||
const GeneratorOptions & /*opts*/) {
|
||||
return !parser.builder_.GetSize() ||
|
||||
flatbuffers::SaveFile(
|
||||
(path + file_name + "_wire.bin").c_str(),
|
||||
@@ -36,11 +37,13 @@ bool GenerateBinary(const Parser &parser,
|
||||
|
||||
bool GenerateTextFile(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name) {
|
||||
const std::string &file_name,
|
||||
const GeneratorOptions &opts) {
|
||||
if (!parser.builder_.GetSize()) return true;
|
||||
if (!parser.root_struct_def) Error("root_type not set");
|
||||
std::string text;
|
||||
GenerateText(parser, parser.builder_.GetBufferPointer(), 2, &text);
|
||||
GenerateText(parser, parser.builder_.GetBufferPointer(), opts,
|
||||
&text);
|
||||
return flatbuffers::SaveFile((path + file_name + "_wire.txt").c_str(),
|
||||
text,
|
||||
false);
|
||||
@@ -54,7 +57,8 @@ bool GenerateTextFile(const Parser &parser,
|
||||
struct Generator {
|
||||
bool (*generate)(const flatbuffers::Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name);
|
||||
const std::string &file_name,
|
||||
const flatbuffers::GeneratorOptions &opts);
|
||||
const char *extension;
|
||||
const char *name;
|
||||
const char *help;
|
||||
@@ -82,6 +86,7 @@ static void Error(const char *err, const char *obj, bool usage) {
|
||||
for (size_t i = 0; i < sizeof(generators) / sizeof(generators[0]); ++i)
|
||||
printf(" -%s %s.\n", generators[i].extension, generators[i].help);
|
||||
printf(" -o PATH Prefix PATH to all generated files.\n"
|
||||
" -S Strict JSON: add quotes to field names.\n"
|
||||
"FILEs may depend on declarations in earlier files.\n"
|
||||
"Output files are named using the base file name of the input,"
|
||||
"and written to the current directory or the path given by -o.\n"
|
||||
@@ -110,6 +115,7 @@ std::string StripPath(const std::string &filename) {
|
||||
int main(int argc, const char *argv[]) {
|
||||
program_name = argv[0];
|
||||
flatbuffers::Parser parser;
|
||||
flatbuffers::GeneratorOptions opts;
|
||||
std::string output_path;
|
||||
const size_t num_generators = sizeof(generators) / sizeof(generators[0]);
|
||||
bool generator_enabled[num_generators] = { false };
|
||||
@@ -127,6 +133,9 @@ int main(int argc, const char *argv[]) {
|
||||
if (++i >= argc) Error("missing path following", arg, true);
|
||||
output_path = argv[i];
|
||||
break;
|
||||
case 'S':
|
||||
opts.strict_json = true;
|
||||
break;
|
||||
default:
|
||||
for (size_t i = 0; i < num_generators; ++i) {
|
||||
if(!strcmp(arg+1, generators[i].extension)) {
|
||||
@@ -165,7 +174,7 @@ int main(int argc, const char *argv[]) {
|
||||
|
||||
for (size_t i = 0; i < num_generators; ++i) {
|
||||
if (generator_enabled[i]) {
|
||||
if (!generators[i].generate(parser, output_path, filebase)) {
|
||||
if (!generators[i].generate(parser, output_path, filebase, opts)) {
|
||||
Error((std::string("Unable to generate ") +
|
||||
generators[i].name +
|
||||
" for " +
|
||||
|
||||
Reference in New Issue
Block a user