Skip to content

Commit a50d0c4

Browse files
committed
add decorators
1 parent e2d713e commit a50d0c4

11 files changed

+233
-131
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#generated
22
json_deserialise*.h
3+
decorator.*.h
34
macros.*.h
45
type_deduction.*.h
56
basic_types.*.hpp

CMakeLists.txt

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,40 @@
11
cmake_minimum_required(VERSION 3.14)
22

3-
if (NOT CMAKE_CXX_STANDARD OR CMAKE_CXX_STANDARD LESS 17)
3+
if(NOT CMAKE_CXX_STANDARD OR CMAKE_CXX_STANDARD LESS 17)
44
set(CMAKE_CXX_STANDARD 17)
55
endif()
66
set(CMAKE_CXX_STANDARD_REQUIRED ON)
77

88
find_package(Boost)
99
if(NOT Boost_FOUND)
10-
message(WARNING "Can't find Boot Library, please add boost_preprocesser/include in the current dir ${CMAKE_CURRENT_LIST_DIR} to your include_path.")
10+
message(WARNING "Can't find Boot Library, please add boost_preprocessor/include in the current dir ${CMAKE_CURRENT_LIST_DIR} to your include_path.")
11+
add_subdirectory(boost_preprocessor)
1112
endif()
12-
include_directories(${CMAKE_CURRENT_LIST_DIR}/boost_preprocesser/include)
13+
include_directories(${CMAKE_CURRENT_LIST_DIR}/boost_preprocessor/include)
1314

1415
message("Currently only Qt supported.")
15-
option(JSON_DESERIALISE_DEFAULT_JSON_LIBRARY "" OFF)
16-
option(JSON_DESERIALISE_JSON_LIBRARIES "" OFF)
16+
option(JSON_DESERIALISE_DEFAULT_JSON_LIBRARY "Default Json Library" OFF)
17+
option(JSON_DESERIALISE_JSON_LIBRARIES "All libs that Need to be generated." OFF)
1718

18-
if (NOT JSON_DESERIALISE_JSON_LIBRARIES)
19-
if (NOT JSON_DESERIALISE_DEFAULT_JSON_LIBRARY)
20-
set(JSON_DESERIALISE_DEFAULT_JSON_LIBRARY "Qt")
19+
if(NOT JSON_DESERIALISE_JSON_LIBRARIES)
20+
if(NOT JSON_DESERIALISE_DEFAULT_JSON_LIBRARY)
21+
set(JSON_DESERIALISE_JSON_LIBRARIES "Qt")
22+
else(NOT JSON_DESERIALISE_JSON_LIBRARIES)
23+
set(JSON_DESERIALISE_JSON_LIBRARIES ${JSON_DESERIALISE_DEFAULT_JSON_LIBRARY})
2124
endif()
22-
set(JSON_DESERIALISE_JSON_LIBRARIES ${JSON_DESERIALISE_DEFAULT_JSON_LIBRARY})
23-
elseif (NOT JSON_DESERIALISE_DEFAULT_JSON_LIBRARY)
24-
list(GET JSON_DESERIALISE_JSON_LIBRARIES 0 JSON_DESERIALISE_DEFAULT_JSON_LIBRARY)
2525
endif()
2626

2727
foreach(LIB_ID ${JSON_DESERIALISE_JSON_LIBRARIES})
2828
string(TOLOWER ${LIB_ID} lib)
29-
if (${LIB_ID} STREQUAL ${JSON_DESERIALISE_DEFAULT_JSON_LIBRARY})
29+
if(${LIB_ID} STREQUAL ${JSON_DESERIALISE_DEFAULT_JSON_LIBRARY})
3030
set(JSON_DESERIALISER_DEFAULT_MODE ON)
3131
configure_file(${CMAKE_CURRENT_LIST_DIR}/json_deserialise.h.in ${CMAKE_CURRENT_LIST_DIR}/json_deserialise.h)
3232
endif()
33-
string(APPEND LIB_ID "Adaptation")
33+
string(APPEND LIB_ID "JsonLib")
3434
configure_file(${CMAKE_CURRENT_LIST_DIR}/type_deduction.h.in ${CMAKE_CURRENT_LIST_DIR}/type_deduction.${lib}.h)
3535
configure_file(${CMAKE_CURRENT_LIST_DIR}/basic_types.hpp.in ${CMAKE_CURRENT_LIST_DIR}/basic_types.${lib}.hpp)
3636
configure_file(${CMAKE_CURRENT_LIST_DIR}/macros.h.in ${CMAKE_CURRENT_LIST_DIR}/macros.${lib}.h)
37+
configure_file(${CMAKE_CURRENT_LIST_DIR}/decorator.h.in ${CMAKE_CURRENT_LIST_DIR}/decorator.${lib}.h)
3738
configure_file(${CMAKE_CURRENT_LIST_DIR}/json_deserialise.h.in ${CMAKE_CURRENT_LIST_DIR}/json_deserialise.${lib}.h)
3839
unset(JSON_DESERIALISER_DEFAULT_MODE)
3940
endforeach()

