jsonhandler.cpp 780 B

12345678910111213141516171819202122232425262728
  1. #include "../include/jsonhandler.h"
  2. #include "../include/cmdmanager.h"
  3. using namespace std;
  4. // This method gets a string and tries to read it as Json
  5. // If it fails to do so, return and do nothing, else handle the content
  6. void JsonHandler::parseJSON(string buffer) {
  7. Json::Value root;
  8. Json::CharReaderBuilder builder;
  9. Json::CharReader *reader = builder.newCharReader();
  10. string jsonError;
  11. // Try to parse the string as Json and store the result of the pasring in a
  12. // boolean
  13. bool parsingSuccessful = reader->parse(buffer.c_str(), buffer.c_str() + buffer.size(), &root, &jsonError);
  14. delete (reader);
  15. // If the string is not correct Json, return
  16. if (!parsingSuccessful) {
  17. return;
  18. }
  19. string cmd = root["command"].asString();
  20. CmdManager::executeCmd(cmd, root);
  21. }