jsonhandler.cpp 761 B

1234567891011121314151617181920212223242526
  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. // If the string is not correct Json, return
  15. if (!parsingSuccessful) {
  16. return;
  17. }
  18. string cmd = root["command"].asString();
  19. CmdManager::executeCmd(cmd, root);
  20. }