Commit 9a674a19 authored by zhaoyunfei's avatar zhaoyunfei

修改为宏文件

parent 34929c5e
#pragma once #ifndef _JUEFX_AJSON_HPP_
#define _JUEFX_AJSON_HPP_
/* /*
__ _____ _____ _____ __ _____ _____ _____
__| | __| | | | JSON for ORM C++ struct __| | __| | | | JSON for ORM C++ struct
...@@ -1075,9 +1075,7 @@ namespace ajson ...@@ -1075,9 +1075,7 @@ namespace ajson
}; };
template<typename ty, class enable = void> template<typename ty, class enable = void>
struct json_impl_in; struct json_impl;
template<typename ty, class enable = void>
struct json_impl_out;
inline bool is_true(token const& tok) inline bool is_true(token const& tok)
{ {
...@@ -1093,7 +1091,7 @@ namespace ajson ...@@ -1093,7 +1091,7 @@ namespace ajson
} }
template<> template<>
struct json_impl_in < bool, void > struct json_impl < bool, void >
{ {
static inline void read(reader& rd, bool& val) static inline void read(reader& rd, bool& val)
{ {
...@@ -1127,11 +1125,7 @@ namespace ajson ...@@ -1127,11 +1125,7 @@ namespace ajson
} }
rd.next(); rd.next();
} }
};
template<>
struct json_impl_out < bool, void >
{
template<typename write_ty> template<typename write_ty>
static inline void write(write_ty& wt, bool const& val) static inline void write(write_ty& wt, bool const& val)
{ {
...@@ -1148,7 +1142,7 @@ namespace ajson ...@@ -1148,7 +1142,7 @@ namespace ajson
}; };
template<> template<>
struct json_impl_in < __uint128_t, void > struct json_impl < __uint128_t, void >
{ {
static inline void read(reader& rd, __uint128_t& val) static inline void read(reader& rd, __uint128_t& val)
{ {
...@@ -1184,11 +1178,7 @@ namespace ajson ...@@ -1184,11 +1178,7 @@ namespace ajson
rd.next(); rd.next();
} }
};
template<>
struct json_impl_out < __uint128_t, void >
{
template<typename write_ty> template<typename write_ty>
static inline void write(write_ty& wt, __uint128_t const& val) static inline void write(write_ty& wt, __uint128_t const& val)
{ {
...@@ -1220,7 +1210,7 @@ namespace ajson ...@@ -1220,7 +1210,7 @@ namespace ajson
}; };
template<typename ty> template<typename ty>
struct json_impl_in < ty, struct json_impl < ty,
typename std::enable_if <detail::is_signed_intergral_like<ty>::value>::type > typename std::enable_if <detail::is_signed_intergral_like<ty>::value>::type >
{ {
static inline void read(reader& rd, ty& val) static inline void read(reader& rd, ty& val)
...@@ -1267,11 +1257,6 @@ namespace ajson ...@@ -1267,11 +1257,6 @@ namespace ajson
} }
rd.next(); rd.next();
} }
};
template<typename ty>
struct json_impl_out < ty,
typename std::enable_if <detail::is_signed_intergral_like<ty>::value>::type >
{
template<typename write_ty> template<typename write_ty>
static inline void write(write_ty& wt, ty const& val) static inline void write(write_ty& wt, ty const& val)
{ {
...@@ -1313,7 +1298,7 @@ namespace ajson ...@@ -1313,7 +1298,7 @@ namespace ajson
}; };
template<typename ty> template<typename ty>
struct json_impl_in < ty, struct json_impl < ty,
typename std::enable_if <detail::is_unsigned_intergral_like<ty>::value>::type > typename std::enable_if <detail::is_unsigned_intergral_like<ty>::value>::type >
{ {
static inline void read(reader& rd, ty& val) static inline void read(reader& rd, ty& val)
...@@ -1364,11 +1349,6 @@ namespace ajson ...@@ -1364,11 +1349,6 @@ namespace ajson
} }
rd.next(); rd.next();
} }
};
template<typename ty>
struct json_impl_out < ty,
typename std::enable_if <detail::is_unsigned_intergral_like<ty>::value>::type >
{
template<typename write_ty> template<typename write_ty>
static inline void write(write_ty& wt, ty const& val) static inline void write(write_ty& wt, ty const& val)
{ {
...@@ -1400,36 +1380,31 @@ namespace ajson ...@@ -1400,36 +1380,31 @@ namespace ajson
}; };
template<typename ty> template<typename ty>
struct json_impl_in < ty, struct json_impl < ty,
typename std::enable_if <std::is_enum<ty>::value>::type > typename std::enable_if <std::is_enum<ty>::value>::type >
{ {
static inline void read(reader& rd, ty& val) static inline void read(reader& rd, ty& val)
{ {
typedef typename std::underlying_type<ty>::type raw_type; typedef typename std::underlying_type<ty>::type raw_type;
json_impl_in<raw_type>::read(rd, (raw_type&)val); json_impl<raw_type>::read(rd, (raw_type&)val);
} }
};
template<typename ty>
struct json_impl_out < ty,
typename std::enable_if <std::is_enum<ty>::value>::type >
{
template<typename write_ty> template<typename write_ty>
static inline void write(write_ty& wt, ty const& val) static inline void write(write_ty& wt, ty const& val)
{ {
typedef typename std::underlying_type<ty>::type raw_type; typedef typename std::underlying_type<ty>::type raw_type;
json_impl_out<raw_type>::write(wt, val); json_impl<raw_type>::write(wt, val);
} }
template<typename write_ty> template<typename write_ty>
static inline void write_key(write_ty& wt, ty const& val) static inline void write_key(write_ty& wt, ty const& val)
{ {
typedef typename std::underlying_type<ty>::type raw_type; typedef typename std::underlying_type<ty>::type raw_type;
json_impl_out<raw_type>::write_key(wt, val); json_impl<raw_type>::write_key(wt, val);
} }
}; };
template<typename ty> template<typename ty>
struct json_impl_in < ty, struct json_impl < ty,
typename std::enable_if <std::is_floating_point<ty>::value>::type > typename std::enable_if <std::is_floating_point<ty>::value>::type >
{ {
static inline void read(reader& rd, ty& val) static inline void read(reader& rd, ty& val)
...@@ -1469,11 +1444,6 @@ namespace ajson ...@@ -1469,11 +1444,6 @@ namespace ajson
} }
rd.next(); rd.next();
} }
};
template<typename ty>
struct json_impl_out < ty,
typename std::enable_if <std::is_floating_point<ty>::value>::type >
{
template<typename write_ty> template<typename write_ty>
static inline void write(write_ty& wt, ty const& val) static inline void write(write_ty& wt, ty const& val)
{ {
...@@ -1656,7 +1626,7 @@ namespace ajson ...@@ -1656,7 +1626,7 @@ namespace ajson
} }
template<typename ty> template<typename ty>
struct json_impl_in < ty, struct json_impl < ty,
typename std::enable_if <detail::is_stdstring<ty>::value>::type > typename std::enable_if <detail::is_stdstring<ty>::value>::type >
{ {
static inline void read(reader& rd, ty& val) static inline void read(reader& rd, ty& val)
...@@ -1675,11 +1645,6 @@ namespace ajson ...@@ -1675,11 +1645,6 @@ namespace ajson
} }
rd.next(); rd.next();
} }
};
template<typename ty>
struct json_impl_out < ty,
typename std::enable_if <detail::is_stdstring<ty>::value>::type >
{
template<typename write_ty> template<typename write_ty>
static inline void write(write_ty& wt, ty const& val) static inline void write(write_ty& wt, ty const& val)
{ {
...@@ -1724,16 +1689,13 @@ namespace ajson ...@@ -1724,16 +1689,13 @@ namespace ajson
} }
template<size_t N> template<size_t N>
struct json_impl_in <char[N]> struct json_impl <char[N]>
{ {
static inline void read(reader& rd, char * val) static inline void read(reader& rd, char * val)
{ {
char_array_read(rd, val, N); char_array_read(rd, val, N);
} }
};
template<size_t N>
struct json_impl_out <char[N]>
{
template<typename write_ty> template<typename write_ty>
static inline void write(write_ty& wt, const char * val) static inline void write(write_ty& wt, const char * val)
{ {
...@@ -1742,17 +1704,13 @@ namespace ajson ...@@ -1742,17 +1704,13 @@ namespace ajson
}; };
template<size_t N> template<size_t N>
struct json_impl_in <const char[N] > struct json_impl <const char[N] >
{ {
static inline void read(reader& rd, char * val) static inline void read(reader& rd, char * val)
{ {
char_array_read(rd, val, N); char_array_read(rd, val, N);
} }
};
template<size_t N>
struct json_impl_out <const char[N] >
{
template<typename write_ty> template<typename write_ty>
static inline void write(write_ty& wt, const char * val) static inline void write(write_ty& wt, const char * val)
{ {
...@@ -1774,7 +1732,7 @@ namespace ajson ...@@ -1774,7 +1732,7 @@ namespace ajson
{ {
if (count < N) if (count < N)
{ {
json_impl_in<T>::read(rd, val[count]); json_impl<T>::read(rd, val[count]);
} }
else else
{ {
...@@ -1808,7 +1766,7 @@ namespace ajson ...@@ -1808,7 +1766,7 @@ namespace ajson
int last = N - 1; int last = N - 1;
for (int i = 0; i < N; ++i) for (int i = 0; i < N; ++i)
{ {
json_impl_out<T>::write(wt, val[i]); json_impl<T>::write(wt, val[i]);
if (i < last) if (i < last)
wt.put(','); wt.put(',');
} }
...@@ -1816,16 +1774,12 @@ namespace ajson ...@@ -1816,16 +1774,12 @@ namespace ajson
} }
template<typename T, size_t N> template<typename T, size_t N>
struct json_impl_in <T[N]> struct json_impl <T[N]>
{ {
static inline void read(reader& rd, T * val) static inline void read(reader& rd, T * val)
{ {
array_read(rd, val, N); array_read(rd, val, N);
} }
};
template<typename T, size_t N>
struct json_impl_out <T[N]>
{
template<typename write_ty> template<typename write_ty>
static inline void write(write_ty& wt, const T * val) static inline void write(write_ty& wt, const T * val)
{ {
...@@ -1834,16 +1788,12 @@ namespace ajson ...@@ -1834,16 +1788,12 @@ namespace ajson
}; };
template<typename T, size_t N> template<typename T, size_t N>
struct json_impl_in < const T[N]> struct json_impl < const T[N]>
{ {
static inline void read(reader& rd, T * val) static inline void read(reader& rd, T * val)
{ {
array_read(rd, val, N); array_read(rd, val, N);
} }
};
template<typename T, size_t N>
struct json_impl_out < const T[N]>
{
template<typename write_ty> template<typename write_ty>
static inline void write(write_ty& wt, const T * val) static inline void write(write_ty& wt, const T * val)
{ {
...@@ -1866,7 +1816,7 @@ namespace ajson ...@@ -1866,7 +1816,7 @@ namespace ajson
} }
template<typename ty> template<typename ty>
struct json_impl_in < ty, struct json_impl < ty,
typename std::enable_if <detail::is_sequence_container<ty>::value>::type > typename std::enable_if <detail::is_sequence_container<ty>::value>::type >
{ {
static inline void read(reader& rd, ty& val) static inline void read(reader& rd, ty& val)
...@@ -1880,7 +1830,7 @@ namespace ajson ...@@ -1880,7 +1830,7 @@ namespace ajson
while (tok->str.str[0] != ']') while (tok->str.str[0] != ']')
{ {
::ajson::emplace_back(val); ::ajson::emplace_back(val);
json_impl_in<typename ty::value_type>::read(rd, val.back()); json_impl<typename ty::value_type>::read(rd, val.back());
tok = &rd.peek(); tok = &rd.peek();
if (tok->str.str[0] == ',') if (tok->str.str[0] == ',')
{ {
...@@ -1900,11 +1850,6 @@ namespace ajson ...@@ -1900,11 +1850,6 @@ namespace ajson
rd.next(); rd.next();
return; return;
} }
};
template<typename ty>
struct json_impl_out < ty,
typename std::enable_if <detail::is_sequence_container<ty>::value>::type >
{
template<typename write_ty> template<typename write_ty>
static inline void write(write_ty& wt, ty const& val) static inline void write(write_ty& wt, ty const& val)
{ {
...@@ -1912,7 +1857,7 @@ namespace ajson ...@@ -1912,7 +1857,7 @@ namespace ajson
auto sz = val.size(); auto sz = val.size();
for (auto& i : val) for (auto& i : val)
{ {
json_impl_out<typename ty::value_type>::write(wt, i); json_impl<typename ty::value_type>::write(wt, i);
if (sz-- > 1) if (sz-- > 1)
wt.put(','); wt.put(',');
} }
...@@ -1921,7 +1866,7 @@ namespace ajson ...@@ -1921,7 +1866,7 @@ namespace ajson
}; };
template<typename ty> template<typename ty>
struct json_impl_in < ty, struct json_impl < ty,
typename std::enable_if <detail::is_associat_container<ty>::value>::type > typename std::enable_if <detail::is_associat_container<ty>::value>::type >
{ {
static inline void read(reader& rd, ty& val) static inline void read(reader& rd, ty& val)
...@@ -1936,13 +1881,13 @@ namespace ajson ...@@ -1936,13 +1881,13 @@ namespace ajson
{ {
typename ty::key_type key; typename ty::key_type key;
typename ty::mapped_type value; typename ty::mapped_type value;
json_impl_in<typename ty::key_type>::read(rd, key); json_impl<typename ty::key_type>::read(rd, key);
if (rd.expect(':') == false) if (rd.expect(':') == false)
{ {
rd.error("invalid object!"); rd.error("invalid object!");
} }
rd.next(); rd.next();
json_impl_in<typename ty::mapped_type>::read(rd, value); json_impl<typename ty::mapped_type>::read(rd, value);
val[key] = value; val[key] = value;
tok = &rd.peek(); tok = &rd.peek();
if (tok->str.str[0] == ',') if (tok->str.str[0] == ',')
...@@ -1963,11 +1908,6 @@ namespace ajson ...@@ -1963,11 +1908,6 @@ namespace ajson
rd.next(); rd.next();
return; return;
} }
};
template<typename ty>
struct json_impl_out < ty,
typename std::enable_if <detail::is_associat_container<ty>::value>::type >
{
template<typename write_ty> template<typename write_ty>
static inline void write(write_ty& wt, ty const& val) static inline void write(write_ty& wt, ty const& val)
{ {
...@@ -1975,9 +1915,9 @@ namespace ajson ...@@ -1975,9 +1915,9 @@ namespace ajson
auto sz = val.size(); auto sz = val.size();
for (auto& i : val) for (auto& i : val)
{ {
json_impl_out<typename ty::key_type>::write_key(wt, i.first); json_impl<typename ty::key_type>::write_key(wt, i.first);
wt.put(':'); wt.put(':');
json_impl_out<typename ty::mapped_type>::write(wt, i.second); json_impl<typename ty::mapped_type>::write(wt, i.second);
if (sz-- > 1) if (sz-- > 1)
wt.put(','); wt.put(',');
} }
...@@ -2104,7 +2044,7 @@ namespace ajson ...@@ -2104,7 +2044,7 @@ namespace ajson
{ {
if (member_ptr[pos] == member) if (member_ptr[pos] == member)
{ {
json_impl_in<head>::read(rd, val); json_impl<head>::read(rd, val);
return 1; return 1;
} }
if (sizeof...(args)) if (sizeof...(args))
...@@ -2132,7 +2072,7 @@ namespace ajson ...@@ -2132,7 +2072,7 @@ namespace ajson
{ {
typedef typename std::remove_cv<ty>::type rty; typedef typename std::remove_cv<ty>::type rty;
reader rd(buff, len); reader rd(buff, len);
json_impl_in<rty>::read(rd, val); json_impl<rty>::read(rd, val);
} }
template<typename ty> template<typename ty>
...@@ -2184,7 +2124,7 @@ namespace ajson ...@@ -2184,7 +2124,7 @@ namespace ajson
} }
reader rd(buffer, sz); reader rd(buffer, sz);
typedef typename std::remove_cv<ty>::type rty; typedef typename std::remove_cv<ty>::type rty;
json_impl_in<rty>::read(rd, val); json_impl<rty>::read(rd, val);
} }
template<typename write_ty, typename head, typename... args> template<typename write_ty, typename head, typename... args>
...@@ -2202,7 +2142,7 @@ namespace ajson ...@@ -2202,7 +2142,7 @@ namespace ajson
{ {
wt.write_str(member_ptr[pos].str, member_ptr[pos].len); wt.write_str(member_ptr[pos].str, member_ptr[pos].len);
wt.put(':'); wt.put(':');
json_impl_out<head>::write(wt, val); json_impl<head>::write(wt, val);
if (sizeof...(args)) if (sizeof...(args))
{ {
wt.put(','); wt.put(',');
...@@ -2230,7 +2170,7 @@ namespace ajson ...@@ -2230,7 +2170,7 @@ namespace ajson
{ {
typedef typename std::remove_cv<ty>::type rty; typedef typename std::remove_cv<ty>::type rty;
write_tp wt(ss); write_tp wt(ss);
json_impl_out<rty>::write(wt, val); json_impl<rty>::write(wt, val);
} }
template<typename ty, typename stream_ty = ajson_file_stream, class write_tp = lite_write<stream_ty> > template<typename ty, typename stream_ty = ajson_file_stream, class write_tp = lite_write<stream_ty> >
...@@ -2242,9 +2182,6 @@ namespace ajson ...@@ -2242,9 +2182,6 @@ namespace ajson
} }
#define AJSON(TYPE,...) \ #define AJSON(TYPE,...) \
AJSON_IN(TYPE, __VA_ARGS__) \
AJSON_OUT(TYPE, __VA_ARGS__)
/*
namespace ajson\ namespace ajson\
{\ {\
template<>\ template<>\
...@@ -2309,87 +2246,5 @@ namespace ajson\ ...@@ -2309,87 +2246,5 @@ namespace ajson\
}\ }\
};\ };\
} }
*/
#define AJSON_IN(TYPE,...) \ #endif
namespace ajson\ \ No newline at end of file
{\
template<>\
struct json_impl_in < TYPE, void >\
{\
struct json_helper_in : public TYPE\
{\
inline void read_(reader& rd)\
{\
auto& fields = this_field_list();\
if (rd.expect('{') == false){ rd.error("read object must start with {!"); }\
rd.next();\
if (rd.expect('}'))\
return;\
auto mber = rd.peek();\
do\
{\
if (mber.type != token::t_string){ rd.error("object key must be string"); }\
rd.next();\
if (rd.expect(':') == false){ rd.error("invalid json document!"); }\
rd.next();\
if (read_members(rd, &fields[0], mber.str, 0,__VA_ARGS__) == 0)\
{\
skip(rd);\
}\
if (rd.expect('}'))\
{\
rd.next();\
return;\
}\
else if (rd.expect(','))\
{\
rd.next();\
mber = rd.peek();\
continue;\
}\
rd.error("invalid json document!");\
} while (true);\
}\
};\
static inline detail::field_list& this_field_list()\
{\
static auto fields = detail::split_fields(STRINGFY_LIST(__VA_ARGS__));\
return fields;\
}\
static inline void read(reader& rd, TYPE& v)\
{\
reinterpret_cast<json_helper_in &>(v).read_(rd);\
}\
};\
}
#define AJSON_OUT(TYPE,...) \
namespace ajson\
{\
template<>\
struct json_impl_out < TYPE , void >\
{\
struct json_helper : public TYPE\
{\
template<typename write_ty>\
inline void write_(write_ty& wt) const\
{\
auto& fields = this_field_list();\
wt.put('{');\
::ajson::write_members(wt, &fields[0], 0,__VA_ARGS__);\
wt.put('}');\
}\
};\
static inline detail::field_list& this_field_list()\
{\
static auto fields = detail::split_fields(STRINGFY_LIST(__VA_ARGS__));\
return fields;\
}\
template<typename write_ty>\
static inline void write(write_ty& wt, TYPE const& v)\
{\
reinterpret_cast<json_helper const &>(v).write_(wt);\
}\
};\
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment