summaryrefslogtreecommitdiff
path: root/vendor/jsoncons-0.104.0/jsoncons/serialization_traits.hpp
blob: aa22769fdea8d99ae068053924a99e484a4e6e6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Copyright 2017 Daniel Parker
// Distributed under the Boost license, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

// See https://github.com/danielaparker/jsoncons for latest version

#ifndef JSONCONS_SERIALIZATION_TRAITS_HPP
#define JSONCONS_SERIALIZATION_TRAITS_HPP

#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch"
#endif

#include <iostream>
#include <string>
#include <tuple>
#include <array>
#include <type_traits>
#include <memory>
#include <jsoncons/json_output_handler.hpp>
#include <jsoncons/serialization_options.hpp>
#include <jsoncons/json_serializer.hpp>
#include <jsoncons/jsoncons_utilities.hpp>

namespace jsoncons {

// serialization_traits

template <class T, class Enable = void>
struct serialization_traits
{
    template <class CharT>
    static void encode(const T&, basic_json_output_handler<CharT>&)
    {
    }
};

// dump

template <class CharT, class T>
void dump(const T& val, basic_json_output_handler<CharT>& handler)
{
    handler.begin_json();
    serialization_traits<T>::encode(val,handler);
    handler.end_json();
}

#if !defined(JSONCONS_NO_DEPRECATED)
template <class CharT, class T>
void dump_body(const T& val, basic_json_output_handler<CharT>& handler)
{
    dump_fragment(val,handler);
}
#endif

template <class CharT, class T>
void dump_fragment(const T& val, basic_json_output_handler<CharT>& handler)
{
    serialization_traits<T>::encode(val,handler);
}

template <class CharT, class T>
void dump(const T& val, std::basic_ostream<CharT>& os)
{
    basic_json_serializer<CharT> serializer(os);
    dump(val, serializer);
}

template <class CharT, class T>
void dump(const T& val, const basic_serialization_options<CharT>& options,
          std::basic_ostream<CharT>& os)
{
    basic_json_serializer<CharT> serializer(os, options);
    dump(val, serializer);
}

template <class CharT, class T>
void dump(const T& val, std::basic_ostream<CharT>& os, bool pprint)
{
    basic_json_serializer<CharT> serializer(os, pprint);
    dump(val, serializer);
}

template <class CharT, class T>
void dump(const T& val, const basic_serialization_options<CharT>& options,
          std::basic_ostream<CharT>& os, bool pprint)
{
    basic_json_serializer<CharT> serializer(os, options, pprint);
    dump(val, serializer);
}

// integer

template<class T>
struct serialization_traits<T,
                          typename std::enable_if<detail::is_integer_like<T>::value
>::type>
{
    template <class CharT>
    static void encode(T val, basic_json_output_handler<CharT>& handler)
    {
        handler.integer_value(val);
    }
};

// uinteger

template<class T>
struct serialization_traits<T,
                          typename std::enable_if<detail::is_uinteger_like<T>::value
>::type>
{
    template <class CharT>
    static void encode(T val, basic_json_output_handler<CharT>& handler)
    {
        handler.uinteger_value(val);
    }
};

// double

template<class T>
struct serialization_traits<T,
                          typename std::enable_if<detail::is_floating_point_like<T>::value
>::type>
{
    template <class CharT>
    static void encode(T val, basic_json_output_handler<CharT>& handler)
    {
        handler.double_value(val);
    }
};

// bool

template<>
struct serialization_traits<bool>
{
    template <class CharT>
    static void encode(bool val, basic_json_output_handler<CharT>& handler)
    {
        handler.bool_value(val);
    }
};

// string

template<class T>
struct serialization_traits<T,
    typename std::enable_if<detail::is_string_like<T>::value
>::type>
{
    template <class CharT>
    static void encode(const T& val, basic_json_output_handler<CharT>& handler)
    {
        handler.string_value(val);
    }
};

/*template<>
struct serialization_traits<typename type_wrapper<CharT>::const_pointer_type>
{
    template <class CharT>
    static void encode(typename type_wrapper<CharT>::const_pointer_type val, basic_json_output_handler<CharT>& handler)
    {
        handler.string_value(val);
    }
};*/

// sequence container (except string and array)

template<class T>
struct serialization_traits<T,
    typename std::enable_if<detail::is_vector_like<T>::value
>::type>
{
    typedef typename std::iterator_traits<typename T::iterator>::value_type value_type;

    template <class CharT>
    static void encode(const T& val, basic_json_output_handler<CharT>& handler)
    {
        handler.begin_array();
        for (auto it = std::begin(val); it != std::end(val); ++it)
        {
            serialization_traits<value_type>::encode(*it,handler);
        }
        handler.end_array();
    }
};

// std::array

template<class T, size_t N>
struct serialization_traits<std::array<T,N>>
{
    typedef typename std::array<T,N>::value_type value_type;
public:
   
    template <class CharT>
    static void encode(const std::array<T, N>& val, basic_json_output_handler<CharT>& handler)
    {
        handler.begin_array();
        for (auto it = std::begin(val); it != std::end(val); ++it)
        {
            serialization_traits<value_type>::encode(*it,handler);
        }
        handler.end_array();
    }
};

// associative container

template<class T>
struct serialization_traits<T,
    typename std::enable_if<detail::is_map_like<T>::value
>::type>
{
    typedef typename T::mapped_type mapped_type;
    typedef typename T::value_type value_type;

    template <class CharT>
    static void encode(const T& val, basic_json_output_handler<CharT>& handler)
    {
        handler.begin_object();
        for (auto it = std::begin(val); it != std::end(val); ++it)
        {
            handler.name(it->first);
            serialization_traits<mapped_type>::encode(it->second,handler);
        }
        handler.end_object();
    }
};

namespace detail { namespace streaming {

template<size_t Pos, class Tuple>
struct tuple_helper
{
    using element_type = typename std::tuple_element<std::tuple_size<Tuple>::value - Pos, Tuple>::type;
    using next = tuple_helper<Pos - 1, Tuple>;
    
    template <class CharT>
    static void encode(const Tuple& tuple, basic_json_output_handler<CharT>& handler)
    {
        serialization_traits<element_type>::encode(std::get<std::tuple_size<Tuple>::value - Pos>(tuple),handler);
        next::encode(tuple, handler);
    }
};

template<class Tuple>
struct tuple_helper<0, Tuple>
{
    template <class CharT>
    static void encode(const Tuple&, basic_json_output_handler<CharT>&)
    {
    }
};

}}

template<typename... E>
struct serialization_traits<std::tuple<E...>>
{
private:
    using helper = detail::streaming::tuple_helper<sizeof...(E), std::tuple<E...>>;

public:
    template <class CharT>
    static void encode(const std::tuple<E...>& value, basic_json_output_handler<CharT>& handler)
    {
        handler.begin_array();
        helper::encode(value, handler);
        handler.end_array();
    }
};

template<class T1, class T2>
struct serialization_traits<std::pair<T1,T2>>
{
public:
   
    template <class CharT>
    static void encode(const std::pair<T1,T2>& value, basic_json_output_handler<CharT>& handler)
    {
        handler.begin_array();
        serialization_traits<T1>::encode(value.first, handler);
        serialization_traits<T2>::encode(value.second, handler);
        handler.end_array();
    }
};

#if !defined(JSONCONS_NO_DEPRECATED)
template<class T>
struct serialization_traits<std::shared_ptr<T>>
{
public:
   
    template <class CharT>
    static void encode(std::shared_ptr<T> p, basic_json_output_handler<CharT>& handler)
    {
        serialization_traits<T>::encode(*p, handler);
    }
};
#endif

}

#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif

#endif