Austin Schuh | d393620 | 2020-04-07 20:11:07 -0700 | [diff] [blame] | 1 | #include <cmath> |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 2 | #include <iostream> |
| 3 | #include <sstream> |
| 4 | |
| 5 | #include "aos/json_to_flatbuffer.h" |
| 6 | |
| 7 | namespace aos { |
| 8 | |
| 9 | namespace { |
| 10 | |
| 11 | using reflection::BaseType; |
| 12 | |
| 13 | void IntToString(int64_t val, reflection::BaseType type, |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 14 | FastStringBuilder *out) { |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 15 | switch (type) { |
| 16 | case BaseType::Bool: |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 17 | out->AppendBool(static_cast<bool>(val)); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 18 | break; |
| 19 | case BaseType::UByte: |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 20 | out->AppendInt(static_cast<uint8_t>(val)); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 21 | break; |
| 22 | case BaseType::Byte: |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 23 | out->AppendInt(static_cast<int8_t>(val)); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 24 | break; |
| 25 | case BaseType::Short: |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 26 | out->AppendInt(static_cast<int16_t>(val)); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 27 | break; |
| 28 | case BaseType::UShort: |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 29 | out->AppendInt(static_cast<uint16_t>(val)); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 30 | break; |
| 31 | case BaseType::Int: |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 32 | out->AppendInt(static_cast<int32_t>(val)); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 33 | break; |
| 34 | case BaseType::UInt: |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 35 | out->AppendInt(static_cast<uint32_t>(val)); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 36 | break; |
| 37 | case BaseType::Long: |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 38 | out->AppendInt(static_cast<int64_t>(val)); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 39 | break; |
| 40 | case BaseType::ULong: |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 41 | out->AppendInt(static_cast<uint64_t>(val)); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 42 | break; |
| 43 | default: |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 44 | out->Append("null"); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
| 48 | void FloatToString(double val, reflection::BaseType type, |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 49 | FastStringBuilder *out) { |
Austin Schuh | d393620 | 2020-04-07 20:11:07 -0700 | [diff] [blame] | 50 | if (std::isnan(val)) { |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 51 | out->Append("null"); |
Austin Schuh | d393620 | 2020-04-07 20:11:07 -0700 | [diff] [blame] | 52 | return; |
| 53 | } |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 54 | switch (type) { |
| 55 | case BaseType::Float: |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 56 | out->Append(static_cast<float>(val)); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 57 | break; |
| 58 | case BaseType::Double: |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 59 | out->Append(val); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 60 | break; |
| 61 | default: |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 62 | out->Append("null"); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | template <typename ObjT> |
| 67 | void ObjectToString( |
| 68 | const reflection::Object *obj, |
| 69 | const flatbuffers::Vector<flatbuffers::Offset<reflection::Object>> *objects, |
| 70 | const flatbuffers::Vector<flatbuffers::Offset<reflection::Enum>> *enums, |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 71 | const ObjT *object, FastStringBuilder *out, JsonOptions json_options, |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 72 | int tree_depth = 0); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 73 | |
| 74 | // Get enum value name |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 75 | std::string_view EnumToString( |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 76 | int64_t enum_value, |
| 77 | const flatbuffers::Vector<flatbuffers::Offset<reflection::EnumVal>> |
| 78 | *values) { |
Tyler Chatow | 905f94c | 2020-09-30 20:07:37 -0700 | [diff] [blame] | 79 | auto search = values->LookupByKey(enum_value); |
| 80 | return search != nullptr ? search->name()->string_view() : std::string_view(); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | // Convert integer to string, checking if it is an enum. |
| 84 | void IntOrEnumToString( |
| 85 | int64_t val, const reflection::Type *type, |
| 86 | const flatbuffers::Vector<flatbuffers::Offset<reflection::Enum>> *enums, |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 87 | FastStringBuilder *out) { |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 88 | // Check if integer is an enum and print string, otherwise fallback to |
| 89 | // printing as int. |
| 90 | if (type->index() > -1 && type->index() < (int32_t)enums->size()) { |
| 91 | const reflection::Enum *enum_props = enums->Get(type->index()); |
| 92 | if (!enum_props->is_union()) { |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 93 | const std::string_view value_string = |
| 94 | EnumToString(val, enum_props->values()); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 95 | |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 96 | if (value_string.data() != nullptr) { |
| 97 | out->AppendChar('"'); |
| 98 | out->Append(value_string); |
| 99 | out->AppendChar('"'); |
Austin Schuh | d393620 | 2020-04-07 20:11:07 -0700 | [diff] [blame] | 100 | } else { |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 101 | out->AppendInt(val); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | } else { |
| 105 | if (type->base_type() == BaseType::Vector || |
| 106 | type->base_type() == BaseType::Array) { |
| 107 | IntToString(val, type->element(), out); |
| 108 | } else { |
| 109 | IntToString(val, type->base_type(), out); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
Ravago Jones | e8338b0 | 2020-04-16 15:43:00 -0700 | [diff] [blame] | 114 | // Adds a newline and indents |
| 115 | // Every increment in tree depth is two spaces |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 116 | void AddWrapping(FastStringBuilder *out, int tree_depth) { |
| 117 | out->Append("\n"); |
Ravago Jones | e8338b0 | 2020-04-16 15:43:00 -0700 | [diff] [blame] | 118 | for (int i = 0; i < tree_depth; i++) { |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 119 | out->Append(" "); |
Ravago Jones | e8338b0 | 2020-04-16 15:43:00 -0700 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| 123 | // Detects if a field should trigger wrapping of the parent object. |
| 124 | bool ShouldCauseWrapping(reflection::BaseType type) { |
| 125 | switch (type) { |
| 126 | case BaseType::Vector: |
| 127 | case BaseType::Obj: |
| 128 | return true; |
| 129 | default: |
| 130 | return false; |
| 131 | } |
| 132 | } |
| 133 | |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 134 | // Print field in flatbuffer table. Field must be populated. |
| 135 | template <typename ObjT> |
| 136 | void FieldToString( |
| 137 | const ObjT *table, const reflection::Field *field, |
| 138 | const flatbuffers::Vector<flatbuffers::Offset<reflection::Object>> *objects, |
| 139 | const flatbuffers::Vector<flatbuffers::Offset<reflection::Enum>> *enums, |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 140 | FastStringBuilder *out, JsonOptions json_options, int tree_depth) { |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 141 | const reflection::Type *type = field->type(); |
| 142 | |
| 143 | switch (type->base_type()) { |
| 144 | case BaseType::Bool: |
| 145 | case BaseType::UByte: |
| 146 | case BaseType::Byte: |
| 147 | case BaseType::Short: |
| 148 | case BaseType::UShort: |
| 149 | case BaseType::Int: |
| 150 | case BaseType::UInt: |
| 151 | case BaseType::Long: |
| 152 | case BaseType::ULong: |
| 153 | IntOrEnumToString(GetAnyFieldI(*table, *field), type, enums, out); |
| 154 | break; |
| 155 | case BaseType::Float: |
| 156 | case BaseType::Double: |
| 157 | FloatToString(GetAnyFieldF(*table, *field), type->base_type(), out); |
| 158 | break; |
| 159 | case BaseType::String: |
| 160 | if constexpr (std::is_same<flatbuffers::Table, ObjT>()) { |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 161 | flatbuffers::string_view str = |
| 162 | flatbuffers::GetFieldS(*table, *field)->string_view(); |
| 163 | out->AppendChar('"'); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 164 | for (char c : str) { |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 165 | switch (c) { |
| 166 | case '"': |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 167 | out->Append("\\\""); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 168 | break; |
| 169 | case '\\': |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 170 | out->Append("\\\\"); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 171 | break; |
| 172 | case '\b': |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 173 | out->Append("\\b"); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 174 | break; |
| 175 | case '\f': |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 176 | out->Append("\\f"); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 177 | break; |
| 178 | case '\n': |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 179 | out->Append("\\n"); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 180 | break; |
| 181 | case '\r': |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 182 | out->Append("\\r"); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 183 | break; |
| 184 | case '\t': |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 185 | out->Append("\\t"); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 186 | break; |
| 187 | default: |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 188 | out->AppendChar(c); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 189 | } |
| 190 | } |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 191 | out->AppendChar('"'); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 192 | } else { |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 193 | out->Append("null"); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 194 | } |
| 195 | break; |
| 196 | case BaseType::Vector: { |
| 197 | if constexpr (std::is_same<flatbuffers::Table, ObjT>()) { |
| 198 | const flatbuffers::VectorOfAny *vector = |
| 199 | flatbuffers::GetFieldAnyV(*table, *field); |
| 200 | reflection::BaseType elem_type = type->element(); |
| 201 | |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 202 | if (vector->size() > json_options.max_vector_size) { |
Austin Schuh | 041fe9f | 2021-10-16 23:01:15 -0700 | [diff] [blame] | 203 | out->Append("[ \"... "); |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 204 | out->AppendInt(vector->size()); |
Austin Schuh | 041fe9f | 2021-10-16 23:01:15 -0700 | [diff] [blame] | 205 | out->Append(" elements ...\" ]"); |
Austin Schuh | d393620 | 2020-04-07 20:11:07 -0700 | [diff] [blame] | 206 | break; |
| 207 | } |
Ravago Jones | e8338b0 | 2020-04-16 15:43:00 -0700 | [diff] [blame] | 208 | |
| 209 | bool wrap = false; |
| 210 | const int child_tree_depth = tree_depth + 1; |
| 211 | |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 212 | if (json_options.multi_line) { |
Ravago Jones | e8338b0 | 2020-04-16 15:43:00 -0700 | [diff] [blame] | 213 | wrap = ShouldCauseWrapping(elem_type); |
| 214 | } |
| 215 | |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 216 | out->AppendChar('['); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 217 | for (flatbuffers::uoffset_t i = 0; i < vector->size(); ++i) { |
| 218 | if (i != 0) { |
Ravago Jones | e8338b0 | 2020-04-16 15:43:00 -0700 | [diff] [blame] | 219 | if (wrap) { |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 220 | out->Append(","); |
Ravago Jones | e8338b0 | 2020-04-16 15:43:00 -0700 | [diff] [blame] | 221 | } else { |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 222 | out->Append(", "); |
Ravago Jones | e8338b0 | 2020-04-16 15:43:00 -0700 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | if (wrap) { |
| 226 | AddWrapping(out, child_tree_depth); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 227 | } |
| 228 | if (flatbuffers::IsInteger(elem_type)) { |
| 229 | IntOrEnumToString( |
| 230 | flatbuffers::GetAnyVectorElemI(vector, elem_type, i), type, |
| 231 | enums, out); |
| 232 | } else if (flatbuffers::IsFloat(elem_type)) { |
| 233 | FloatToString(flatbuffers::GetAnyVectorElemF(vector, elem_type, i), |
| 234 | elem_type, out); |
| 235 | } else if (elem_type == BaseType::String) { |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 236 | out->AppendChar('"'); |
| 237 | out->Append(flatbuffers::GetAnyVectorElemS(vector, elem_type, i)); |
| 238 | out->AppendChar('"'); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 239 | } else if (elem_type == BaseType::Obj) { |
| 240 | if (type->index() > -1 && |
| 241 | type->index() < (int32_t)objects->size()) { |
| 242 | if (objects->Get(type->index())->is_struct()) { |
| 243 | ObjectToString( |
| 244 | objects->Get(type->index()), objects, enums, |
| 245 | flatbuffers::GetAnyVectorElemAddressOf< |
| 246 | const flatbuffers::Struct>( |
| 247 | vector, i, objects->Get(type->index())->bytesize()), |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 248 | out, json_options, child_tree_depth); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 249 | } else { |
| 250 | ObjectToString(objects->Get(type->index()), objects, enums, |
| 251 | flatbuffers::GetAnyVectorElemPointer< |
| 252 | const flatbuffers::Table>(vector, i), |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 253 | out, json_options, child_tree_depth); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | } |
| 257 | } |
Ravago Jones | e8338b0 | 2020-04-16 15:43:00 -0700 | [diff] [blame] | 258 | if (wrap) { |
| 259 | AddWrapping(out, tree_depth); |
| 260 | } |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 261 | out->AppendChar(']'); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 262 | } else { |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 263 | out->Append("null"); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 264 | } |
| 265 | } break; |
| 266 | case BaseType::Obj: { |
| 267 | if (type->index() > -1 && type->index() < (int32_t)objects->size()) { |
| 268 | if (objects->Get(type->index())->is_struct()) { |
| 269 | ObjectToString(objects->Get(type->index()), objects, enums, |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 270 | flatbuffers::GetFieldStruct(*table, *field), out, |
| 271 | json_options, tree_depth); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 272 | } else if constexpr (std::is_same<flatbuffers::Table, ObjT>()) { |
| 273 | ObjectToString(objects->Get(type->index()), objects, enums, |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 274 | flatbuffers::GetFieldT(*table, *field), out, |
| 275 | json_options, tree_depth); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 276 | } |
| 277 | } else { |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 278 | out->Append("null"); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 279 | } |
| 280 | } break; |
| 281 | default: |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 282 | out->Append("null"); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
| 286 | // Prints flatbuffer table or struct given list of possible child objects and |
| 287 | // enums. Prints "null" if the child object type is not found. |
| 288 | template <typename ObjT> |
| 289 | void ObjectToString( |
| 290 | const reflection::Object *obj, |
| 291 | const flatbuffers::Vector<flatbuffers::Offset<reflection::Object>> *objects, |
| 292 | const flatbuffers::Vector<flatbuffers::Offset<reflection::Enum>> *enums, |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 293 | const ObjT *object, FastStringBuilder *out, JsonOptions json_options, |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 294 | int tree_depth) { |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 295 | static_assert(std::is_same<flatbuffers::Table, ObjT>() || |
| 296 | std::is_same<flatbuffers::Struct, ObjT>(), |
| 297 | "Type must be either flatbuffer table or struct"); |
| 298 | bool print_sep = false; |
Ravago Jones | e8338b0 | 2020-04-16 15:43:00 -0700 | [diff] [blame] | 299 | |
| 300 | const int child_tree_depth = tree_depth + 1; |
| 301 | |
| 302 | bool wrap = false; |
Dan Ford | 44e0251 | 2022-06-07 23:45:14 -0700 | [diff] [blame^] | 303 | if (json_options.max_multi_line) { |
| 304 | wrap = true; |
| 305 | } else if (json_options.multi_line) { |
Ravago Jones | e8338b0 | 2020-04-16 15:43:00 -0700 | [diff] [blame] | 306 | // Check whether this object has objects, vectors, or floats inside of it |
| 307 | for (const reflection::Field *field : *obj->fields()) { |
| 308 | if (ShouldCauseWrapping(field->type()->base_type())) { |
| 309 | wrap = true; |
| 310 | break; |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 315 | out->AppendChar('{'); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 316 | for (const reflection::Field *field : *obj->fields()) { |
| 317 | // Check whether this object has the field populated (even for structs, |
| 318 | // which should have all fields populated) |
| 319 | if (object->GetAddressOf(field->offset())) { |
| 320 | if (print_sep) { |
Ravago Jones | e8338b0 | 2020-04-16 15:43:00 -0700 | [diff] [blame] | 321 | if (wrap) { |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 322 | out->Append(","); |
Ravago Jones | e8338b0 | 2020-04-16 15:43:00 -0700 | [diff] [blame] | 323 | } else { |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 324 | out->Append(", "); |
Ravago Jones | e8338b0 | 2020-04-16 15:43:00 -0700 | [diff] [blame] | 325 | } |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 326 | } else { |
| 327 | print_sep = true; |
| 328 | } |
Ravago Jones | e8338b0 | 2020-04-16 15:43:00 -0700 | [diff] [blame] | 329 | |
| 330 | if (wrap) { |
| 331 | AddWrapping(out, child_tree_depth); |
| 332 | } |
| 333 | |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 334 | out->AppendChar('"'); |
| 335 | out->Append(field->name()->string_view()); |
| 336 | out->Append("\": "); |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 337 | FieldToString(object, field, objects, enums, out, json_options, |
| 338 | child_tree_depth); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 339 | } |
| 340 | } |
Ravago Jones | e8338b0 | 2020-04-16 15:43:00 -0700 | [diff] [blame] | 341 | |
| 342 | if (wrap) { |
| 343 | AddWrapping(out, tree_depth); |
| 344 | } |
| 345 | |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 346 | out->AppendChar('}'); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | } // namespace |
| 350 | |
| 351 | std::string FlatbufferToJson(const reflection::Schema *schema, |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 352 | const uint8_t *data, JsonOptions json_options) { |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 353 | FastStringBuilder builder; |
| 354 | FlatbufferToJson(&builder, schema, data, json_options); |
| 355 | return builder.MoveResult(); |
| 356 | } |
| 357 | |
| 358 | void FlatbufferToJson(FastStringBuilder *builder, |
| 359 | const reflection::Schema *schema, const uint8_t *data, |
| 360 | JsonOptions json_options) { |
Austin Schuh | 7f99e47 | 2020-06-17 20:38:17 -0700 | [diff] [blame] | 361 | CHECK(schema != nullptr) << ": Need to provide a schema"; |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 362 | CHECK(builder != nullptr) << ": Need to provide an output builder"; |
Austin Schuh | 7f99e47 | 2020-06-17 20:38:17 -0700 | [diff] [blame] | 363 | |
| 364 | // It is pretty common to get passed in a nullptr when a test fails. Rather |
| 365 | // than CHECK, return a more user friendly result. |
| 366 | if (data == nullptr) { |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 367 | builder->Append("null"); |
| 368 | return; |
Austin Schuh | 7f99e47 | 2020-06-17 20:38:17 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 371 | const flatbuffers::Table *table = flatbuffers::GetAnyRoot(data); |
| 372 | |
| 373 | const reflection::Object *obj = schema->root_table(); |
| 374 | |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 375 | ObjectToString(obj, schema->objects(), schema->enums(), table, builder, |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 376 | json_options); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 377 | } |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 378 | |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 379 | } // namespace aos |