Skip to content

Commit

Permalink
Check errors on parsing json
Browse files Browse the repository at this point in the history
  • Loading branch information
iurienistor committed Feb 18, 2024
1 parent 08d017e commit 963c2c1
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/kit_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ void KitModel::loadModelData()
bool KitModel::open(const std::string &file)
{
auto kit = std::make_unique<KitState>();
if (kit->open(file)) {
GEONKICK_LOG_ERROR("can't open kit");
if (!kit->open(file)) {
GEONKICK_LOG_ERROR("can't open kit, the preset might be wrong or corrupted");
return false;
}

Expand Down
24 changes: 15 additions & 9 deletions src/kit_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ bool KitState::open(const std::string &fileName)
(std::istreambuf_iterator<char>()));

sfile.close();
fromJson(fileData);
return false;
return fromJson(fileData);
}

bool KitState::save(const std::string &fileName)
Expand Down Expand Up @@ -123,17 +122,18 @@ std::vector<std::shared_ptr<PercussionState>>& KitState::percussions()
return percussionsList;
}

void KitState::fromJson(const std::string &jsonData)
bool KitState::fromJson(const std::string &jsonData)
{
rapidjson::Document document;
document.Parse(jsonData.c_str());
if (!document.IsObject())
return;
fromJsonObject(document);
return false;
return fromJsonObject(document);
}

void KitState::fromJsonObject(const rapidjson::Value &obj)
bool KitState::fromJsonObject(const rapidjson::Value &obj)
{
bool isOk = true;
for (const auto &m: obj.GetObject()) {
if (m.name == "KitAppVersion" && m.value.IsInt())
kitAppVersion = m.value.GetInt();
Expand All @@ -144,19 +144,25 @@ void KitState::fromJsonObject(const rapidjson::Value &obj)
if (m.name == "url" && m.value.IsString())
setUrl(m.value.GetString());
if (m.name == "percussions" && m.value.IsArray())
parsePercussions(m.value);
isOk = parsePercussions(m.value);
}
return isOk;
}

void KitState::parsePercussions(const rapidjson::Value &percussionsArray)
bool KitState::parsePercussions(const rapidjson::Value &percussionsArray)
{
if (percussionsArray.Empty())
return false;

size_t i = 0;
for (const auto &per: percussionsArray.GetArray()) {
auto state = std::make_shared<PercussionState>();
state->setId(i++);
state->loadObject(per);
if (!state->loadObject(per))
return false;
addPercussion(state);
}
return true;
}

std::string KitState::toJson() const
Expand Down
6 changes: 3 additions & 3 deletions src/kit_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class KitState {
KitState();
bool open(const std::string &fileName);
bool save(const std::string &fileName);
void fromJson(const std::string &jsonData);
void fromJsonObject(const rapidjson::Value &obj);
bool fromJson(const std::string &jsonData);
bool fromJsonObject(const rapidjson::Value &obj);
void setName(const std::string &name);
std::string getName() const;
void setAuthor(const std::string &author);
Expand All @@ -48,7 +48,7 @@ class KitState {
std::vector<std::shared_ptr<PercussionState>>& percussions();

protected:
void parsePercussions(const rapidjson::Value &percussionsArray);
bool parsePercussions(const rapidjson::Value &percussionsArray);

private:
std::vector<std::shared_ptr<PercussionState>> percussionsList;
Expand Down
7 changes: 4 additions & 3 deletions src/percussion_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ bool PercussionState::loadData(const std::string &data)
return true;
}

void PercussionState::loadObject(const rapidjson::Value &obj)
bool PercussionState::loadObject(const rapidjson::Value &obj)
{
if (!obj.IsObject())
return;
return false;

bool kickParsed = false;
for (const auto &m: obj.GetObject()) {
Expand All @@ -110,7 +110,7 @@ void PercussionState::loadObject(const rapidjson::Value &obj)
}

if (!kickParsed)
return;
return false;

for (const auto &m: obj.GetObject()) {
for (decltype(layers.size()) i = 0; i < layers.size(); i++) {
Expand All @@ -127,6 +127,7 @@ void PercussionState::loadObject(const rapidjson::Value &obj)
}
}
}
return true;
}

size_t PercussionState::getId() const
Expand Down
2 changes: 1 addition & 1 deletion src/percussion_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class PercussionState
PercussionState();
bool loadFile(const std::string &file);
bool loadData(const std::string &data);
void loadObject(const rapidjson::Value &obj);
bool loadObject(const rapidjson::Value &obj);
size_t getId() const;
void setId(size_t id);
void setChannel(size_t channel);
Expand Down

0 comments on commit 963c2c1

Please sign in to comment.