По какой-то причине цикл for
в методе fromJson
не может перебрать доступные поля JSON. Насколько я понимаю (основываясь на отладке), он не может прочитать входную строку как объект JSON. Но когда я проверяю это в отладчике, я вижу правильную строку JSON.
Точно такой же код работает для другого класса POD.
Объект:
struct Config
{
Config();
Config(Config&&) = default;
Config& operator=(Config&&) = default;
bool operator==(const Config& c) const;
bool operator!=(const Config& c) const;
bool isValid() const;
unsigned short m_taskTimer;
bool m_test;
};
Сериализация / десериализации. fromJson
не удалось разобрать входную строку:
bool Serialization::fromJson(Controller::Config& c, const std::string& str)
try
{
json::value temp;
unsigned short count = 0;
temp.parse(str);
for (auto it = temp.as_object().cbegin(); it != temp.as_object().cend(); ++it)
{
const std::string& key = it->first;
const json::value& value = it->second;
if (key == "taskTimer")
{
c.m_taskTimer = value.as_integer();
++count;
continue;
}
if (key == "test")
{
c.m_test = value.as_bool();
++count;
continue;
}
return false;
}
if (count != 2)
{
return false;
}
return true;
}
catch (...)
{
return false;
}
std::string Serialization::toJson(const Controller::Config& c)
{
json::value temp;
temp["taskTimer"] = json::value::number(c.m_taskTimer);
temp["test"] = json::value::boolean(c.m_test);
return temp.serialize();
}
Тест:
Controller::Config cc1, cc2;
cc1.m_taskTimer = 300;
cc1.m_test = true;
Serialization::fromJson(cc2, Serialization::toJson(cc1));
assert(cc1 == cc2);
Что я пытаюсь понять, так это почему он не может перебирать поля? Строка temp.parse(str)
работает, поэтому библиотека считает, что ввод действителен.