2019-02-15 23:04:04 +00:00
|
|
|
#ifndef LLARP_UTIL_JSON_HPP
|
|
|
|
#define LLARP_UTIL_JSON_HPP
|
2018-10-24 18:02:42 +00:00
|
|
|
|
2018-10-25 17:03:25 +00:00
|
|
|
#include <rapidjson/document.h>
|
2019-02-14 13:42:38 +00:00
|
|
|
#include <rapidjson/writer.h>
|
2019-02-15 23:04:04 +00:00
|
|
|
|
|
|
|
#include <memory>
|
2019-02-08 22:44:21 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
2019-02-15 23:04:04 +00:00
|
|
|
namespace llarp
|
2018-10-25 17:03:25 +00:00
|
|
|
{
|
|
|
|
namespace json
|
|
|
|
{
|
2019-02-14 13:42:38 +00:00
|
|
|
/// add this because debian stable doesn't have it
|
|
|
|
template < typename StreamType >
|
|
|
|
class BasicOStreamWrapper
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef typename StreamType::char_type Ch;
|
|
|
|
BasicOStreamWrapper(StreamType& stream) : stream_(stream)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Put(Ch c)
|
|
|
|
{
|
|
|
|
stream_.put(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Flush()
|
|
|
|
{
|
|
|
|
stream_.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not implemented
|
|
|
|
char
|
|
|
|
Peek() const
|
|
|
|
{
|
|
|
|
RAPIDJSON_ASSERT(false);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
char
|
|
|
|
Take()
|
|
|
|
{
|
|
|
|
RAPIDJSON_ASSERT(false);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
size_t
|
|
|
|
Tell() const
|
|
|
|
{
|
|
|
|
RAPIDJSON_ASSERT(false);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
char*
|
|
|
|
PutBegin()
|
|
|
|
{
|
|
|
|
RAPIDJSON_ASSERT(false);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
size_t
|
|
|
|
PutEnd(char*)
|
|
|
|
{
|
|
|
|
RAPIDJSON_ASSERT(false);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
BasicOStreamWrapper(const BasicOStreamWrapper&);
|
|
|
|
BasicOStreamWrapper&
|
|
|
|
operator=(const BasicOStreamWrapper&);
|
|
|
|
|
|
|
|
StreamType& stream_;
|
|
|
|
};
|
|
|
|
|
2018-11-22 23:59:03 +00:00
|
|
|
using Document = rapidjson::Document;
|
|
|
|
using Value = rapidjson::Value;
|
2019-02-14 13:42:38 +00:00
|
|
|
using Stream = BasicOStreamWrapper< std::ostream >;
|
|
|
|
using Writer = rapidjson::Writer< Stream >;
|
2018-10-25 17:03:25 +00:00
|
|
|
} // namespace json
|
2019-02-08 19:43:25 +00:00
|
|
|
|
2018-11-01 12:47:14 +00:00
|
|
|
#if __cplusplus >= 201703L
|
2018-11-22 23:59:03 +00:00
|
|
|
using string_view = std::string_view;
|
2018-10-26 13:02:15 +00:00
|
|
|
#else
|
2018-11-22 23:59:03 +00:00
|
|
|
using string_view = std::string;
|
2018-10-26 13:02:15 +00:00
|
|
|
#endif
|
2018-10-25 17:03:25 +00:00
|
|
|
namespace json
|
|
|
|
{
|
|
|
|
struct IParser
|
|
|
|
{
|
|
|
|
virtual ~IParser(){};
|
|
|
|
|
|
|
|
/// result from feeding data to parser
|
|
|
|
enum Result
|
|
|
|
{
|
|
|
|
/// we need more data to finish parsing
|
|
|
|
eNeedData,
|
|
|
|
/// we have parsed the object fully
|
|
|
|
eDone,
|
|
|
|
/// we have a parsing syntax error
|
|
|
|
eParseError
|
|
|
|
};
|
|
|
|
|
|
|
|
/// feed data to parser return true if successful
|
|
|
|
virtual bool
|
2019-02-14 13:42:38 +00:00
|
|
|
FeedData(const char* buf, size_t sz) = 0;
|
2018-10-25 17:03:25 +00:00
|
|
|
/// parse internal buffer
|
|
|
|
virtual Result
|
2019-02-14 13:42:38 +00:00
|
|
|
Parse(Document& obj) const = 0;
|
2018-10-25 17:03:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// create new parser
|
2019-02-14 13:42:38 +00:00
|
|
|
IParser*
|
2018-10-25 17:03:25 +00:00
|
|
|
MakeParser(size_t contentSize);
|
|
|
|
|
|
|
|
void
|
2019-02-14 13:42:38 +00:00
|
|
|
ToString(const json::Document& obj, std::ostream& out);
|
2018-10-25 17:03:25 +00:00
|
|
|
|
|
|
|
} // namespace json
|
2019-02-15 23:04:04 +00:00
|
|
|
} // namespace llarp
|
2018-10-24 18:02:42 +00:00
|
|
|
|
|
|
|
#endif
|