20 #include <QJsonDocument>
21 #include <QJsonObject>
43 for (
auto model :
p_impl->models)
44 array.push_back(converter->to_json(*model));
46 QJsonDocument document(array);
47 QFile file(QString::fromStdString(file_name));
49 if (!file.open(QIODevice::WriteOnly))
50 throw std::runtime_error(
"Error in JsonDocument: can't save the file '" + file_name +
"'");
52 file.write(document.toJson());
61 QFile file(QString::fromStdString(file_name));
62 if (!file.open(QIODevice::ReadOnly))
63 throw std::runtime_error(
"Error in JsonDocument: can't read the file '" + file_name +
"'");
65 auto document = QJsonDocument::fromJson(file.readAll());
66 auto array = document.array();
67 if (array.size() !=
static_cast<int>(
p_impl->models.size())) {
68 std::ostringstream ostr;
69 ostr <<
"Error in JsonDocument: number of application models " <<
p_impl->models.size()
70 <<
" and number of json models " << array.size() <<
" doesn't match";
71 throw std::runtime_error(ostr.str());
76 for (
auto model :
p_impl->models) {
77 converter->from_json(array.at(index).toObject(), *model);
void save(const std::string &file_name) const override
Saves models on disk.
void load(const std::string &file_name) override
Loads models from disk. If models have some data already, it will be rewritten.
std::unique_ptr< JsonDocumentImpl > p_impl
JsonDocument(const std::vector< SessionModel * > &models)
materialitems.h Collection of materials to populate MaterialModel.
MVVM_MODEL_EXPORT std::unique_ptr< JsonModelConverterInterface > CreateModelProjectConverter()
Creates a JSON model converter intended for save/load of the project on disk.
JsonDocumentImpl(const std::vector< SessionModel * > &models)
std::vector< SessionModel * > models