| 27 | break;\ |
| 28 | } |
| 29 | static flatbuffers::Offset<void> _writeJsonToFlatbuffer(const flatbuffers::TypeTable * table, flatbuffers::FlatBufferBuilder& builder, const rapidjson::GenericObject<false, rapidjson::GenericValue<rapidjson::UTF8<>>>& object) { |
| 30 | std::vector<std::pair<int, flatbuffers::Offset<void>>> indexes; |
| 31 | // Load union type for easy to use |
| 32 | std::map<std::string, int> unionNames; |
| 33 | for (int i=0; i<table->num_elems; ++i) { |
| 34 | if (table->type_codes[i].sequence_ref == -1) { |
| 35 | continue; |
| 36 | } |
| 37 | const flatbuffers::TypeTable *ref = table->type_refs[table->type_codes[i].sequence_ref](); |
| 38 | if (ref->st == flatbuffers::ST_UNION) { |
| 39 | unionNames.insert(std::make_pair(std::string(table->names[i]) + "_type", i)); |
| 40 | } |
| 41 | } |
| 42 | // Find index and cache |
| 43 | std::map<int, int> unionTypes; |
| 44 | for (auto iter = object.begin(); iter !=object.end(); iter++) { |
| 45 | auto name = iter->name.GetString(); |
| 46 | int index = -1; |
| 47 | for (int i=0; i<table->num_elems; ++i) { |
| 48 | if (0 == ::strcmp(table->names[i], name)) { |
| 49 | index = i; |
| 50 | break; |
| 51 | } |
| 52 | } |
| 53 | auto uiter = unionNames.find(name); |
| 54 | if (uiter != unionNames.end()) { |
| 55 | // Find union type id |
| 56 | auto value = iter->value.GetString(); |
| 57 | int typePos = -1; |
| 58 | auto unionIndex = uiter->second; |
| 59 | auto ref = table->type_refs[table->type_codes[unionIndex].sequence_ref](); |
| 60 | for (int j=0; j<ref->num_elems; ++j) { |
| 61 | if (0 == ::strcmp(ref->names[j], value)) { |
| 62 | typePos = j; |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | if (-1 == typePos) { |
| 67 | MNN_ERROR("Can't find union type\n"); |
| 68 | continue; |
| 69 | } |
| 70 | if (typePos > 0) { |
| 71 | // First is None |
| 72 | unionTypes.insert(std::make_pair(unionIndex, typePos-1)); |
| 73 | } |
| 74 | } |
| 75 | if (index == -1) { |
| 76 | MNN_PRINT("Invalid: %s, Skip it\n", name); |
| 77 | } |
| 78 | indexes.emplace_back(std::make_pair(index, 0)); |
| 79 | } |
| 80 | |
| 81 | // resolve single object |
| 82 | int pos = 0; |
| 83 | for (auto iter = object.begin(); iter !=object.end(); iter++, pos++) { |
| 84 | int index = indexes[pos].first; |
| 85 | if (-1 == index) { |
| 86 | continue; |