2019-02-15 23:04:04 +00:00
|
|
|
#include <util/json.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/string_view.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
|
|
|
{
|
2019-03-02 02:27:38 +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
|
|
|
}
|
|
|
|
|
|
|
|
std::vector< char > m_Buf;
|
|
|
|
size_t m_Offset;
|
|
|
|
|
|
|
|
bool
|
|
|
|
FeedData(const char* buf, size_t sz)
|
|
|
|
{
|
2018-10-26 13:03:10 +00:00
|
|
|
if(m_Offset + sz > m_Buf.size() - 1)
|
2018-10-25 17:03:25 +00:00
|
|
|
return false;
|
2019-03-02 02:27:38 +00:00
|
|
|
std::copy(buf, buf + sz, m_Buf.begin());
|
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-03-02 02:27:38 +00:00
|
|
|
Parse(nlohmann::json& obj) const
|
2018-10-25 17:03:25 +00:00
|
|
|
{
|
2018-10-26 13:02:15 +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;
|
|
|
|
}
|
|
|
|
catch(const nlohmann::json::exception&)
|
|
|
|
{
|
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
|