mirror of
https://github.com/binary-kitchen/doorlockd
synced 2024-12-22 10:24:26 +01:00
Added Response::fromJSON
This commit is contained in:
parent
4c05636ff0
commit
aa79228e47
@ -1,6 +1,9 @@
|
||||
#include <exception>
|
||||
|
||||
#include <json/json.h>
|
||||
|
||||
#include "response.h"
|
||||
#include "util.h"
|
||||
|
||||
const std::string Response::_codeKey = "code";
|
||||
const std::string Response::_messageKey = "message";
|
||||
@ -20,3 +23,22 @@ std::string Response::toJson() const
|
||||
|
||||
return writer.write(response);
|
||||
}
|
||||
|
||||
Response Response::fromJson(const std::string &json)
|
||||
{
|
||||
Json::Reader reader;
|
||||
Json::Value root;
|
||||
Response retval;
|
||||
|
||||
if (reader.parse(json, root) == false)
|
||||
throw std::runtime_error("Error parsing JSON");
|
||||
|
||||
retval.message = getJsonOrFail<std::string>(root, _messageKey);
|
||||
|
||||
const auto code = getJsonOrFail<int>(root, _codeKey);
|
||||
if (code > Code::RESPONSE_NUM_ITEMS)
|
||||
throw std::runtime_error("Error code out of range");
|
||||
retval.code = static_cast<Code>(code);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
@ -18,11 +18,9 @@ struct Response
|
||||
UnknownCommand, // Unknown action
|
||||
LDAPInit, // Ldap initialization failed
|
||||
AccessDenied, // Access denied
|
||||
RESPONSE_NUM_ITEMS
|
||||
} code;
|
||||
|
||||
std::string message;
|
||||
std::string toJson() const;
|
||||
operator bool() const;
|
||||
|
||||
Response() :
|
||||
code(Fail),
|
||||
@ -36,6 +34,12 @@ struct Response
|
||||
{
|
||||
}
|
||||
|
||||
static Response fromJson(const std::string &json);
|
||||
std::string toJson() const;
|
||||
|
||||
// Returns true if code is success
|
||||
operator bool() const;
|
||||
|
||||
private:
|
||||
|
||||
const static std::string _codeKey;
|
||||
|
Loading…
Reference in New Issue
Block a user