| 1369 | } |
| 1370 | |
| 1371 | LuaTable LuaDetail::jsonContainerToTable(LuaEngine& engine, Json const& container) { |
| 1372 | if (!container.isType(Json::Type::Array) && !container.isType(Json::Type::Object)) |
| 1373 | throw LuaException("jsonContainerToTable called on improper json type"); |
| 1374 | |
| 1375 | auto table = engine.createTable(); |
| 1376 | auto nils = insertJsonMetatable(engine, table, container.type()); |
| 1377 | |
| 1378 | if (container.isType(Json::Type::Array)) { |
| 1379 | auto vlist = container.arrayPtr(); |
| 1380 | for (size_t i = 0; i < vlist->size(); ++i) { |
| 1381 | auto const& val = (*vlist)[i]; |
| 1382 | if (val) |
| 1383 | table.rawSet(i + 1, val); |
| 1384 | else |
| 1385 | nils.rawSet(i + 1, 0); |
| 1386 | } |
| 1387 | } else { |
| 1388 | for (auto const& pair : *container.objectPtr()) { |
| 1389 | if (pair.second) |
| 1390 | table.rawSet(pair.first, pair.second); |
| 1391 | else |
| 1392 | nils.rawSet(pair.first, 0); |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | return table; |
| 1397 | } |
| 1398 | |
| 1399 | Maybe<Json> LuaDetail::tableToJsonContainer(LuaTable const& table) { |
| 1400 | JsonObject stringEntries; |