#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/prettywriter.h"
#include
enum class data_type {
type_bool = 0,
type_char = 1,
type_uchar = 2,
type_short = 3,
type_ushort = 4,
type_int = 5,
type_uint = 6,
type_long = 7,
type_ulong = 8,
type_float = 9,
type_double = 10,
type_string = 11,
};
class translate_writer : public rapidjson::Writer < rapidjson::StringBuffer >
{
private:
rapidjson::StringBuffer sb;
bool add_type_int(const char* name, data_type type, int val) {
return this->add_array(name, [&](translate_writer* wr){
return wr->Int((int)type) && wr->Int(val);
});
}
bool add_type_uint(const char* name, data_type type, uint32_t val) {
return add_array(name, [&](translate_writer* wr){
return wr->Int((int)type) && wr->Uint(val);
});
}
bool add_type_double(const char* name, data_type type, double val) {
return add_array(name, [&](translate_writer* wr){
return wr->Int((int)type) && wr->Double(val);
});
}
public:
translate_writer() : rapidjson::Writer(sb) {
}
std::string GetString() {
return sb.GetString();
}
bool add_array(const char* name, std::function func) {
return this->Key(name) && this->StartArray() && func(this) && this->EndArray();
}
bool add_object(const char* name, std::function func) {
return this->Key(name) && this->StartObject() && func(this) && this->EndObject();
}
bool add_char(const char* name, char val) {
return add_type_int(name, data_type::type_char, val);
}
bool add_short(const char* name, short val) {
return add_type_int(name, data_type::type_char, val);
}
bool add_int(const char* name, int val) {
return add_type_int(name, data_type::type_int, val);
}
bool add_long(const char* name, long val) {
return add_array(name, [&](translate_writer* wr){
return wr->Int((int)data_type::type_long) && wr->Int64(val);
});
}
bool add_uint8(const char* name, uint8_t val) {
return add_type_int(name, data_type::type_uchar, val);
}
bool add_uint16(const char* name, uint16_t val) {
return add_type_int(name, data_type::type_uchar, val);
}
bool add_uint32(const char* name, uint32_t val) {
return add_type_int(name, data_type::type_uint, val);
}
bool add_uint64(const char* name, uint64_t val) {
return add_array(name, [&](translate_writer* wr) {
return wr->Int((int)data_type::type_ulong) && wr->Uint64(val);
});
}
bool add_float(const char* name, float val) {
return add_type_double(name, data_type::type_float, val);
}
bool add_double(const char* name, double val) {
return add_type_double(name, data_type::type_double, val);
}
bool add_string(const char* name, const std::string& val) {
return add_array(name, [&](translate_writer* wr){
return wr->Int((int)data_type::type_string) && wr->String(val.c_str());
});
}
bool add_bool(const char* name, bool val) {
return add_array(name, [&](translate_writer* wr){
return wr->Int((int)data_type::type_bool) && wr->Bool(val);
});
}
};
// 测试代码, 直接调用即可)--> 欢迎大家指出其中问题
void test_json_3() {
translate_writer writer;
writer.StartObject();
writer.add_object("msg_header", [](translate_writer* wr){
wr->add_uint32("len", 123);
wr->add_uint16("msg_type", 61499);
wr->add_uint32("user_id", 61493);
wr->add_uint32("result", 0);
wr->add_string("md5", "115546546as903aas3l2lakk234k");
return true;
});
writer.add_object("body", [](translate_writer* wr){
wr->add_uint16("room_id", 102);
wr->add_uint32("table_id", 4);
wr->add_float("ratio", 3.141592654);
return true;
});
writer.EndObject();
std::cout << writer.GetString() << std::endl;
}