lokinet/llarp/util/json.cpp
Thomas Winget 7caa87862e standardize include format and pragma once
All #ifndef guards on headers have been removed, I think,
in favor of #pragma once

Headers are now included as `#include "filename"` if the included file
resides in the same directory as the file including it, or any
subdirectory therein.  Otherwise they are included as
`#include <project/top/dir/relative/path/filename>`

The above does not include system/os headers.
2021-03-09 19:01:41 -05:00

60 lines
1.2 KiB
C++

#include "json.hpp"
#include <cstring>
#include <string>
#include <vector>
namespace llarp
{
namespace json
{
struct NlohmannJSONParser : public IParser
{
NlohmannJSONParser(size_t contentSize) : m_Buf(contentSize + 1), m_Offset(0)
{
assert(contentSize != 0);
}
std::vector<char> m_Buf;
size_t m_Offset;
bool
FeedData(const char* buf, size_t sz) override
{
if (sz == 0)
return true;
if (m_Offset + sz > m_Buf.size() - 1)
return false;
std::copy_n(buf, sz, m_Buf.data() + m_Offset);
m_Offset += sz;
m_Buf[m_Offset] = 0;
return true;
}
Result
Parse(nlohmann::json& obj) const override
{
if (m_Offset < m_Buf.size() - 1)
return eNeedData;
try
{
obj = nlohmann::json::parse(m_Buf.data());
return eDone;
}
catch (const nlohmann::json::exception&)
{
return eParseError;
}
}
};
IParser*
MakeParser(size_t contentSize)
{
return new NlohmannJSONParser(contentSize);
}
} // namespace json
} // namespace llarp