README.md

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ Serialise and Deserialise json from various types and data structures, performin
44
Currently only support Qt Json Library and will support nlohmann soon.
55

66
Headers only!
7-
So You just need to add subdirectory and setup include path in cmake and include the autogen file "json_deserialise.h".
7+
So You just need to add subdirectory and setup include path in cmake, then include autogen files "json_deserialise.*lib_name*.h", and "json_deserialise.h" for the default one(You should set this by setting *JSON_DESERIALISE_DEFAULT_JSON_LIBRARY* in cmake).
8+
Depending boost preprocessor library, please also add *boost_preprocessor/include* to your include path if your project does not config Boost.
89

910
## Basic Types
1011

@@ -55,9 +56,9 @@ Supose you have a json like below:
5556
QJsonObject json;
5657
TypeA a;
5758
TypeB b;
58-
declare_deserialiser("A", a, a_holder);
59-
declare_deserialiser("B", b, b_holder);
60-
declare_top_object_deserialiser(deserialiser, a_holder, b_holder);
59+
Field a("A", obj.a);
60+
Field b("B", obj.b);
61+
ObjectDeserialiser deserialiser(a, b);
6162
// From File
6263
deserialiser.deserialise_file(FILENAME);
6364
// From String
@@ -84,10 +85,10 @@ struct Simple {
8485
};
8586
8687
QJsonObject json;
87-
Sample obj;
88-
declare_deserialiser("A", obj.a, a_holder);
89-
declare_deserialiser("B", obj.b, b_holder);
90-
declare_top_object_deserialiser(deserialiser, a_holder, b_holder);
88+
Simple obj;
89+
Field a("A", obj.a);
90+
Field b("B", obj.b);
91+
ObjectDeserialiser deserialiser(a, b);
9192
deserialiser.deserialise(json); // And two other methods.
9293
std::cout << deserialiser.serialise(); // And two other methods.
9394
```
@@ -107,13 +108,12 @@ declare_object(Sample,
107108
// Somewhere
108109
Sample s;
109110
QJsonObject json;
110-
declare_top_deserialiser(s, deserialiser);
111-
deserialiser.assign(json); // json only
112-
json = deserialiser.to_json().toObject(); // json only
111+
Deserialiser holder(s);
112+
holder.from_json(json);
113+
holder.from_file(FILENAME);
114+
json = holder.to_json().toObject();
113115
//Also
114-
declare_top_object_deserialiser(s, serialiser);
115-
json = s.deserialise_to_json(); // And two other methods.
116-
s.deserialise_file(FILENAME); // And two other methods.
116+
json = Serialise(s).toObject();
117117
```
118118
119119
### 3. For Enum
@@ -168,6 +168,5 @@ enum class Type : int {
168168
} sample;
169169
declare_as_trivial(Type, int);
170170

