Classdesc 3.44
json_pack_epilogue.h
1/*
2 @copyright Russell Standish 2000-2013
3 @author Russell Standish
4 This file is part of Classdesc
5
6 Open source licensed under the MIT license. See LICENSE for details.
7*/
8
9#ifndef CLASSDESC_JSON_PACK_EPILOGUE_H
10#define CLASSDESC_JSON_PACK_EPILOGUE_H
11#include "classdesc.h"
12#include <sstream>
13#include "json_pack-allCDs.h"
14
15namespace classdesc
16{
17 template <>
19 {
20 static string name() {return "classdesc::json_pack_t";}
21 };
22
23 template <class T>
25 public Not< Or< Or< Or< Or<is_fundamental<T>,is_string<T> >, is_sequence<T> >,
26 And<is_associative_container<T>,Not<is_stringKeyMap<T> > > >, is_pointer<T> > >
27 {};
28
29 template <class T> typename
31 json_packp(json_pack_t& o, const string& d, T& a)
32 {
33 o.objectify();
34 if (tail(d)!="")
35 {
36 //create the object, if it doesn't already exist
37 try
38 {
39 json5_parser::mValue& parent=json_find(o,head(d));
40 if (parent.type()!=json5_parser::obj_type)
41 throw json_pack_error("trying to create object %s in non-object",
42 d.c_str());
43 json5_parser::mObject::iterator member=parent.get_obj().find(tail(d));
44 if (member==parent.get_obj().end())
45 parent.get_obj().insert(make_pair(tail(d), json5_parser::mObject()));
46 }
47 catch (json_pack_error&)
48 {
49 // only throw if this flag is set
50 if (o.throw_on_error) throw;
51 }
52 }
53 classdesc_access::access_json_pack<T>()(o,d,a);
54 }
55
56 template <class T> typename
58 json_unpackp(json_pack_t& o, const string& d, T& a, dummy<3> dum=0)
59 {classdesc_access::access_json_unpack<T>()(o,d,a);}
60
61 template <class T> void json_pack(json_pack_t& o, const string& d, T& a)
62 {json_packp(o,d,a);}
63
64 template <class T> void json_unpack(json_unpack_t& o, const string& d, T& a)
65 {json_unpackp(o,d,a);}
66
67 template <class T> void json_packp(json_pack_t& o, const string& d, T* a)
68 {}
69
70 template <class T> void json_packp(json_pack_t& o, const string& d, const char* a)
71 {o=json_pack_t(a);}
72}
73
74namespace classdesc_access
75{
76 namespace cd=classdesc;
77
78#if defined(__cplusplus) && __cplusplus>=201103L
79 // nobble iterators
80 template <class T>
81 struct access_json_pack<T, cd::void_t<typename std::iterator_traits<T>::value_type>>:
82 public cd::NullDescriptor<cd::json_pack_t> {};
83 template <class T>
84 struct access_json_unpack<T, cd::void_t<typename std::iterator_traits<T>::value_type>>:
85 public cd::NullDescriptor<cd::json_unpack_t> {};
86#endif
87
88#ifndef JSON_PACK_NO_FALL_THROUGH_TO_STREAMING
89 // fall through to streaming operators
90 template <class T,class Enable>
92 {
93 public:
94 void operator()(cd::json_pack_t& b, const cd::string& d, T& a)
95 {
96 std::ostringstream o;
97 o<<a;
98 b<<o.str();
99 }
100 };
101
102 template <class T,class Enable>
104 {
105 public:
106 void operator()(cd::json_unpack_t& b, const cd::string& d,
107 T& a)
108 {
109 std::string s;
110 b>>s;
111 std::istringstream i(s);
112 i>>a;
113 }
114 };
115#endif
116
117#if defined(__cplusplus) && __cplusplus>=201103L
118 template <class T>
119 struct access_json_pack<std::function<T>>
120 {
121 public:
122 void operator()(cd::json_unpack_t& b, const cd::string& d,
123 std::function<T>& a)
124 {b<<cd::typeName<T>();}
125 };
126 template <class T>
127 struct access_json_unpack<std::function<T>>
128 {
129 public:
130 void operator()(cd::json_unpack_t& b, const cd::string& d,
131 std::function<T>& a)
132 {}
133 };
134#endif
135
136 template <> struct access_json_pack<cd::json_pack_t>
137 {
138 template <class U>
139 void operator()(cd::json_pack_t& b, const cd::string& d, U& a)
140 {b=a;}
141 };
142 template <> struct access_json_unpack<cd::json_pack_t>
143 {
144 void operator()(cd::json_pack_t& b, const cd::string& d, cd::json_pack_t& a){a=b;}
145 void operator()(cd::json_pack_t& b, const cd::string& d, const cd::json_pack_t& a){}
146 };
147
148 template <> struct access_json_pack<json5_parser::mValue>
149 {
150 template <class U>
151 void operator()(cd::json_pack_t& b, const cd::string& d, U& a)
152 {b=cd::json_pack_t(a);}
153 };
154 template <> struct access_json_unpack<json5_parser::mValue>
155 {
156 void operator()(cd::json_pack_t& b, const cd::string& d, json5_parser::mValue& a){a=b;}
157 void operator()(cd::json_pack_t& b, const cd::string& d, const json5_parser::mValue& a){}
158 };
159
160
161
162 // support for polymorphic types, if loaded
163//#ifdef NEW_POLY_H
164// template <class T> struct access_json_pack<cd::PolyBase<T> >:
165// public cd::NullDescriptor<cd::json_pack_t> {};
166// template <class T> struct access_json_unpack<cd::PolyBase<T> >:
167// public cd::NullDescriptor<cd::json_unpack_t> {};
168// template <class T, class B> struct access_json_pack<cd::Poly<T,B> >
169// {
170// template <class U>
171// void operator()(cd::json_pack_t& t, const cd::string& d, U& a)
172// {
173// json_pack(t,d,cd::base_cast<B>::cast(a));
174// }
175// };
176// template <class T, class B> struct access_json_unpack<cd::Poly<T,B> >
177// {
178// template <class U>
179// void operator()(cd::json_pack_t& t, const cd::string& d, U& a)
180// {
181// json_unpack(t,d,cd::base_cast<B>::cast(a));
182// }
183// };
184//#endif
185
186#ifdef CLASSDESC_POLYPACKBASE_H
187 template <> struct access_json_pack<cd::PolyPackBase>:
188 public cd::NullDescriptor<cd::json_pack_t> {};
189 template <> struct access_json_unpack<cd::PolyPackBase>:
190 public cd::NullDescriptor<cd::json_unpack_t> {};
191 template <class T> struct access_json_pack<cd::PolyPack<T> >:
192 public cd::NullDescriptor<cd::json_pack_t> {};
193 template <class T> struct access_json_unpack<cd::PolyPack<T> >:
194 public cd::NullDescriptor<cd::json_unpack_t> {};
195#endif
196
197#ifdef CLASSDESC_POLYJSONBASE_H
198 template <> struct access_json_pack<cd::PolyJsonBase>:
199 public cd::NullDescriptor<cd::json_pack_t> {};
200 template <> struct access_json_unpack<cd::PolyJsonBase>:
201 public cd::NullDescriptor<cd::json_unpack_t> {};
202 template <class T> struct access_json_pack<cd::PolyJson<T> >:
203 public cd::NullDescriptor<cd::json_pack_t> {};
204 template <class T> struct access_json_unpack<cd::PolyJson<T> >:
205 public cd::NullDescriptor<cd::json_unpack_t> {};
206#endif
207
208#ifdef CLASSDESC_POLYXMLBASE_H
209 template <> struct access_json_pack<cd::PolyXMLBase>:
210 public cd::NullDescriptor<cd::json_pack_t> {};
211 template <> struct access_json_unpack<cd::PolyXMLBase>:
212 public cd::NullDescriptor<cd::json_unpack_t> {};
213 template <class T> struct access_json_pack<cd::PolyXML<T> >:
214 public cd::NullDescriptor<cd::json_pack_t> {};
215 template <class T> struct access_json_unpack<cd::PolyXML<T> >:
216 public cd::NullDescriptor<cd::json_unpack_t> {};
217#endif
218
219template <> struct access_json_pack< enum ::classdesc::RESTProcessType::Type > {
220template <class _CD_ARG_TYPE>
221void operator()(classdesc::json_pack_t& targ, const classdesc::string& desc,_CD_ARG_TYPE& arg)
222{
223::json_pack(targ,desc+"",(int&)arg);
224}
225template <class _CD_TYPE>
226void type(classdesc::json_pack_t& targ, const classdesc::string& desc)
227{
228}
229};
230template <> struct access_json_pack< struct ::classdesc::RESTProcessType > {
231template <class _CD_ARG_TYPE>
232void operator()(classdesc::json_pack_t& targ, const classdesc::string& desc,_CD_ARG_TYPE& arg)
233{
234}
235template <class _CD_TYPE>
236void type(classdesc::json_pack_t& targ, const classdesc::string& desc)
237{
238}
239};
240
241template <> struct access_json_unpack< enum ::classdesc::RESTProcessType::Type > {
242template <class _CD_ARG_TYPE>
243void operator()(classdesc::json_unpack_t& targ, const classdesc::string& desc,_CD_ARG_TYPE& arg)
244{
245::json_unpack(targ,desc+"",(int&)arg);
246}
247template <class _CD_TYPE>
248void type(classdesc::json_unpack_t& targ, const classdesc::string& desc)
249{
250}
251};
252template <> struct access_json_unpack< struct ::classdesc::RESTProcessType > {
253template <class _CD_ARG_TYPE>
254void operator()(classdesc::json_unpack_t& targ, const classdesc::string& desc,_CD_ARG_TYPE& arg)
255{
256}
257template <class _CD_TYPE>
258void type(classdesc::json_unpack_t& targ, const classdesc::string& desc)
259{
260}
261};
262}
263
264#include "polyAccessJsonPack.h"
265
266#endif
Definition json_pack_base.h:35
Definition json_pack_base.h:99
void objectify()
convert this to an object type (if not already)
Definition json_pack_base.h:160
bool throw_on_error
enable exceptions on error conditions
Definition json_pack_base.h:101
Contains access_* structs, and nothing else. These structs are used to gain access to private members...
Definition classdesc_access.h:20
Contains definitions related to classdesc functionality.
json5_parser::mValue & json_find(json5_parser::mValue &x, std::string name)
find an object named by name within the json object x
Definition json_pack_base.h:228
void json_pack(json_pack_t &o, const string &d, T &a)
forward declare generic json operations
Definition json_pack_epilogue.h:61
STL namespace.
Definition json_pack_epilogue.h:27
Definition classdesc.h:405
helper for constructing null descriptors
Definition classdesc.h:1106
types for RESTProcess and other embedding applications
Definition classdesc.h:1137
Definition classdesc.h:299
controlled template specialisation: stolen from boost::enable_if.
Definition classdesc.h:282
Definition classdesc.h:571
Definition json_pack_epilogue.h:92
Definition json_pack_epilogue.h:104