2019-02-15 23:04:04 +00:00
|
|
|
#include <util/json.hpp>
|
2018-12-12 02:52:51 +00:00
|
|
|
|
|
|
|
#include <cstring>
|
2018-10-27 12:41:04 +00:00
|
|
|
#include <string>
|
2018-12-12 02:52:51 +00:00
|
|
|
#include <vector>
|
2018-10-25 17:03:25 +00:00
|
|
|
|
2019-02-15 23:04:04 +00:00
|
|
|
namespace llarp
|
2018-10-25 17:03:25 +00:00
|
|
|
{
|
|
|
|
namespace json
|
|
|
|
{
|
2019-03-02 02:27:38 +00:00
|
|
|
struct NlohmannJSONParser : public IParser
|
2018-10-25 17:03:25 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
NlohmannJSONParser(size_t contentSize) : m_Buf(contentSize + 1), m_Offset(0)
|
2018-10-25 17:03:25 +00:00
|
|
|
{
|
2019-03-02 02:27:38 +00:00
|
|
|
assert(contentSize != 0);
|
2018-10-25 17:03:25 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
std::vector<char> m_Buf;
|
2018-10-25 17:03:25 +00:00
|
|
|
size_t m_Offset;
|
|
|
|
|
|
|
|
bool
|
2019-07-30 23:42:13 +00:00
|
|
|
FeedData(const char* buf, size_t sz) override
|
2018-10-25 17:03:25 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (sz == 0)
|
2019-12-15 16:59:26 +00:00
|
|
|
return true;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_Offset + sz > m_Buf.size() - 1)
|
2018-10-25 17:03:25 +00:00
|
|
|
return false;
|
2019-12-15 17:01:29 +00:00
|
|
|
std::copy_n(buf, sz, m_Buf.data() + m_Offset);
|
2018-10-25 17:03:25 +00:00
|
|
|
m_Offset += sz;
|
2018-10-26 13:02:15 +00:00
|
|
|
m_Buf[m_Offset] = 0;
|
2018-10-25 17:03:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Result
|
2019-07-30 23:42:13 +00:00
|
|
|
Parse(nlohmann::json& obj) const override
|
2018-10-25 17:03:25 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_Offset < m_Buf.size() - 1)
|
2018-10-25 17:03:25 +00:00
|
|
|
return eNeedData;
|
2019-03-02 02:27:38 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
obj = nlohmann::json::parse(m_Buf.data());
|
|
|
|
return eDone;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
catch (const nlohmann::json::exception&)
|
2019-03-02 02:27:38 +00:00
|
|
|
{
|
2018-10-25 17:03:25 +00:00
|
|
|
return eParseError;
|
2019-03-02 02:27:38 +00:00
|
|
|
}
|
2018-10-25 17:03:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
IParser*
|
|
|
|
MakeParser(size_t contentSize)
|
|
|
|
{
|
2019-03-02 02:27:38 +00:00
|
|
|
return new NlohmannJSONParser(contentSize);
|
2018-10-25 17:03:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace json
|
2019-02-15 23:04:04 +00:00
|
|
|
} // namespace llarp
|