/src/flatbuffers/src/idl_gen_text.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2014 Google Inc. All rights reserved. |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | * you may not use this file except in compliance with the License. |
6 | | * You may obtain a copy of the License at |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | * See the License for the specific language governing permissions and |
14 | | * limitations under the License. |
15 | | */ |
16 | | |
17 | | // independent from idl_parser, since this code is not needed for most clients |
18 | | #include "idl_gen_text.h" |
19 | | |
20 | | #include <algorithm> |
21 | | |
22 | | #include "flatbuffers/base.h" |
23 | | #include "flatbuffers/code_generator.h" |
24 | | #include "flatbuffers/flatbuffers.h" |
25 | | #include "flatbuffers/flexbuffers.h" |
26 | | #include "flatbuffers/idl.h" |
27 | | #include "flatbuffers/util.h" |
28 | | |
29 | | namespace flatbuffers { |
30 | | |
31 | | struct PrintScalarTag {}; |
32 | | struct PrintPointerTag {}; |
33 | | template <typename T> |
34 | | struct PrintTag { |
35 | | typedef PrintScalarTag type; |
36 | | }; |
37 | | template <> |
38 | | struct PrintTag<const void*> { |
39 | | typedef PrintPointerTag type; |
40 | | }; |
41 | | |
42 | | struct JsonPrinter { |
43 | | // If indentation is less than 0, that indicates we don't want any newlines |
44 | | // either. |
45 | 920k | void AddNewLine() { |
46 | 920k | if (opts.indent_step >= 0) text += '\n'; |
47 | 920k | } |
48 | | |
49 | 907k | void AddIndent(int ident) { text.append(ident, ' '); } |
50 | | |
51 | 187k | int Indent() const { return std::max(opts.indent_step, 0); } |
52 | | |
53 | | // Output an identifier with or without quotes depending on strictness. |
54 | 278k | void OutputIdentifier(const std::string& name) { |
55 | 278k | if (opts.strict_json) text += '\"'; |
56 | 278k | text += name; |
57 | 278k | if (opts.strict_json) text += '\"'; |
58 | 278k | } |
59 | | |
60 | | // Print (and its template specialization below for pointers) generate text |
61 | | // for a single FlatBuffer value into JSON format. |
62 | | // The general case for scalars: |
63 | | template <typename T> |
64 | 416k | void PrintScalar(T val, const Type& type, int /*indent*/) { |
65 | 416k | if (IsBool(type.base_type)) { |
66 | 10.0k | text += val != 0 ? "true" : "false"; |
67 | 10.0k | return; // done |
68 | 10.0k | } |
69 | | |
70 | 406k | if (opts.output_enum_identifiers && type.enum_def) { |
71 | 71.6k | const auto& enum_def = *type.enum_def; |
72 | 71.6k | if (auto ev = enum_def.ReverseLookup(static_cast<int64_t>(val))) { |
73 | 22.9k | text += '\"'; |
74 | 22.9k | text += ev->name; |
75 | 22.9k | text += '\"'; |
76 | 22.9k | return; // done |
77 | 48.6k | } else if (val && enum_def.attributes.Lookup("bit_flags")) { |
78 | 39.1k | const auto entry_len = text.length(); |
79 | 39.1k | const auto u64 = static_cast<uint64_t>(val); |
80 | 39.1k | uint64_t mask = 0; |
81 | 39.1k | text += '\"'; |
82 | 39.1k | for (auto it = enum_def.Vals().begin(), e = enum_def.Vals().end(); |
83 | 156k | it != e; ++it) { |
84 | 117k | auto f = (*it)->GetAsUInt64(); |
85 | 117k | if (f & u64) { |
86 | 47.4k | mask |= f; |
87 | 47.4k | text += (*it)->name; |
88 | 47.4k | text += ' '; |
89 | 47.4k | } |
90 | 117k | } |
91 | | // Don't slice if (u64 != mask) |
92 | 39.1k | if (mask && (u64 == mask)) { |
93 | 15.7k | text[text.length() - 1] = '\"'; |
94 | 15.7k | return; // done |
95 | 15.7k | } |
96 | 23.4k | text.resize(entry_len); // restore |
97 | 23.4k | } |
98 | | // print as numeric value |
99 | 71.6k | } |
100 | | |
101 | 368k | text += NumToString(val); |
102 | 368k | return; |
103 | 406k | } void flatbuffers::JsonPrinter::PrintScalar<unsigned char>(unsigned char, flatbuffers::Type const&, int) Line | Count | Source | 64 | 79.6k | void PrintScalar(T val, const Type& type, int /*indent*/) { | 65 | 79.6k | if (IsBool(type.base_type)) { | 66 | 10.0k | text += val != 0 ? "true" : "false"; | 67 | 10.0k | return; // done | 68 | 10.0k | } | 69 | | | 70 | 69.5k | if (opts.output_enum_identifiers && type.enum_def) { | 71 | 68.0k | const auto& enum_def = *type.enum_def; | 72 | 68.0k | if (auto ev = enum_def.ReverseLookup(static_cast<int64_t>(val))) { | 73 | 21.8k | text += '\"'; | 74 | 21.8k | text += ev->name; | 75 | 21.8k | text += '\"'; | 76 | 21.8k | return; // done | 77 | 46.2k | } else if (val && enum_def.attributes.Lookup("bit_flags")) { | 78 | 37.6k | const auto entry_len = text.length(); | 79 | 37.6k | const auto u64 = static_cast<uint64_t>(val); | 80 | 37.6k | uint64_t mask = 0; | 81 | 37.6k | text += '\"'; | 82 | 37.6k | for (auto it = enum_def.Vals().begin(), e = enum_def.Vals().end(); | 83 | 150k | it != e; ++it) { | 84 | 113k | auto f = (*it)->GetAsUInt64(); | 85 | 113k | if (f & u64) { | 86 | 45.8k | mask |= f; | 87 | 45.8k | text += (*it)->name; | 88 | 45.8k | text += ' '; | 89 | 45.8k | } | 90 | 113k | } | 91 | | // Don't slice if (u64 != mask) | 92 | 37.6k | if (mask && (u64 == mask)) { | 93 | 15.4k | text[text.length() - 1] = '\"'; | 94 | 15.4k | return; // done | 95 | 15.4k | } | 96 | 22.1k | text.resize(entry_len); // restore | 97 | 22.1k | } | 98 | | // print as numeric value | 99 | 68.0k | } | 100 | | | 101 | 32.2k | text += NumToString(val); | 102 | 32.2k | return; | 103 | 69.5k | } |
void flatbuffers::JsonPrinter::PrintScalar<signed char>(signed char, flatbuffers::Type const&, int) Line | Count | Source | 64 | 4.80k | void PrintScalar(T val, const Type& type, int /*indent*/) { | 65 | 4.80k | if (IsBool(type.base_type)) { | 66 | 0 | text += val != 0 ? "true" : "false"; | 67 | 0 | return; // done | 68 | 0 | } | 69 | | | 70 | 4.80k | if (opts.output_enum_identifiers && type.enum_def) { | 71 | 1.43k | const auto& enum_def = *type.enum_def; | 72 | 1.43k | if (auto ev = enum_def.ReverseLookup(static_cast<int64_t>(val))) { | 73 | 800 | text += '\"'; | 74 | 800 | text += ev->name; | 75 | 800 | text += '\"'; | 76 | 800 | return; // done | 77 | 800 | } else if (val && enum_def.attributes.Lookup("bit_flags")) { | 78 | 0 | const auto entry_len = text.length(); | 79 | 0 | const auto u64 = static_cast<uint64_t>(val); | 80 | 0 | uint64_t mask = 0; | 81 | 0 | text += '\"'; | 82 | 0 | for (auto it = enum_def.Vals().begin(), e = enum_def.Vals().end(); | 83 | 0 | it != e; ++it) { | 84 | 0 | auto f = (*it)->GetAsUInt64(); | 85 | 0 | if (f & u64) { | 86 | 0 | mask |= f; | 87 | 0 | text += (*it)->name; | 88 | 0 | text += ' '; | 89 | 0 | } | 90 | 0 | } | 91 | | // Don't slice if (u64 != mask) | 92 | 0 | if (mask && (u64 == mask)) { | 93 | 0 | text[text.length() - 1] = '\"'; | 94 | 0 | return; // done | 95 | 0 | } | 96 | 0 | text.resize(entry_len); // restore | 97 | 0 | } | 98 | | // print as numeric value | 99 | 1.43k | } | 100 | | | 101 | 4.00k | text += NumToString(val); | 102 | 4.00k | return; | 103 | 4.80k | } |
void flatbuffers::JsonPrinter::PrintScalar<short>(short, flatbuffers::Type const&, int) Line | Count | Source | 64 | 6.16k | void PrintScalar(T val, const Type& type, int /*indent*/) { | 65 | 6.16k | if (IsBool(type.base_type)) { | 66 | 0 | text += val != 0 ? "true" : "false"; | 67 | 0 | return; // done | 68 | 0 | } | 69 | | | 70 | 6.16k | if (opts.output_enum_identifiers && type.enum_def) { | 71 | 0 | const auto& enum_def = *type.enum_def; | 72 | 0 | if (auto ev = enum_def.ReverseLookup(static_cast<int64_t>(val))) { | 73 | 0 | text += '\"'; | 74 | 0 | text += ev->name; | 75 | 0 | text += '\"'; | 76 | 0 | return; // done | 77 | 0 | } else if (val && enum_def.attributes.Lookup("bit_flags")) { | 78 | 0 | const auto entry_len = text.length(); | 79 | 0 | const auto u64 = static_cast<uint64_t>(val); | 80 | 0 | uint64_t mask = 0; | 81 | 0 | text += '\"'; | 82 | 0 | for (auto it = enum_def.Vals().begin(), e = enum_def.Vals().end(); | 83 | 0 | it != e; ++it) { | 84 | 0 | auto f = (*it)->GetAsUInt64(); | 85 | 0 | if (f & u64) { | 86 | 0 | mask |= f; | 87 | 0 | text += (*it)->name; | 88 | 0 | text += ' '; | 89 | 0 | } | 90 | 0 | } | 91 | | // Don't slice if (u64 != mask) | 92 | 0 | if (mask && (u64 == mask)) { | 93 | 0 | text[text.length() - 1] = '\"'; | 94 | 0 | return; // done | 95 | 0 | } | 96 | 0 | text.resize(entry_len); // restore | 97 | 0 | } | 98 | | // print as numeric value | 99 | 0 | } | 100 | | | 101 | 6.16k | text += NumToString(val); | 102 | 6.16k | return; | 103 | 6.16k | } |
void flatbuffers::JsonPrinter::PrintScalar<unsigned short>(unsigned short, flatbuffers::Type const&, int) Line | Count | Source | 64 | 58.4k | void PrintScalar(T val, const Type& type, int /*indent*/) { | 65 | 58.4k | if (IsBool(type.base_type)) { | 66 | 0 | text += val != 0 ? "true" : "false"; | 67 | 0 | return; // done | 68 | 0 | } | 69 | | | 70 | 58.4k | if (opts.output_enum_identifiers && type.enum_def) { | 71 | 0 | const auto& enum_def = *type.enum_def; | 72 | 0 | if (auto ev = enum_def.ReverseLookup(static_cast<int64_t>(val))) { | 73 | 0 | text += '\"'; | 74 | 0 | text += ev->name; | 75 | 0 | text += '\"'; | 76 | 0 | return; // done | 77 | 0 | } else if (val && enum_def.attributes.Lookup("bit_flags")) { | 78 | 0 | const auto entry_len = text.length(); | 79 | 0 | const auto u64 = static_cast<uint64_t>(val); | 80 | 0 | uint64_t mask = 0; | 81 | 0 | text += '\"'; | 82 | 0 | for (auto it = enum_def.Vals().begin(), e = enum_def.Vals().end(); | 83 | 0 | it != e; ++it) { | 84 | 0 | auto f = (*it)->GetAsUInt64(); | 85 | 0 | if (f & u64) { | 86 | 0 | mask |= f; | 87 | 0 | text += (*it)->name; | 88 | 0 | text += ' '; | 89 | 0 | } | 90 | 0 | } | 91 | | // Don't slice if (u64 != mask) | 92 | 0 | if (mask && (u64 == mask)) { | 93 | 0 | text[text.length() - 1] = '\"'; | 94 | 0 | return; // done | 95 | 0 | } | 96 | 0 | text.resize(entry_len); // restore | 97 | 0 | } | 98 | | // print as numeric value | 99 | 0 | } | 100 | | | 101 | 58.4k | text += NumToString(val); | 102 | 58.4k | return; | 103 | 58.4k | } |
void flatbuffers::JsonPrinter::PrintScalar<int>(int, flatbuffers::Type const&, int) Line | Count | Source | 64 | 1.18k | void PrintScalar(T val, const Type& type, int /*indent*/) { | 65 | 1.18k | if (IsBool(type.base_type)) { | 66 | 0 | text += val != 0 ? "true" : "false"; | 67 | 0 | return; // done | 68 | 0 | } | 69 | | | 70 | 1.18k | if (opts.output_enum_identifiers && type.enum_def) { | 71 | 0 | const auto& enum_def = *type.enum_def; | 72 | 0 | if (auto ev = enum_def.ReverseLookup(static_cast<int64_t>(val))) { | 73 | 0 | text += '\"'; | 74 | 0 | text += ev->name; | 75 | 0 | text += '\"'; | 76 | 0 | return; // done | 77 | 0 | } else if (val && enum_def.attributes.Lookup("bit_flags")) { | 78 | 0 | const auto entry_len = text.length(); | 79 | 0 | const auto u64 = static_cast<uint64_t>(val); | 80 | 0 | uint64_t mask = 0; | 81 | 0 | text += '\"'; | 82 | 0 | for (auto it = enum_def.Vals().begin(), e = enum_def.Vals().end(); | 83 | 0 | it != e; ++it) { | 84 | 0 | auto f = (*it)->GetAsUInt64(); | 85 | 0 | if (f & u64) { | 86 | 0 | mask |= f; | 87 | 0 | text += (*it)->name; | 88 | 0 | text += ' '; | 89 | 0 | } | 90 | 0 | } | 91 | | // Don't slice if (u64 != mask) | 92 | 0 | if (mask && (u64 == mask)) { | 93 | 0 | text[text.length() - 1] = '\"'; | 94 | 0 | return; // done | 95 | 0 | } | 96 | 0 | text.resize(entry_len); // restore | 97 | 0 | } | 98 | | // print as numeric value | 99 | 0 | } | 100 | | | 101 | 1.18k | text += NumToString(val); | 102 | 1.18k | return; | 103 | 1.18k | } |
void flatbuffers::JsonPrinter::PrintScalar<unsigned int>(unsigned int, flatbuffers::Type const&, int) Line | Count | Source | 64 | 47.0k | void PrintScalar(T val, const Type& type, int /*indent*/) { | 65 | 47.0k | if (IsBool(type.base_type)) { | 66 | 0 | text += val != 0 ? "true" : "false"; | 67 | 0 | return; // done | 68 | 0 | } | 69 | | | 70 | 47.0k | if (opts.output_enum_identifiers && type.enum_def) { | 71 | 0 | const auto& enum_def = *type.enum_def; | 72 | 0 | if (auto ev = enum_def.ReverseLookup(static_cast<int64_t>(val))) { | 73 | 0 | text += '\"'; | 74 | 0 | text += ev->name; | 75 | 0 | text += '\"'; | 76 | 0 | return; // done | 77 | 0 | } else if (val && enum_def.attributes.Lookup("bit_flags")) { | 78 | 0 | const auto entry_len = text.length(); | 79 | 0 | const auto u64 = static_cast<uint64_t>(val); | 80 | 0 | uint64_t mask = 0; | 81 | 0 | text += '\"'; | 82 | 0 | for (auto it = enum_def.Vals().begin(), e = enum_def.Vals().end(); | 83 | 0 | it != e; ++it) { | 84 | 0 | auto f = (*it)->GetAsUInt64(); | 85 | 0 | if (f & u64) { | 86 | 0 | mask |= f; | 87 | 0 | text += (*it)->name; | 88 | 0 | text += ' '; | 89 | 0 | } | 90 | 0 | } | 91 | | // Don't slice if (u64 != mask) | 92 | 0 | if (mask && (u64 == mask)) { | 93 | 0 | text[text.length() - 1] = '\"'; | 94 | 0 | return; // done | 95 | 0 | } | 96 | 0 | text.resize(entry_len); // restore | 97 | 0 | } | 98 | | // print as numeric value | 99 | 0 | } | 100 | | | 101 | 47.0k | text += NumToString(val); | 102 | 47.0k | return; | 103 | 47.0k | } |
void flatbuffers::JsonPrinter::PrintScalar<long>(long, flatbuffers::Type const&, int) Line | Count | Source | 64 | 98.1k | void PrintScalar(T val, const Type& type, int /*indent*/) { | 65 | 98.1k | if (IsBool(type.base_type)) { | 66 | 0 | text += val != 0 ? "true" : "false"; | 67 | 0 | return; // done | 68 | 0 | } | 69 | | | 70 | 98.1k | if (opts.output_enum_identifiers && type.enum_def) { | 71 | 0 | const auto& enum_def = *type.enum_def; | 72 | 0 | if (auto ev = enum_def.ReverseLookup(static_cast<int64_t>(val))) { | 73 | 0 | text += '\"'; | 74 | 0 | text += ev->name; | 75 | 0 | text += '\"'; | 76 | 0 | return; // done | 77 | 0 | } else if (val && enum_def.attributes.Lookup("bit_flags")) { | 78 | 0 | const auto entry_len = text.length(); | 79 | 0 | const auto u64 = static_cast<uint64_t>(val); | 80 | 0 | uint64_t mask = 0; | 81 | 0 | text += '\"'; | 82 | 0 | for (auto it = enum_def.Vals().begin(), e = enum_def.Vals().end(); | 83 | 0 | it != e; ++it) { | 84 | 0 | auto f = (*it)->GetAsUInt64(); | 85 | 0 | if (f & u64) { | 86 | 0 | mask |= f; | 87 | 0 | text += (*it)->name; | 88 | 0 | text += ' '; | 89 | 0 | } | 90 | 0 | } | 91 | | // Don't slice if (u64 != mask) | 92 | 0 | if (mask && (u64 == mask)) { | 93 | 0 | text[text.length() - 1] = '\"'; | 94 | 0 | return; // done | 95 | 0 | } | 96 | 0 | text.resize(entry_len); // restore | 97 | 0 | } | 98 | | // print as numeric value | 99 | 0 | } | 100 | | | 101 | 98.1k | text += NumToString(val); | 102 | 98.1k | return; | 103 | 98.1k | } |
void flatbuffers::JsonPrinter::PrintScalar<unsigned long>(unsigned long, flatbuffers::Type const&, int) Line | Count | Source | 64 | 58.0k | void PrintScalar(T val, const Type& type, int /*indent*/) { | 65 | 58.0k | if (IsBool(type.base_type)) { | 66 | 0 | text += val != 0 ? "true" : "false"; | 67 | 0 | return; // done | 68 | 0 | } | 69 | | | 70 | 58.0k | if (opts.output_enum_identifiers && type.enum_def) { | 71 | 2.08k | const auto& enum_def = *type.enum_def; | 72 | 2.08k | if (auto ev = enum_def.ReverseLookup(static_cast<int64_t>(val))) { | 73 | 322 | text += '\"'; | 74 | 322 | text += ev->name; | 75 | 322 | text += '\"'; | 76 | 322 | return; // done | 77 | 1.76k | } else if (val && enum_def.attributes.Lookup("bit_flags")) { | 78 | 1.49k | const auto entry_len = text.length(); | 79 | 1.49k | const auto u64 = static_cast<uint64_t>(val); | 80 | 1.49k | uint64_t mask = 0; | 81 | 1.49k | text += '\"'; | 82 | 1.49k | for (auto it = enum_def.Vals().begin(), e = enum_def.Vals().end(); | 83 | 5.99k | it != e; ++it) { | 84 | 4.49k | auto f = (*it)->GetAsUInt64(); | 85 | 4.49k | if (f & u64) { | 86 | 1.59k | mask |= f; | 87 | 1.59k | text += (*it)->name; | 88 | 1.59k | text += ' '; | 89 | 1.59k | } | 90 | 4.49k | } | 91 | | // Don't slice if (u64 != mask) | 92 | 1.49k | if (mask && (u64 == mask)) { | 93 | 268 | text[text.length() - 1] = '\"'; | 94 | 268 | return; // done | 95 | 268 | } | 96 | 1.23k | text.resize(entry_len); // restore | 97 | 1.23k | } | 98 | | // print as numeric value | 99 | 2.08k | } | 100 | | | 101 | 57.5k | text += NumToString(val); | 102 | 57.5k | return; | 103 | 58.0k | } |
void flatbuffers::JsonPrinter::PrintScalar<float>(float, flatbuffers::Type const&, int) Line | Count | Source | 64 | 8.59k | void PrintScalar(T val, const Type& type, int /*indent*/) { | 65 | 8.59k | if (IsBool(type.base_type)) { | 66 | 0 | text += val != 0 ? "true" : "false"; | 67 | 0 | return; // done | 68 | 0 | } | 69 | | | 70 | 8.59k | if (opts.output_enum_identifiers && type.enum_def) { | 71 | 0 | const auto& enum_def = *type.enum_def; | 72 | 0 | if (auto ev = enum_def.ReverseLookup(static_cast<int64_t>(val))) { | 73 | 0 | text += '\"'; | 74 | 0 | text += ev->name; | 75 | 0 | text += '\"'; | 76 | 0 | return; // done | 77 | 0 | } else if (val && enum_def.attributes.Lookup("bit_flags")) { | 78 | 0 | const auto entry_len = text.length(); | 79 | 0 | const auto u64 = static_cast<uint64_t>(val); | 80 | 0 | uint64_t mask = 0; | 81 | 0 | text += '\"'; | 82 | 0 | for (auto it = enum_def.Vals().begin(), e = enum_def.Vals().end(); | 83 | 0 | it != e; ++it) { | 84 | 0 | auto f = (*it)->GetAsUInt64(); | 85 | 0 | if (f & u64) { | 86 | 0 | mask |= f; | 87 | 0 | text += (*it)->name; | 88 | 0 | text += ' '; | 89 | 0 | } | 90 | 0 | } | 91 | | // Don't slice if (u64 != mask) | 92 | 0 | if (mask && (u64 == mask)) { | 93 | 0 | text[text.length() - 1] = '\"'; | 94 | 0 | return; // done | 95 | 0 | } | 96 | 0 | text.resize(entry_len); // restore | 97 | 0 | } | 98 | | // print as numeric value | 99 | 0 | } | 100 | | | 101 | 8.59k | text += NumToString(val); | 102 | 8.59k | return; | 103 | 8.59k | } |
void flatbuffers::JsonPrinter::PrintScalar<double>(double, flatbuffers::Type const&, int) Line | Count | Source | 64 | 54.7k | void PrintScalar(T val, const Type& type, int /*indent*/) { | 65 | 54.7k | if (IsBool(type.base_type)) { | 66 | 0 | text += val != 0 ? "true" : "false"; | 67 | 0 | return; // done | 68 | 0 | } | 69 | | | 70 | 54.7k | if (opts.output_enum_identifiers && type.enum_def) { | 71 | 0 | const auto& enum_def = *type.enum_def; | 72 | 0 | if (auto ev = enum_def.ReverseLookup(static_cast<int64_t>(val))) { | 73 | 0 | text += '\"'; | 74 | 0 | text += ev->name; | 75 | 0 | text += '\"'; | 76 | 0 | return; // done | 77 | 0 | } else if (val && enum_def.attributes.Lookup("bit_flags")) { | 78 | 0 | const auto entry_len = text.length(); | 79 | 0 | const auto u64 = static_cast<uint64_t>(val); | 80 | 0 | uint64_t mask = 0; | 81 | 0 | text += '\"'; | 82 | 0 | for (auto it = enum_def.Vals().begin(), e = enum_def.Vals().end(); | 83 | 0 | it != e; ++it) { | 84 | 0 | auto f = (*it)->GetAsUInt64(); | 85 | 0 | if (f & u64) { | 86 | 0 | mask |= f; | 87 | 0 | text += (*it)->name; | 88 | 0 | text += ' '; | 89 | 0 | } | 90 | 0 | } | 91 | | // Don't slice if (u64 != mask) | 92 | 0 | if (mask && (u64 == mask)) { | 93 | 0 | text[text.length() - 1] = '\"'; | 94 | 0 | return; // done | 95 | 0 | } | 96 | 0 | text.resize(entry_len); // restore | 97 | 0 | } | 98 | | // print as numeric value | 99 | 0 | } | 100 | | | 101 | 54.7k | text += NumToString(val); | 102 | 54.7k | return; | 103 | 54.7k | } |
|
104 | | |
105 | 539k | void AddComma() { |
106 | 539k | if (!opts.protobuf_ascii_alike) text += ','; |
107 | 539k | } |
108 | | |
109 | | // Print a vector or an array of JSON values, comma seperated, wrapped in |
110 | | // "[]". |
111 | | template <typename Container, typename SizeT = typename Container::size_type> |
112 | | const char* PrintContainer(PrintScalarTag, const Container& c, SizeT size, |
113 | 4.84k | const Type& type, int indent, const uint8_t*) { |
114 | 4.84k | const auto elem_indent = indent + Indent(); |
115 | 4.84k | text += '['; |
116 | 4.84k | AddNewLine(); |
117 | 247k | for (SizeT i = 0; i < size; i++) { |
118 | 242k | if (i) { |
119 | 239k | AddComma(); |
120 | 239k | AddNewLine(); |
121 | 239k | } |
122 | 242k | AddIndent(elem_indent); |
123 | 242k | PrintScalar(c[i], type, elem_indent); |
124 | 242k | } |
125 | 4.84k | AddNewLine(); |
126 | 4.84k | AddIndent(indent); |
127 | 4.84k | text += ']'; |
128 | 4.84k | return nullptr; |
129 | 4.84k | } char const* flatbuffers::JsonPrinter::PrintContainer<flatbuffers::Vector<unsigned char, unsigned int>, unsigned int>(flatbuffers::PrintScalarTag, flatbuffers::Vector<unsigned char, unsigned int> const&, unsigned int, flatbuffers::Type const&, int, unsigned char const*) Line | Count | Source | 113 | 2.01k | const Type& type, int indent, const uint8_t*) { | 114 | 2.01k | const auto elem_indent = indent + Indent(); | 115 | 2.01k | text += '['; | 116 | 2.01k | AddNewLine(); | 117 | 70.7k | for (SizeT i = 0; i < size; i++) { | 118 | 68.7k | if (i) { | 119 | 67.5k | AddComma(); | 120 | 67.5k | AddNewLine(); | 121 | 67.5k | } | 122 | 68.7k | AddIndent(elem_indent); | 123 | 68.7k | PrintScalar(c[i], type, elem_indent); | 124 | 68.7k | } | 125 | 2.01k | AddNewLine(); | 126 | 2.01k | AddIndent(indent); | 127 | 2.01k | text += ']'; | 128 | 2.01k | return nullptr; | 129 | 2.01k | } |
Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintContainer<flatbuffers::Vector<signed char, unsigned int>, unsigned int>(flatbuffers::PrintScalarTag, flatbuffers::Vector<signed char, unsigned int> const&, unsigned int, flatbuffers::Type const&, int, unsigned char const*) Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintContainer<flatbuffers::Vector<short, unsigned int>, unsigned int>(flatbuffers::PrintScalarTag, flatbuffers::Vector<short, unsigned int> const&, unsigned int, flatbuffers::Type const&, int, unsigned char const*) Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintContainer<flatbuffers::Vector<unsigned short, unsigned int>, unsigned int>(flatbuffers::PrintScalarTag, flatbuffers::Vector<unsigned short, unsigned int> const&, unsigned int, flatbuffers::Type const&, int, unsigned char const*) Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintContainer<flatbuffers::Vector<int, unsigned int>, unsigned int>(flatbuffers::PrintScalarTag, flatbuffers::Vector<int, unsigned int> const&, unsigned int, flatbuffers::Type const&, int, unsigned char const*) Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintContainer<flatbuffers::Vector<unsigned int, unsigned int>, unsigned int>(flatbuffers::PrintScalarTag, flatbuffers::Vector<unsigned int, unsigned int> const&, unsigned int, flatbuffers::Type const&, int, unsigned char const*) char const* flatbuffers::JsonPrinter::PrintContainer<flatbuffers::Vector<long, unsigned int>, unsigned int>(flatbuffers::PrintScalarTag, flatbuffers::Vector<long, unsigned int> const&, unsigned int, flatbuffers::Type const&, int, unsigned char const*) Line | Count | Source | 113 | 736 | const Type& type, int indent, const uint8_t*) { | 114 | 736 | const auto elem_indent = indent + Indent(); | 115 | 736 | text += '['; | 116 | 736 | AddNewLine(); | 117 | 93.5k | for (SizeT i = 0; i < size; i++) { | 118 | 92.8k | if (i) { | 119 | 92.3k | AddComma(); | 120 | 92.3k | AddNewLine(); | 121 | 92.3k | } | 122 | 92.8k | AddIndent(elem_indent); | 123 | 92.8k | PrintScalar(c[i], type, elem_indent); | 124 | 92.8k | } | 125 | 736 | AddNewLine(); | 126 | 736 | AddIndent(indent); | 127 | 736 | text += ']'; | 128 | 736 | return nullptr; | 129 | 736 | } |
char const* flatbuffers::JsonPrinter::PrintContainer<flatbuffers::Vector<unsigned long, unsigned int>, unsigned int>(flatbuffers::PrintScalarTag, flatbuffers::Vector<unsigned long, unsigned int> const&, unsigned int, flatbuffers::Type const&, int, unsigned char const*) Line | Count | Source | 113 | 678 | const Type& type, int indent, const uint8_t*) { | 114 | 678 | const auto elem_indent = indent + Indent(); | 115 | 678 | text += '['; | 116 | 678 | AddNewLine(); | 117 | 30.6k | for (SizeT i = 0; i < size; i++) { | 118 | 29.9k | if (i) { | 119 | 29.4k | AddComma(); | 120 | 29.4k | AddNewLine(); | 121 | 29.4k | } | 122 | 29.9k | AddIndent(elem_indent); | 123 | 29.9k | PrintScalar(c[i], type, elem_indent); | 124 | 29.9k | } | 125 | 678 | AddNewLine(); | 126 | 678 | AddIndent(indent); | 127 | 678 | text += ']'; | 128 | 678 | return nullptr; | 129 | 678 | } |
Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintContainer<flatbuffers::Vector<float, unsigned int>, unsigned int>(flatbuffers::PrintScalarTag, flatbuffers::Vector<float, unsigned int> const&, unsigned int, flatbuffers::Type const&, int, unsigned char const*) char const* flatbuffers::JsonPrinter::PrintContainer<flatbuffers::Vector<double, unsigned int>, unsigned int>(flatbuffers::PrintScalarTag, flatbuffers::Vector<double, unsigned int> const&, unsigned int, flatbuffers::Type const&, int, unsigned char const*) Line | Count | Source | 113 | 1.42k | const Type& type, int indent, const uint8_t*) { | 114 | 1.42k | const auto elem_indent = indent + Indent(); | 115 | 1.42k | text += '['; | 116 | 1.42k | AddNewLine(); | 117 | 52.5k | for (SizeT i = 0; i < size; i++) { | 118 | 51.1k | if (i) { | 119 | 49.8k | AddComma(); | 120 | 49.8k | AddNewLine(); | 121 | 49.8k | } | 122 | 51.1k | AddIndent(elem_indent); | 123 | 51.1k | PrintScalar(c[i], type, elem_indent); | 124 | 51.1k | } | 125 | 1.42k | AddNewLine(); | 126 | 1.42k | AddIndent(indent); | 127 | 1.42k | text += ']'; | 128 | 1.42k | return nullptr; | 129 | 1.42k | } |
Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintContainer<flatbuffers::Array<unsigned char, (unsigned short)65535>, unsigned short>(flatbuffers::PrintScalarTag, flatbuffers::Array<unsigned char, (unsigned short)65535> const&, unsigned short, flatbuffers::Type const&, int, unsigned char const*) Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintContainer<flatbuffers::Array<signed char, (unsigned short)65535>, unsigned short>(flatbuffers::PrintScalarTag, flatbuffers::Array<signed char, (unsigned short)65535> const&, unsigned short, flatbuffers::Type const&, int, unsigned char const*) Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintContainer<flatbuffers::Array<short, (unsigned short)65535>, unsigned short>(flatbuffers::PrintScalarTag, flatbuffers::Array<short, (unsigned short)65535> const&, unsigned short, flatbuffers::Type const&, int, unsigned char const*) Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintContainer<flatbuffers::Array<unsigned short, (unsigned short)65535>, unsigned short>(flatbuffers::PrintScalarTag, flatbuffers::Array<unsigned short, (unsigned short)65535> const&, unsigned short, flatbuffers::Type const&, int, unsigned char const*) Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintContainer<flatbuffers::Array<int, (unsigned short)65535>, unsigned short>(flatbuffers::PrintScalarTag, flatbuffers::Array<int, (unsigned short)65535> const&, unsigned short, flatbuffers::Type const&, int, unsigned char const*) Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintContainer<flatbuffers::Array<unsigned int, (unsigned short)65535>, unsigned short>(flatbuffers::PrintScalarTag, flatbuffers::Array<unsigned int, (unsigned short)65535> const&, unsigned short, flatbuffers::Type const&, int, unsigned char const*) Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintContainer<flatbuffers::Array<long, (unsigned short)65535>, unsigned short>(flatbuffers::PrintScalarTag, flatbuffers::Array<long, (unsigned short)65535> const&, unsigned short, flatbuffers::Type const&, int, unsigned char const*) Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintContainer<flatbuffers::Array<unsigned long, (unsigned short)65535>, unsigned short>(flatbuffers::PrintScalarTag, flatbuffers::Array<unsigned long, (unsigned short)65535> const&, unsigned short, flatbuffers::Type const&, int, unsigned char const*) Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintContainer<flatbuffers::Array<float, (unsigned short)65535>, unsigned short>(flatbuffers::PrintScalarTag, flatbuffers::Array<float, (unsigned short)65535> const&, unsigned short, flatbuffers::Type const&, int, unsigned char const*) Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintContainer<flatbuffers::Array<double, (unsigned short)65535>, unsigned short>(flatbuffers::PrintScalarTag, flatbuffers::Array<double, (unsigned short)65535> const&, unsigned short, flatbuffers::Type const&, int, unsigned char const*) |
130 | | |
131 | | // Print a vector or an array of JSON values, comma seperated, wrapped in |
132 | | // "[]". |
133 | | template <typename Container, typename SizeT = typename Container::size_type> |
134 | | const char* PrintContainer(PrintPointerTag, const Container& c, SizeT size, |
135 | | const Type& type, int indent, |
136 | 6.35k | const uint8_t* prev_val) { |
137 | 6.35k | const auto is_struct = IsStruct(type); |
138 | 6.35k | const auto elem_indent = indent + Indent(); |
139 | 6.35k | text += '['; |
140 | 6.35k | AddNewLine(); |
141 | 206k | for (SizeT i = 0; i < size; i++) { |
142 | 199k | if (i) { |
143 | 196k | AddComma(); |
144 | 196k | AddNewLine(); |
145 | 196k | } |
146 | 199k | AddIndent(elem_indent); |
147 | 199k | auto ptr = is_struct ? reinterpret_cast<const void*>( |
148 | 23.2k | c.Data() + type.struct_def->bytesize * i) |
149 | 199k | : c[i]; |
150 | 199k | auto err = PrintOffset(ptr, type, elem_indent, prev_val, |
151 | 199k | static_cast<soffset_t>(i)); |
152 | 199k | if (err) return err; |
153 | 199k | } |
154 | 6.35k | AddNewLine(); |
155 | 6.35k | AddIndent(indent); |
156 | 6.35k | text += ']'; |
157 | 6.35k | return nullptr; |
158 | 6.35k | } char const* flatbuffers::JsonPrinter::PrintContainer<flatbuffers::Vector<flatbuffers::Offset<void>, unsigned int>, unsigned int>(flatbuffers::PrintPointerTag, flatbuffers::Vector<flatbuffers::Offset<void>, unsigned int> const&, unsigned int, flatbuffers::Type const&, int, unsigned char const*) Line | Count | Source | 136 | 6.35k | const uint8_t* prev_val) { | 137 | 6.35k | const auto is_struct = IsStruct(type); | 138 | 6.35k | const auto elem_indent = indent + Indent(); | 139 | 6.35k | text += '['; | 140 | 6.35k | AddNewLine(); | 141 | 206k | for (SizeT i = 0; i < size; i++) { | 142 | 199k | if (i) { | 143 | 196k | AddComma(); | 144 | 196k | AddNewLine(); | 145 | 196k | } | 146 | 199k | AddIndent(elem_indent); | 147 | 199k | auto ptr = is_struct ? reinterpret_cast<const void*>( | 148 | 23.2k | c.Data() + type.struct_def->bytesize * i) | 149 | 199k | : c[i]; | 150 | 199k | auto err = PrintOffset(ptr, type, elem_indent, prev_val, | 151 | 199k | static_cast<soffset_t>(i)); | 152 | 199k | if (err) return err; | 153 | 199k | } | 154 | 6.35k | AddNewLine(); | 155 | 6.35k | AddIndent(indent); | 156 | 6.35k | text += ']'; | 157 | 6.35k | return nullptr; | 158 | 6.35k | } |
Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintContainer<flatbuffers::Vector<flatbuffers::Offset64<void>, unsigned int>, unsigned int>(flatbuffers::PrintPointerTag, flatbuffers::Vector<flatbuffers::Offset64<void>, unsigned int> const&, unsigned int, flatbuffers::Type const&, int, unsigned char const*) Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintContainer<flatbuffers::Array<flatbuffers::Offset<void>, (unsigned short)65535>, unsigned short>(flatbuffers::PrintPointerTag, flatbuffers::Array<flatbuffers::Offset<void>, (unsigned short)65535> const&, unsigned short, flatbuffers::Type const&, int, unsigned char const*) Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintContainer<flatbuffers::Array<flatbuffers::Offset64<void>, (unsigned short)65535>, unsigned short>(flatbuffers::PrintPointerTag, flatbuffers::Array<flatbuffers::Offset64<void>, (unsigned short)65535> const&, unsigned short, flatbuffers::Type const&, int, unsigned char const*) |
159 | | |
160 | | template <typename T, typename SizeT = uoffset_t> |
161 | | const char* PrintVector(const void* val, const Type& type, int indent, |
162 | 11.1k | const uint8_t* prev_val) { |
163 | 11.1k | typedef Vector<T, SizeT> Container; |
164 | 11.1k | typedef typename PrintTag<typename Container::return_type>::type tag; |
165 | 11.1k | auto& vec = *reinterpret_cast<const Container*>(val); |
166 | 11.1k | return PrintContainer<Container>(tag(), vec, vec.size(), type, indent, |
167 | 11.1k | prev_val); |
168 | 11.1k | } char const* flatbuffers::JsonPrinter::PrintVector<unsigned char, unsigned int>(void const*, flatbuffers::Type const&, int, unsigned char const*) Line | Count | Source | 162 | 2.01k | const uint8_t* prev_val) { | 163 | 2.01k | typedef Vector<T, SizeT> Container; | 164 | 2.01k | typedef typename PrintTag<typename Container::return_type>::type tag; | 165 | 2.01k | auto& vec = *reinterpret_cast<const Container*>(val); | 166 | 2.01k | return PrintContainer<Container>(tag(), vec, vec.size(), type, indent, | 167 | 2.01k | prev_val); | 168 | 2.01k | } |
Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintVector<signed char, unsigned int>(void const*, flatbuffers::Type const&, int, unsigned char const*) Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintVector<short, unsigned int>(void const*, flatbuffers::Type const&, int, unsigned char const*) Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintVector<unsigned short, unsigned int>(void const*, flatbuffers::Type const&, int, unsigned char const*) Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintVector<int, unsigned int>(void const*, flatbuffers::Type const&, int, unsigned char const*) Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintVector<unsigned int, unsigned int>(void const*, flatbuffers::Type const&, int, unsigned char const*) char const* flatbuffers::JsonPrinter::PrintVector<long, unsigned int>(void const*, flatbuffers::Type const&, int, unsigned char const*) Line | Count | Source | 162 | 736 | const uint8_t* prev_val) { | 163 | 736 | typedef Vector<T, SizeT> Container; | 164 | 736 | typedef typename PrintTag<typename Container::return_type>::type tag; | 165 | 736 | auto& vec = *reinterpret_cast<const Container*>(val); | 166 | 736 | return PrintContainer<Container>(tag(), vec, vec.size(), type, indent, | 167 | 736 | prev_val); | 168 | 736 | } |
char const* flatbuffers::JsonPrinter::PrintVector<unsigned long, unsigned int>(void const*, flatbuffers::Type const&, int, unsigned char const*) Line | Count | Source | 162 | 678 | const uint8_t* prev_val) { | 163 | 678 | typedef Vector<T, SizeT> Container; | 164 | 678 | typedef typename PrintTag<typename Container::return_type>::type tag; | 165 | 678 | auto& vec = *reinterpret_cast<const Container*>(val); | 166 | 678 | return PrintContainer<Container>(tag(), vec, vec.size(), type, indent, | 167 | 678 | prev_val); | 168 | 678 | } |
Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintVector<float, unsigned int>(void const*, flatbuffers::Type const&, int, unsigned char const*) char const* flatbuffers::JsonPrinter::PrintVector<double, unsigned int>(void const*, flatbuffers::Type const&, int, unsigned char const*) Line | Count | Source | 162 | 1.42k | const uint8_t* prev_val) { | 163 | 1.42k | typedef Vector<T, SizeT> Container; | 164 | 1.42k | typedef typename PrintTag<typename Container::return_type>::type tag; | 165 | 1.42k | auto& vec = *reinterpret_cast<const Container*>(val); | 166 | 1.42k | return PrintContainer<Container>(tag(), vec, vec.size(), type, indent, | 167 | 1.42k | prev_val); | 168 | 1.42k | } |
char const* flatbuffers::JsonPrinter::PrintVector<flatbuffers::Offset<void>, unsigned int>(void const*, flatbuffers::Type const&, int, unsigned char const*) Line | Count | Source | 162 | 6.35k | const uint8_t* prev_val) { | 163 | 6.35k | typedef Vector<T, SizeT> Container; | 164 | 6.35k | typedef typename PrintTag<typename Container::return_type>::type tag; | 165 | 6.35k | auto& vec = *reinterpret_cast<const Container*>(val); | 166 | 6.35k | return PrintContainer<Container>(tag(), vec, vec.size(), type, indent, | 167 | 6.35k | prev_val); | 168 | 6.35k | } |
Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintVector<flatbuffers::Offset64<void>, unsigned int>(void const*, flatbuffers::Type const&, int, unsigned char const*) |
169 | | |
170 | | // Print an array a sequence of JSON values, comma separated, wrapped in "[]". |
171 | | template <typename T> |
172 | | const char* PrintArray(const void* val, uint16_t size, const Type& type, |
173 | | |
174 | 0 | int indent) { |
175 | 0 | typedef Array<T, 0xFFFF> Container; |
176 | 0 | typedef typename PrintTag<typename Container::return_type>::type tag; |
177 | 0 | auto& arr = *reinterpret_cast<const Container*>(val); |
178 | 0 | return PrintContainer<Container>(tag(), arr, size, type, indent, nullptr); |
179 | 0 | } Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintArray<unsigned char>(void const*, unsigned short, flatbuffers::Type const&, int) Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintArray<signed char>(void const*, unsigned short, flatbuffers::Type const&, int) Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintArray<short>(void const*, unsigned short, flatbuffers::Type const&, int) Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintArray<unsigned short>(void const*, unsigned short, flatbuffers::Type const&, int) Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintArray<int>(void const*, unsigned short, flatbuffers::Type const&, int) Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintArray<unsigned int>(void const*, unsigned short, flatbuffers::Type const&, int) Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintArray<long>(void const*, unsigned short, flatbuffers::Type const&, int) Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintArray<unsigned long>(void const*, unsigned short, flatbuffers::Type const&, int) Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintArray<float>(void const*, unsigned short, flatbuffers::Type const&, int) Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintArray<double>(void const*, unsigned short, flatbuffers::Type const&, int) Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintArray<flatbuffers::Offset<void> >(void const*, unsigned short, flatbuffers::Type const&, int) Unexecuted instantiation: char const* flatbuffers::JsonPrinter::PrintArray<flatbuffers::Offset64<void> >(void const*, unsigned short, flatbuffers::Type const&, int) |
180 | | |
181 | | const char* PrintOffset(const void* val, const Type& type, int indent, |
182 | 287k | const uint8_t* prev_val, soffset_t vector_index) { |
183 | 287k | switch (type.base_type) { |
184 | 2.66k | case BASE_TYPE_UNION: { |
185 | | // If this assert hits, you have an corrupt buffer, a union type field |
186 | | // was not present or was out of range. |
187 | 2.66k | FLATBUFFERS_ASSERT(prev_val); |
188 | 2.66k | auto union_type_byte = *prev_val; // Always a uint8_t. |
189 | 2.66k | if (vector_index >= 0) { |
190 | 0 | auto type_vec = reinterpret_cast<const Vector<uint8_t>*>( |
191 | 0 | prev_val + ReadScalar<uoffset_t>(prev_val)); |
192 | 0 | union_type_byte = type_vec->Get(static_cast<uoffset_t>(vector_index)); |
193 | 0 | } |
194 | 2.66k | auto enum_val = type.enum_def->ReverseLookup(union_type_byte, true); |
195 | 2.66k | if (enum_val) { |
196 | 2.66k | return PrintOffset(val, enum_val->union_type, indent, nullptr, -1); |
197 | 2.66k | } else { |
198 | 0 | return "unknown enum value"; |
199 | 0 | } |
200 | 2.66k | } |
201 | 160k | case BASE_TYPE_STRUCT: |
202 | 160k | return GenStruct(*type.struct_def, reinterpret_cast<const Table*>(val), |
203 | 160k | indent); |
204 | 113k | case BASE_TYPE_STRING: { |
205 | 113k | auto s = reinterpret_cast<const String*>(val); |
206 | 113k | bool ok = EscapeString(s->c_str(), s->size(), &text, |
207 | 113k | opts.allow_non_utf8, opts.natural_utf8); |
208 | 113k | return ok ? nullptr : "string contains non-utf8 bytes"; |
209 | 2.66k | } |
210 | 11.1k | case BASE_TYPE_VECTOR: { |
211 | 11.1k | const auto vec_type = type.VectorType(); |
212 | | // Call PrintVector above specifically for each element type: |
213 | | // clang-format off |
214 | 11.1k | switch (vec_type.base_type) { |
215 | 0 | #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, ...) \ |
216 | 11.1k | case BASE_TYPE_ ## ENUM: { \ |
217 | 11.1k | auto err = PrintVector<CTYPE>(val, vec_type, indent, prev_val); \ |
218 | 11.1k | if (err) return err; \ |
219 | 11.1k | break; } |
220 | 11.1k | FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD) |
221 | 11.1k | #undef FLATBUFFERS_TD |
222 | 11.1k | } |
223 | | // clang-format on |
224 | 11.1k | return nullptr; |
225 | 11.1k | } |
226 | 0 | case BASE_TYPE_ARRAY: { |
227 | 0 | const auto vec_type = type.VectorType(); |
228 | | // Call PrintArray above specifically for each element type: |
229 | | // clang-format off |
230 | 0 | switch (vec_type.base_type) { |
231 | 0 | #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, ...) \ |
232 | 0 | case BASE_TYPE_ ## ENUM: { \ |
233 | 0 | auto err = PrintArray<CTYPE>(val, type.fixed_length, vec_type, indent); \ |
234 | 0 | if (err) return err; \ |
235 | 0 | break; } |
236 | 0 | FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD) |
237 | | // Arrays of scalars or structs are only possible. |
238 | 0 | FLATBUFFERS_GEN_TYPES_POINTER(FLATBUFFERS_TD) |
239 | 0 | #undef FLATBUFFERS_TD |
240 | 0 | case BASE_TYPE_ARRAY: FLATBUFFERS_ASSERT(0); |
241 | 0 | } |
242 | | // clang-format on |
243 | 0 | return nullptr; |
244 | 0 | } |
245 | 0 | default: |
246 | 0 | FLATBUFFERS_ASSERT(0); |
247 | 0 | return "unknown type"; |
248 | 287k | } |
249 | 287k | } |
250 | | |
251 | | template <typename T> |
252 | 112k | static T GetFieldDefault(const FieldDef& fd) { |
253 | 112k | T val{}; |
254 | 112k | auto check = StringToNumber(fd.value.constant.c_str(), &val); |
255 | 112k | (void)check; |
256 | 112k | FLATBUFFERS_ASSERT(check); |
257 | 112k | return val; |
258 | 112k | } unsigned char flatbuffers::JsonPrinter::GetFieldDefault<unsigned char>(flatbuffers::FieldDef const&) Line | Count | Source | 252 | 8.88k | static T GetFieldDefault(const FieldDef& fd) { | 253 | 8.88k | T val{}; | 254 | 8.88k | auto check = StringToNumber(fd.value.constant.c_str(), &val); | 255 | 8.88k | (void)check; | 256 | 8.88k | FLATBUFFERS_ASSERT(check); | 257 | 8.88k | return val; | 258 | 8.88k | } |
signed char flatbuffers::JsonPrinter::GetFieldDefault<signed char>(flatbuffers::FieldDef const&) Line | Count | Source | 252 | 1.43k | static T GetFieldDefault(const FieldDef& fd) { | 253 | 1.43k | T val{}; | 254 | 1.43k | auto check = StringToNumber(fd.value.constant.c_str(), &val); | 255 | 1.43k | (void)check; | 256 | 1.43k | FLATBUFFERS_ASSERT(check); | 257 | 1.43k | return val; | 258 | 1.43k | } |
short flatbuffers::JsonPrinter::GetFieldDefault<short>(flatbuffers::FieldDef const&) Line | Count | Source | 252 | 2.79k | static T GetFieldDefault(const FieldDef& fd) { | 253 | 2.79k | T val{}; | 254 | 2.79k | auto check = StringToNumber(fd.value.constant.c_str(), &val); | 255 | 2.79k | (void)check; | 256 | 2.79k | FLATBUFFERS_ASSERT(check); | 257 | 2.79k | return val; | 258 | 2.79k | } |
unsigned short flatbuffers::JsonPrinter::GetFieldDefault<unsigned short>(flatbuffers::FieldDef const&) Line | Count | Source | 252 | 58.4k | static T GetFieldDefault(const FieldDef& fd) { | 253 | 58.4k | T val{}; | 254 | 58.4k | auto check = StringToNumber(fd.value.constant.c_str(), &val); | 255 | 58.4k | (void)check; | 256 | 58.4k | FLATBUFFERS_ASSERT(check); | 257 | 58.4k | return val; | 258 | 58.4k | } |
int flatbuffers::JsonPrinter::GetFieldDefault<int>(flatbuffers::FieldDef const&) Line | Count | Source | 252 | 1.18k | static T GetFieldDefault(const FieldDef& fd) { | 253 | 1.18k | T val{}; | 254 | 1.18k | auto check = StringToNumber(fd.value.constant.c_str(), &val); | 255 | 1.18k | (void)check; | 256 | 1.18k | FLATBUFFERS_ASSERT(check); | 257 | 1.18k | return val; | 258 | 1.18k | } |
unsigned int flatbuffers::JsonPrinter::GetFieldDefault<unsigned int>(flatbuffers::FieldDef const&) Line | Count | Source | 252 | 2.17k | static T GetFieldDefault(const FieldDef& fd) { | 253 | 2.17k | T val{}; | 254 | 2.17k | auto check = StringToNumber(fd.value.constant.c_str(), &val); | 255 | 2.17k | (void)check; | 256 | 2.17k | FLATBUFFERS_ASSERT(check); | 257 | 2.17k | return val; | 258 | 2.17k | } |
long flatbuffers::JsonPrinter::GetFieldDefault<long>(flatbuffers::FieldDef const&) Line | Count | Source | 252 | 5.38k | static T GetFieldDefault(const FieldDef& fd) { | 253 | 5.38k | T val{}; | 254 | 5.38k | auto check = StringToNumber(fd.value.constant.c_str(), &val); | 255 | 5.38k | (void)check; | 256 | 5.38k | FLATBUFFERS_ASSERT(check); | 257 | 5.38k | return val; | 258 | 5.38k | } |
unsigned long flatbuffers::JsonPrinter::GetFieldDefault<unsigned long>(flatbuffers::FieldDef const&) Line | Count | Source | 252 | 28.1k | static T GetFieldDefault(const FieldDef& fd) { | 253 | 28.1k | T val{}; | 254 | 28.1k | auto check = StringToNumber(fd.value.constant.c_str(), &val); | 255 | 28.1k | (void)check; | 256 | 28.1k | FLATBUFFERS_ASSERT(check); | 257 | 28.1k | return val; | 258 | 28.1k | } |
float flatbuffers::JsonPrinter::GetFieldDefault<float>(flatbuffers::FieldDef const&) Line | Count | Source | 252 | 2.55k | static T GetFieldDefault(const FieldDef& fd) { | 253 | 2.55k | T val{}; | 254 | 2.55k | auto check = StringToNumber(fd.value.constant.c_str(), &val); | 255 | 2.55k | (void)check; | 256 | 2.55k | FLATBUFFERS_ASSERT(check); | 257 | 2.55k | return val; | 258 | 2.55k | } |
double flatbuffers::JsonPrinter::GetFieldDefault<double>(flatbuffers::FieldDef const&) Line | Count | Source | 252 | 1.67k | static T GetFieldDefault(const FieldDef& fd) { | 253 | 1.67k | T val{}; | 254 | 1.67k | auto check = StringToNumber(fd.value.constant.c_str(), &val); | 255 | 1.67k | (void)check; | 256 | 1.67k | FLATBUFFERS_ASSERT(check); | 257 | 1.67k | return val; | 258 | 1.67k | } |
|
259 | | |
260 | | // Generate text for a scalar field. |
261 | | template <typename T> |
262 | | void GenField(const FieldDef& fd, const Table* table, bool fixed, |
263 | 174k | int indent) { |
264 | 174k | if (fixed) { |
265 | 61.6k | PrintScalar( |
266 | 61.6k | reinterpret_cast<const Struct*>(table)->GetField<T>(fd.value.offset), |
267 | 61.6k | fd.value.type, indent); |
268 | 112k | } else if (fd.IsOptional()) { |
269 | 0 | auto opt = table->GetOptional<T, T>(fd.value.offset); |
270 | 0 | if (opt) { |
271 | 0 | PrintScalar(*opt, fd.value.type, indent); |
272 | 0 | } else { |
273 | 0 | text += "null"; |
274 | 0 | } |
275 | 112k | } else { |
276 | 112k | PrintScalar(table->GetField<T>(fd.value.offset, GetFieldDefault<T>(fd)), |
277 | 112k | fd.value.type, indent); |
278 | 112k | } |
279 | 174k | } void flatbuffers::JsonPrinter::GenField<unsigned char>(flatbuffers::FieldDef const&, flatbuffers::Table const*, bool, int) Line | Count | Source | 263 | 10.8k | int indent) { | 264 | 10.8k | if (fixed) { | 265 | 2.01k | PrintScalar( | 266 | 2.01k | reinterpret_cast<const Struct*>(table)->GetField<T>(fd.value.offset), | 267 | 2.01k | fd.value.type, indent); | 268 | 8.88k | } else if (fd.IsOptional()) { | 269 | 0 | auto opt = table->GetOptional<T, T>(fd.value.offset); | 270 | 0 | if (opt) { | 271 | 0 | PrintScalar(*opt, fd.value.type, indent); | 272 | 0 | } else { | 273 | 0 | text += "null"; | 274 | 0 | } | 275 | 8.88k | } else { | 276 | 8.88k | PrintScalar(table->GetField<T>(fd.value.offset, GetFieldDefault<T>(fd)), | 277 | 8.88k | fd.value.type, indent); | 278 | 8.88k | } | 279 | 10.8k | } |
void flatbuffers::JsonPrinter::GenField<signed char>(flatbuffers::FieldDef const&, flatbuffers::Table const*, bool, int) Line | Count | Source | 263 | 4.80k | int indent) { | 264 | 4.80k | if (fixed) { | 265 | 3.36k | PrintScalar( | 266 | 3.36k | reinterpret_cast<const Struct*>(table)->GetField<T>(fd.value.offset), | 267 | 3.36k | fd.value.type, indent); | 268 | 3.36k | } else if (fd.IsOptional()) { | 269 | 0 | auto opt = table->GetOptional<T, T>(fd.value.offset); | 270 | 0 | if (opt) { | 271 | 0 | PrintScalar(*opt, fd.value.type, indent); | 272 | 0 | } else { | 273 | 0 | text += "null"; | 274 | 0 | } | 275 | 1.43k | } else { | 276 | 1.43k | PrintScalar(table->GetField<T>(fd.value.offset, GetFieldDefault<T>(fd)), | 277 | 1.43k | fd.value.type, indent); | 278 | 1.43k | } | 279 | 4.80k | } |
void flatbuffers::JsonPrinter::GenField<short>(flatbuffers::FieldDef const&, flatbuffers::Table const*, bool, int) Line | Count | Source | 263 | 6.16k | int indent) { | 264 | 6.16k | if (fixed) { | 265 | 3.36k | PrintScalar( | 266 | 3.36k | reinterpret_cast<const Struct*>(table)->GetField<T>(fd.value.offset), | 267 | 3.36k | fd.value.type, indent); | 268 | 3.36k | } else if (fd.IsOptional()) { | 269 | 0 | auto opt = table->GetOptional<T, T>(fd.value.offset); | 270 | 0 | if (opt) { | 271 | 0 | PrintScalar(*opt, fd.value.type, indent); | 272 | 0 | } else { | 273 | 0 | text += "null"; | 274 | 0 | } | 275 | 2.79k | } else { | 276 | 2.79k | PrintScalar(table->GetField<T>(fd.value.offset, GetFieldDefault<T>(fd)), | 277 | 2.79k | fd.value.type, indent); | 278 | 2.79k | } | 279 | 6.16k | } |
void flatbuffers::JsonPrinter::GenField<unsigned short>(flatbuffers::FieldDef const&, flatbuffers::Table const*, bool, int) Line | Count | Source | 263 | 58.4k | int indent) { | 264 | 58.4k | if (fixed) { | 265 | 0 | PrintScalar( | 266 | 0 | reinterpret_cast<const Struct*>(table)->GetField<T>(fd.value.offset), | 267 | 0 | fd.value.type, indent); | 268 | 58.4k | } else if (fd.IsOptional()) { | 269 | 0 | auto opt = table->GetOptional<T, T>(fd.value.offset); | 270 | 0 | if (opt) { | 271 | 0 | PrintScalar(*opt, fd.value.type, indent); | 272 | 0 | } else { | 273 | 0 | text += "null"; | 274 | 0 | } | 275 | 58.4k | } else { | 276 | 58.4k | PrintScalar(table->GetField<T>(fd.value.offset, GetFieldDefault<T>(fd)), | 277 | 58.4k | fd.value.type, indent); | 278 | 58.4k | } | 279 | 58.4k | } |
void flatbuffers::JsonPrinter::GenField<int>(flatbuffers::FieldDef const&, flatbuffers::Table const*, bool, int) Line | Count | Source | 263 | 1.18k | int indent) { | 264 | 1.18k | if (fixed) { | 265 | 0 | PrintScalar( | 266 | 0 | reinterpret_cast<const Struct*>(table)->GetField<T>(fd.value.offset), | 267 | 0 | fd.value.type, indent); | 268 | 1.18k | } else if (fd.IsOptional()) { | 269 | 0 | auto opt = table->GetOptional<T, T>(fd.value.offset); | 270 | 0 | if (opt) { | 271 | 0 | PrintScalar(*opt, fd.value.type, indent); | 272 | 0 | } else { | 273 | 0 | text += "null"; | 274 | 0 | } | 275 | 1.18k | } else { | 276 | 1.18k | PrintScalar(table->GetField<T>(fd.value.offset, GetFieldDefault<T>(fd)), | 277 | 1.18k | fd.value.type, indent); | 278 | 1.18k | } | 279 | 1.18k | } |
void flatbuffers::JsonPrinter::GenField<unsigned int>(flatbuffers::FieldDef const&, flatbuffers::Table const*, bool, int) Line | Count | Source | 263 | 47.0k | int indent) { | 264 | 47.0k | if (fixed) { | 265 | 44.8k | PrintScalar( | 266 | 44.8k | reinterpret_cast<const Struct*>(table)->GetField<T>(fd.value.offset), | 267 | 44.8k | fd.value.type, indent); | 268 | 44.8k | } else if (fd.IsOptional()) { | 269 | 0 | auto opt = table->GetOptional<T, T>(fd.value.offset); | 270 | 0 | if (opt) { | 271 | 0 | PrintScalar(*opt, fd.value.type, indent); | 272 | 0 | } else { | 273 | 0 | text += "null"; | 274 | 0 | } | 275 | 2.17k | } else { | 276 | 2.17k | PrintScalar(table->GetField<T>(fd.value.offset, GetFieldDefault<T>(fd)), | 277 | 2.17k | fd.value.type, indent); | 278 | 2.17k | } | 279 | 47.0k | } |
void flatbuffers::JsonPrinter::GenField<long>(flatbuffers::FieldDef const&, flatbuffers::Table const*, bool, int) Line | Count | Source | 263 | 5.38k | int indent) { | 264 | 5.38k | if (fixed) { | 265 | 0 | PrintScalar( | 266 | 0 | reinterpret_cast<const Struct*>(table)->GetField<T>(fd.value.offset), | 267 | 0 | fd.value.type, indent); | 268 | 5.38k | } else if (fd.IsOptional()) { | 269 | 0 | auto opt = table->GetOptional<T, T>(fd.value.offset); | 270 | 0 | if (opt) { | 271 | 0 | PrintScalar(*opt, fd.value.type, indent); | 272 | 0 | } else { | 273 | 0 | text += "null"; | 274 | 0 | } | 275 | 5.38k | } else { | 276 | 5.38k | PrintScalar(table->GetField<T>(fd.value.offset, GetFieldDefault<T>(fd)), | 277 | 5.38k | fd.value.type, indent); | 278 | 5.38k | } | 279 | 5.38k | } |
void flatbuffers::JsonPrinter::GenField<unsigned long>(flatbuffers::FieldDef const&, flatbuffers::Table const*, bool, int) Line | Count | Source | 263 | 28.1k | int indent) { | 264 | 28.1k | if (fixed) { | 265 | 0 | PrintScalar( | 266 | 0 | reinterpret_cast<const Struct*>(table)->GetField<T>(fd.value.offset), | 267 | 0 | fd.value.type, indent); | 268 | 28.1k | } else if (fd.IsOptional()) { | 269 | 0 | auto opt = table->GetOptional<T, T>(fd.value.offset); | 270 | 0 | if (opt) { | 271 | 0 | PrintScalar(*opt, fd.value.type, indent); | 272 | 0 | } else { | 273 | 0 | text += "null"; | 274 | 0 | } | 275 | 28.1k | } else { | 276 | 28.1k | PrintScalar(table->GetField<T>(fd.value.offset, GetFieldDefault<T>(fd)), | 277 | 28.1k | fd.value.type, indent); | 278 | 28.1k | } | 279 | 28.1k | } |
void flatbuffers::JsonPrinter::GenField<float>(flatbuffers::FieldDef const&, flatbuffers::Table const*, bool, int) Line | Count | Source | 263 | 8.59k | int indent) { | 264 | 8.59k | if (fixed) { | 265 | 6.03k | PrintScalar( | 266 | 6.03k | reinterpret_cast<const Struct*>(table)->GetField<T>(fd.value.offset), | 267 | 6.03k | fd.value.type, indent); | 268 | 6.03k | } else if (fd.IsOptional()) { | 269 | 0 | auto opt = table->GetOptional<T, T>(fd.value.offset); | 270 | 0 | if (opt) { | 271 | 0 | PrintScalar(*opt, fd.value.type, indent); | 272 | 0 | } else { | 273 | 0 | text += "null"; | 274 | 0 | } | 275 | 2.55k | } else { | 276 | 2.55k | PrintScalar(table->GetField<T>(fd.value.offset, GetFieldDefault<T>(fd)), | 277 | 2.55k | fd.value.type, indent); | 278 | 2.55k | } | 279 | 8.59k | } |
void flatbuffers::JsonPrinter::GenField<double>(flatbuffers::FieldDef const&, flatbuffers::Table const*, bool, int) Line | Count | Source | 263 | 3.68k | int indent) { | 264 | 3.68k | if (fixed) { | 265 | 2.01k | PrintScalar( | 266 | 2.01k | reinterpret_cast<const Struct*>(table)->GetField<T>(fd.value.offset), | 267 | 2.01k | fd.value.type, indent); | 268 | 2.01k | } else if (fd.IsOptional()) { | 269 | 0 | auto opt = table->GetOptional<T, T>(fd.value.offset); | 270 | 0 | if (opt) { | 271 | 0 | PrintScalar(*opt, fd.value.type, indent); | 272 | 0 | } else { | 273 | 0 | text += "null"; | 274 | 0 | } | 275 | 1.67k | } else { | 276 | 1.67k | PrintScalar(table->GetField<T>(fd.value.offset, GetFieldDefault<T>(fd)), | 277 | 1.67k | fd.value.type, indent); | 278 | 1.67k | } | 279 | 3.68k | } |
|
280 | | |
281 | | // Generate text for non-scalar field. |
282 | | const char* GenFieldOffset(const FieldDef& fd, const Table* table, bool fixed, |
283 | 103k | int indent, const uint8_t* prev_val) { |
284 | 103k | const void* val = nullptr; |
285 | 103k | if (fixed) { |
286 | | // The only non-scalar fields in structs are structs or arrays. |
287 | 2.01k | FLATBUFFERS_ASSERT(IsStruct(fd.value.type) || IsArray(fd.value.type)); |
288 | 2.01k | val = reinterpret_cast<const Struct*>(table)->GetStruct<const void*>( |
289 | 2.01k | fd.value.offset); |
290 | 101k | } else if (fd.flexbuffer && opts.json_nested_flexbuffers) { |
291 | | // We could verify this FlexBuffer before access, but since this sits |
292 | | // inside a FlatBuffer that we don't know wether it has been verified or |
293 | | // not, there is little point making this part safer than the parent.. |
294 | | // The caller should really be verifying the whole. |
295 | | // If the whole buffer is corrupt, we likely crash before we even get |
296 | | // here. |
297 | 11.5k | auto vec = table->GetPointer<const Vector<uint8_t>*>(fd.value.offset); |
298 | 11.5k | auto root = flexbuffers::GetRoot(vec->data(), vec->size()); |
299 | 11.5k | root.ToString(true, opts.strict_json, text); |
300 | 11.5k | return nullptr; |
301 | 90.2k | } else if (fd.nested_flatbuffer && opts.json_nested_flatbuffers) { |
302 | 6.98k | auto vec = table->GetPointer<const Vector<uint8_t>*>(fd.value.offset); |
303 | 6.98k | auto root = GetRoot<Table>(vec->data()); |
304 | 6.98k | return GenStruct(*fd.nested_flatbuffer, root, indent); |
305 | 83.3k | } else { |
306 | 83.3k | val = IsStruct(fd.value.type) |
307 | 83.3k | ? table->GetStruct<const void*>(fd.value.offset) |
308 | 83.3k | : table->GetPointer<const void*>(fd.value.offset); |
309 | 83.3k | } |
310 | 85.3k | return PrintOffset(val, fd.value.type, indent, prev_val, -1); |
311 | 103k | } |
312 | | |
313 | | // Generate text for a struct or table, values separated by commas, indented, |
314 | | // and bracketed by "{}" |
315 | | const char* GenStruct(const StructDef& struct_def, const Table* table, |
316 | 175k | int indent) { |
317 | 175k | text += '{'; |
318 | 175k | int fieldout = 0; |
319 | 175k | const uint8_t* prev_val = nullptr; |
320 | 175k | const auto elem_indent = indent + Indent(); |
321 | 175k | for (auto it = struct_def.fields.vec.begin(); |
322 | 4.25M | it != struct_def.fields.vec.end(); ++it) { |
323 | 4.08M | FieldDef& fd = **it; |
324 | 4.08M | auto is_present = struct_def.fixed || table->CheckField(fd.value.offset); |
325 | 4.08M | auto output_anyway = (opts.output_default_scalars_in_json || fd.key) && |
326 | 168k | IsScalar(fd.value.type.base_type) && !fd.deprecated; |
327 | 4.08M | if (is_present || output_anyway) { |
328 | 278k | if (fieldout++) { |
329 | 103k | AddComma(); |
330 | 103k | } |
331 | 278k | AddNewLine(); |
332 | 278k | AddIndent(elem_indent); |
333 | 278k | OutputIdentifier(fd.name); |
334 | 278k | if (!opts.protobuf_ascii_alike || |
335 | 0 | (fd.value.type.base_type != BASE_TYPE_STRUCT && |
336 | 0 | fd.value.type.base_type != BASE_TYPE_VECTOR)) |
337 | 278k | text += ':'; |
338 | 278k | text += ' '; |
339 | | // clang-format off |
340 | 278k | switch (fd.value.type.base_type) { |
341 | 0 | #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, ...) \ |
342 | 174k | case BASE_TYPE_ ## ENUM: { \ |
343 | 174k | GenField<CTYPE>(fd, table, struct_def.fixed, elem_indent); \ |
344 | 174k | break; } |
345 | 174k | FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD) |
346 | 0 | #undef FLATBUFFERS_TD |
347 | | // Generate drop-thru case statements for all pointer types: |
348 | 0 | #define FLATBUFFERS_TD(ENUM, ...) \ |
349 | 554k | case BASE_TYPE_ ## ENUM: |
350 | 450k | FLATBUFFERS_GEN_TYPES_POINTER(FLATBUFFERS_TD) |
351 | 450k | FLATBUFFERS_GEN_TYPE_ARRAY(FLATBUFFERS_TD) |
352 | 103k | #undef FLATBUFFERS_TD |
353 | 103k | { |
354 | 103k | auto err = GenFieldOffset(fd, table, struct_def.fixed, elem_indent, prev_val); |
355 | 103k | if (err) return err; |
356 | 103k | break; |
357 | 103k | } |
358 | 278k | } |
359 | | // clang-format on |
360 | | // Track prev val for use with union types. |
361 | 278k | if (struct_def.fixed) { |
362 | 63.6k | prev_val = reinterpret_cast<const uint8_t*>(table) + fd.value.offset; |
363 | 214k | } else { |
364 | 214k | prev_val = table->GetAddressOf(fd.value.offset); |
365 | 214k | } |
366 | 278k | } |
367 | 4.08M | } |
368 | 175k | AddNewLine(); |
369 | 175k | AddIndent(indent); |
370 | 175k | text += '}'; |
371 | 175k | return nullptr; |
372 | 175k | } |
373 | | |
374 | | JsonPrinter(const Parser& parser, std::string& dest) |
375 | 8.25k | : opts(parser.opts), text(dest) { |
376 | 8.25k | text.reserve(1024); // Reduce amount of inevitable reallocs. |
377 | 8.25k | } |
378 | | |
379 | | const IDLOptions& opts; |
380 | | std::string& text; |
381 | | }; |
382 | | |
383 | | static const char* GenerateTextImpl(const Parser& parser, const Table* table, |
384 | | const StructDef& struct_def, |
385 | 8.25k | std::string* _text) { |
386 | 8.25k | JsonPrinter printer(parser, *_text); |
387 | 8.25k | auto err = printer.GenStruct(struct_def, table, 0); |
388 | 8.25k | if (err) return err; |
389 | 8.25k | printer.AddNewLine(); |
390 | 8.25k | return nullptr; |
391 | 8.25k | } |
392 | | |
393 | | // Generate a text representation of a flatbuffer in JSON format. |
394 | | // Deprecated: please use `GenTextFromTable` |
395 | | bool GenerateTextFromTable(const Parser& parser, const void* table, |
396 | 0 | const std::string& table_name, std::string* _text) { |
397 | 0 | return GenTextFromTable(parser, table, table_name, _text) != nullptr; |
398 | 0 | } |
399 | | |
400 | | // Generate a text representation of a flatbuffer in JSON format. |
401 | | const char* GenTextFromTable(const Parser& parser, const void* table, |
402 | | const std::string& table_name, |
403 | 0 | std::string* _text) { |
404 | 0 | auto struct_def = parser.LookupStruct(table_name); |
405 | 0 | if (struct_def == nullptr) { |
406 | 0 | return "unknown struct"; |
407 | 0 | } |
408 | 0 | auto root = static_cast<const Table*>(table); |
409 | 0 | return GenerateTextImpl(parser, root, *struct_def, _text); |
410 | 0 | } |
411 | | |
412 | | // Deprecated: please use `GenText` |
413 | | const char* GenerateText(const Parser& parser, const void* flatbuffer, |
414 | 0 | std::string* _text) { |
415 | 0 | return GenText(parser, flatbuffer, _text); |
416 | 0 | } |
417 | | |
418 | | // Generate a text representation of a flatbuffer in JSON format. |
419 | | const char* GenText(const Parser& parser, const void* flatbuffer, |
420 | 8.25k | std::string* _text) { |
421 | 8.25k | FLATBUFFERS_ASSERT(parser.root_struct_def_); // call SetRootType() |
422 | 8.25k | auto root = parser.opts.size_prefixed ? GetSizePrefixedRoot<Table>(flatbuffer) |
423 | 8.25k | : GetRoot<Table>(flatbuffer); |
424 | 8.25k | return GenerateTextImpl(parser, root, *parser.root_struct_def_, _text); |
425 | 8.25k | } |
426 | | |
427 | | static std::string TextFileName(const std::string& path, |
428 | 0 | const std::string& file_name) { |
429 | 0 | return path + file_name + ".json"; |
430 | 0 | } |
431 | | |
432 | | // Deprecated: please use `GenTextFile` |
433 | | const char* GenerateTextFile(const Parser& parser, const std::string& path, |
434 | 0 | const std::string& file_name) { |
435 | 0 | return GenTextFile(parser, path, file_name); |
436 | 0 | } |
437 | | |
438 | | const char* GenTextFile(const Parser& parser, const std::string& path, |
439 | 0 | const std::string& file_name) { |
440 | 0 | if (parser.opts.use_flexbuffers) { |
441 | 0 | std::string json; |
442 | 0 | parser.flex_root_.ToString(true, parser.opts.strict_json, json); |
443 | 0 | return flatbuffers::SaveFile(TextFileName(path, file_name).c_str(), |
444 | 0 | json.c_str(), json.size(), true) |
445 | 0 | ? nullptr |
446 | 0 | : "SaveFile failed"; |
447 | 0 | } |
448 | 0 | if (!parser.builder_.GetSize() || !parser.root_struct_def_) return nullptr; |
449 | 0 | std::string text; |
450 | 0 | auto err = GenText(parser, parser.builder_.GetBufferPointer(), &text); |
451 | 0 | if (err) return err; |
452 | 0 | return flatbuffers::SaveFile(TextFileName(path, file_name).c_str(), text, |
453 | 0 | false) |
454 | 0 | ? nullptr |
455 | 0 | : "SaveFile failed"; |
456 | 0 | } |
457 | | |
458 | | static std::string TextMakeRule(const Parser& parser, const std::string& path, |
459 | 0 | const std::string& file_name) { |
460 | 0 | if (!parser.builder_.GetSize() || !parser.root_struct_def_) return ""; |
461 | 0 | std::string filebase = |
462 | 0 | flatbuffers::StripPath(flatbuffers::StripExtension(file_name)); |
463 | 0 | std::string make_rule = TextFileName(path, filebase) + ": " + file_name; |
464 | 0 | auto included_files = |
465 | 0 | parser.GetIncludedFilesRecursive(parser.root_struct_def_->file); |
466 | 0 | for (auto it = included_files.begin(); it != included_files.end(); ++it) { |
467 | 0 | make_rule += " " + *it; |
468 | 0 | } |
469 | 0 | return make_rule; |
470 | 0 | } |
471 | | |
472 | | namespace { |
473 | | |
474 | | class TextCodeGenerator : public CodeGenerator { |
475 | | public: |
476 | | Status GenerateCode(const Parser& parser, const std::string& path, |
477 | 0 | const std::string& filename) override { |
478 | 0 | auto err = GenTextFile(parser, path, filename); |
479 | 0 | if (err) { |
480 | 0 | status_detail = " (" + std::string(err) + ")"; |
481 | 0 | return Status::ERROR; |
482 | 0 | } |
483 | 0 | return Status::OK; |
484 | 0 | } |
485 | | |
486 | | // Generate code from the provided `buffer` of given `length`. The buffer is a |
487 | | // serialized reflection.fbs. |
488 | 0 | Status GenerateCode(const uint8_t*, int64_t, const CodeGenOptions&) override { |
489 | 0 | return Status::NOT_IMPLEMENTED; |
490 | 0 | } |
491 | | |
492 | | Status GenerateMakeRule(const Parser& parser, const std::string& path, |
493 | | const std::string& filename, |
494 | 0 | std::string& output) override { |
495 | 0 | output = TextMakeRule(parser, path, filename); |
496 | 0 | return Status::OK; |
497 | 0 | } |
498 | | |
499 | | Status GenerateGrpcCode(const Parser& parser, const std::string& path, |
500 | 0 | const std::string& filename) override { |
501 | 0 | (void)parser; |
502 | 0 | (void)path; |
503 | 0 | (void)filename; |
504 | 0 | return Status::NOT_IMPLEMENTED; |
505 | 0 | } |
506 | | |
507 | | Status GenerateRootFile(const Parser& parser, |
508 | 0 | const std::string& path) override { |
509 | 0 | (void)parser; |
510 | 0 | (void)path; |
511 | 0 | return Status::NOT_IMPLEMENTED; |
512 | 0 | } |
513 | | |
514 | 0 | bool IsSchemaOnly() const override { return false; } |
515 | | |
516 | 0 | bool SupportsBfbsGeneration() const override { return false; } |
517 | | |
518 | 0 | bool SupportsRootFileGeneration() const override { return false; } |
519 | | |
520 | 0 | IDLOptions::Language Language() const override { return IDLOptions::kJson; } |
521 | | |
522 | 0 | std::string LanguageName() const override { return "text"; } |
523 | | }; |
524 | | |
525 | | } // namespace |
526 | | |
527 | 0 | std::unique_ptr<CodeGenerator> NewTextCodeGenerator() { |
528 | 0 | return std::unique_ptr<TextCodeGenerator>(new TextCodeGenerator()); |
529 | 0 | } |
530 | | |
531 | | } // namespace flatbuffers |