mirror of
https://github.com/binary-kitchen/doorlockd
synced 2024-12-22 18:34:25 +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 <json/json.h>
|
||||||
|
|
||||||
#include "response.h"
|
#include "response.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
const std::string Response::_codeKey = "code";
|
const std::string Response::_codeKey = "code";
|
||||||
const std::string Response::_messageKey = "message";
|
const std::string Response::_messageKey = "message";
|
||||||
@ -20,3 +23,22 @@ std::string Response::toJson() const
|
|||||||
|
|
||||||
return writer.write(response);
|
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
|
UnknownCommand, // Unknown action
|
||||||
LDAPInit, // Ldap initialization failed
|
LDAPInit, // Ldap initialization failed
|
||||||
AccessDenied, // Access denied
|
AccessDenied, // Access denied
|
||||||
|
RESPONSE_NUM_ITEMS
|
||||||
} code;
|
} code;
|
||||||
|
|
||||||
std::string message;
|
std::string message;
|
||||||
std::string toJson() const;
|
|
||||||
operator bool() const;
|
|
||||||
|
|
||||||
Response() :
|
Response() :
|
||||||
code(Fail),
|
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:
|
private:
|
||||||
|
|
||||||
const static std::string _codeKey;
|
const static std::string _codeKey;
|
||||||
|
Loading…
Reference in New Issue
Block a user