171-
declare_deserialiser("Enum", sample, holder);
172-
qDebug() << holder.to_json();
171+
qDebug() << Serialise(sample);
173172
```

adaptor.qt.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@
1212
#include <cstring>
1313

1414
namespace JsonDeserialise {
15-
struct QtAdaptation {
15+
struct QtJsonLib {
1616

1717
// Essential alias
1818

1919
template <typename Any>
20-
using Deserialisable = QtAdaptation_::Deserialisable<Any>;
20+
using Deserialisable = QtJsonLibPrivate::Deserialisable<Any>;
2121

2222
template <typename Any>
2323
using DeserialisableType = typename Deserialisable<Any>::Type;
2424

2525
template <auto member_offset>
26-
using Customised = QtAdaptation_::Customised<member_offset>;
26+
using Customised = QtJsonLibPrivate::Customised<member_offset>;
2727

2828
// Basic Types
2929

basic_types.hpp.in

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
#include <set>
1111
#include <vector>
1212

13-
namespace JsonDeserialise::@LIB_ID@_ {
13+
namespace JsonDeserialise::@LIB_ID@Private {
1414

15-
using Impl = Deserialiser<@LIB_ID@>;
15+
using Impl = Implementation<@LIB_ID@>;
1616

1717
template <typename T>
1818
using StringConvertor = typename @LIB_ID@::template StringConvertor<T>;
@@ -233,6 +233,6 @@ namespace JsonDeserialise::@LIB_ID@_ {
233233
typename ConstexprIncArray<RegisteredStyleInfo<member_ptr>::argc>::Type>
234234
struct RegisteredStyle;
235235

236-
} // namespace JsonDeserialise::@LIB_ID@_
236+
} // namespace JsonDeserialise::@LIB_ID@Private
237237

238238
#endif // JSON_DESERIALISER_@LIB_ID@_TYPE_BASIC_

decorator.h.in

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#ifndef JSON_DESERIALISER_@LIB_ID@_DECORATOR
2+
#define JSON_DESERIALISER_@LIB_ID@_DECORATOR
3+
4+
#include "adaptor.@[email protected]"
5+
#include "json_deserialise.hpp"
6+
7+
#cmakedefine JSON_DESERIALISER_DEFAULT_MODE
8+
#ifdef JSON_DESERIALISER_DEFAULT_MODE
9+
inline
10+
#endif
11+
#undef JSON_DESERIALISER_DEFAULT_MODE
12+
namespace JsonDeserialiseDecoratorFor@LIB_ID@ {
13+
14+
using Impl = JsonDeserialise::Implementation<JsonDeserialise::@LIB_ID@>;
15+
16+
template <typename T>
17+
inline decltype(auto) Serialise(const T& source) {
18+
return Impl::DeserialisableType<T>(source).to_json();
19+
}
20+
21+
template <typename T>
22+
struct Deserialiser : public Impl::DeserialisableType<T> {
23+
Deserialiser(T& source) : Impl::DeserialisableType<T>(source) {}
24+
25+
template <typename String>
26+
inline void from_json_string(String&& str) {
27+
Impl::JsonDeserialiser(*this)->deserialise(std::forward<String>(str));
28+
}
29+
30+
template <typename String>
31+
inline void from_file(String&& filepath) {
32+
Impl::JsonDeserialiser(*this)->deserialise_file(std::forward<String>(filepath));
33+
}
34+
};
35+
36+
template <typename T>
37+
struct Serialiser {
38+
using Base = Impl::DeserialisableType<T>;
39+
const Base base;
40+
Serialiser(const T& source) : base(source) {}
41+
42+
inline decltype(auto) to_json() const {
43+
return base.to_json();
44+
}
45+
};
46+
47+
template <typename T, bool = std::is_base_of_v<T, Impl::DeserialisableBase>>
48+
struct Field : protected Impl::DeserialisableType<T> {
49+
using Base = Impl::DeserialisableType<T>;
50+
template <typename String>
51+
Field(String&& key, T& source, bool optional = false)
52+
: Base(std::forward<String>(key), source, optional) {}
53+
};
54+
55+
template <typename T>
56+
struct Field<T, true> : protected T {
57+
using Base = T;
58+
template <typename... Args>
59+
Field(Args&&... args) : Base(std::forward<Args>(args)...) {}
60+
};
61+
62+
template <typename U, typename V, typename... Args>
63+
struct ObjectDeserialiser
64+
: public Impl::JsonDeserialiser<typename Field<U>::Base, typename Field<V>::Base,
65+
typename Field<Args>::Base...> {
66+
using Base = typename Impl::JsonDeserialiser<typename Field<U>::Base, typename Field<V>::Base,
67+
typename Field<Args>::Base...>;
68+
69+
ObjectDeserialiser(Field<U>& arg1, Field<V>& arg2, Field<Args>&... args)
70+
: Base(arg1, arg2, args...) {}
71+
};
72+
73+
template <typename T, bool = std::is_base_of_v<T, Impl::DeserialisableBase>>
74+
struct ConstField : protected Impl::DeserialisableType<T> {
75+
using Base = Impl::DeserialisableType<T>;
76+
template <typename String>
77+
ConstField(String&& key, const T& source) : Base(std::forward<String>(key), source) {}
78+
};
79+
80+
template <typename T>
81+
struct ConstField<T, true> : protected T {
82+
using Base = T;
83+
template <typename... Args>
84+
ConstField(Args&&... args) : Base(std::forward<Args>(args)...) {}
85+
};
86+
87+
template <typename U, typename V, typename... Args>
88+
struct ObjectSerialiser {
89+
using Base =
90+
typename Impl::JsonDeserialiser<typename ConstField<U>::Base, typename ConstField<V>::Base,
91+
typename ConstField<Args>::Base...>;
92+
const Base base;
93+
94+
ObjectSerialiser(ConstField<U>& arg1, ConstField<V>& arg2, ConstField<Args>&... args)
95+
: Base(arg1, arg2, args...) {}
96+
97+
template <typename String>
98+
inline decltype(auto) to_file(String&& filepath, bool compress = false) const {
99+
base.serialise_to_file(std::forward<String>(filepath), compress);
100+
}
101+
102+
inline decltype(auto) to_string(bool compress = false) const {
103+
base.serialise(compress);
104+
}
105+
106+
inline decltype(auto) to_json() const {
107+
base.serialise_to_json();
108+
}
109+
};
110+
111+
} // namespace JsonDeserialiseDecoratorFor@LIB_ID@
112+
113+
#endif

json_deserialise.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
#include "macros.@[email protected]"
22
#include "types.@[email protected]"
3+
#include "decorator.@[email protected]"

0 commit comments

Comments
 (0)