| 1053 | } |
| 1054 | |
| 1055 | CheckedError Parser::ParseAnyValue(Value &val, FieldDef *field, |
| 1056 | size_t parent_fieldn, |
| 1057 | const StructDef *parent_struct_def, |
| 1058 | uoffset_t count, bool inside_vector) { |
| 1059 | switch (val.type.base_type) { |
| 1060 | case BASE_TYPE_UNION: { |
| 1061 | FLATBUFFERS_ASSERT(field); |
| 1062 | std::string constant; |
| 1063 | Vector<uint8_t> *vector_of_union_types = nullptr; |
| 1064 | // Find corresponding type field we may have already parsed. |
| 1065 | for (auto elem = field_stack_.rbegin() + count; |
| 1066 | elem != field_stack_.rbegin() + parent_fieldn + count; ++elem) { |
| 1067 | auto &type = elem->second->value.type; |
| 1068 | if (type.enum_def == val.type.enum_def) { |
| 1069 | if (inside_vector) { |
| 1070 | if (IsVector(type) && type.element == BASE_TYPE_UTYPE) { |
| 1071 | // Vector of union type field. |
| 1072 | uoffset_t offset; |
| 1073 | ECHECK(atot(elem->first.constant.c_str(), *this, &offset)); |
| 1074 | vector_of_union_types = reinterpret_cast<Vector<uint8_t> *>( |
| 1075 | builder_.GetCurrentBufferPointer() + builder_.GetSize() - |
| 1076 | offset); |
| 1077 | break; |
| 1078 | } |
| 1079 | } else { |
| 1080 | if (type.base_type == BASE_TYPE_UTYPE) { |
| 1081 | // Union type field. |
| 1082 | constant = elem->first.constant; |
| 1083 | break; |
| 1084 | } |
| 1085 | } |
| 1086 | } |
| 1087 | } |
| 1088 | if (constant.empty() && !inside_vector) { |
| 1089 | // We haven't seen the type field yet. Sadly a lot of JSON writers |
| 1090 | // output these in alphabetical order, meaning it comes after this |
| 1091 | // value. So we scan past the value to find it, then come back here. |
| 1092 | // We currently don't do this for vectors of unions because the |
| 1093 | // scanning/serialization logic would get very complicated. |
| 1094 | auto type_name = field->name + UnionTypeFieldSuffix(); |
| 1095 | FLATBUFFERS_ASSERT(parent_struct_def); |
| 1096 | auto type_field = parent_struct_def->fields.Lookup(type_name); |
| 1097 | FLATBUFFERS_ASSERT(type_field); // Guaranteed by ParseField(). |
| 1098 | // Remember where we are in the source file, so we can come back here. |
| 1099 | auto backup = *static_cast<ParserState *>(this); |
| 1100 | ECHECK(SkipAnyJsonValue()); // The table. |
| 1101 | ECHECK(ParseComma()); |
| 1102 | auto next_name = attribute_; |
| 1103 | if (Is(kTokenStringConstant)) { |
| 1104 | NEXT(); |
| 1105 | } else { |
| 1106 | EXPECT(kTokenIdentifier); |
| 1107 | } |
| 1108 | if (next_name == type_name) { |
| 1109 | EXPECT(':'); |
| 1110 | ParseDepthGuard depth_guard(this); |
| 1111 | ECHECK(depth_guard.Check()); |
| 1112 | Value type_val = type_field->value; |
nothing calls this directly
no test coverage detected