aboutsummaryrefslogtreecommitdiff
path: root/include/jsoncons/converter.hpp
blob: 15d19178c8a156729db10d4ce9462c604f0aceca (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
// Copyright 2020 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_CONVERTER_HPP
#define JSONCONS_CONVERTER_HPP

#include <system_error> // std::error_code
#include <jsoncons/more_type_traits.hpp>
#include <jsoncons/byte_string.hpp>
#include <jsoncons/json_type.hpp>
#include <jsoncons/conv_error.hpp>
#include <jsoncons/detail/write_number.hpp> // from_integer

namespace jsoncons {

    template <class Into, class Enable=void>
    class converter
    {
    };

    // Into list like of bytes
    template <class Into>
    class converter<Into,typename std::enable_if<(!type_traits::is_basic_string<Into>::value && 
                                                   type_traits::is_back_insertable_byte_container<Into>::value) ||
                                                   type_traits::is_basic_byte_string<Into>::value>::type>
    {
        using value_type = typename Into::value_type;
        using allocator_type = typename Into::allocator_type;

    public:

        JSONCONS_CPP14_CONSTEXPR 
        Into from(const byte_string_view& bstr, semantic_tag tag, std::error_code& ec) const
        {
            Into bytes;
            from_(bytes, bstr, tag, ec);
            return bytes;
        }

        JSONCONS_CPP14_CONSTEXPR 
        Into from(const byte_string_view& bstr, semantic_tag tag, const allocator_type& alloc, std::error_code& ec) const
        {
            Into bytes(alloc);
            from_(bytes, bstr, tag, ec);
            return bytes;
        }

        template <class CharT>
        JSONCONS_CPP14_CONSTEXPR 
        Into from(const jsoncons::basic_string_view<CharT>& s, semantic_tag tag, std::error_code& ec) const
        {
            Into bytes;
            from_(bytes, s, tag, ec);
            return bytes;
        }

        template <class CharT>
        JSONCONS_CPP14_CONSTEXPR 
        Into from(const jsoncons::basic_string_view<CharT>& s, semantic_tag tag, const allocator_type& alloc, std::error_code& ec) const
        {
            Into bytes(alloc);
            from_(bytes, s, tag, ec);
            return bytes;
        }

    private:
        JSONCONS_CPP14_CONSTEXPR 
        void from_(Into& bytes, const byte_string_view& bstr, semantic_tag, std::error_code&) const
        {
            for (auto ch : bstr)
            {
                bytes.push_back(static_cast<value_type>(ch));
            }
        }

        template <class CharT>
        JSONCONS_CPP14_CONSTEXPR 
        typename std::enable_if<type_traits::is_narrow_character<CharT>::value>::type
        from_(Into& bytes, const jsoncons::basic_string_view<CharT>& s, semantic_tag tag, std::error_code& ec) const
        {
            switch (tag)
            {
                case semantic_tag::base16:
                {
                    auto res = decode_base16(s.begin(), s.end(), bytes);
                    if (res.ec != conv_errc::success)
                    {
                        ec = conv_errc::not_byte_string;
                    }
                    break;
                }
                case semantic_tag::base64:
                {
                    decode_base64(s.begin(), s.end(), bytes);
                    break;
                }
                case semantic_tag::base64url:
                {
                    decode_base64url(s.begin(), s.end(), bytes);
                    break;
                }
                default:
                {
                    ec = conv_errc::not_byte_string;
                    break;
                }
            }
        }

        template <class CharT>
        typename std::enable_if<type_traits::is_wide_character<CharT>::value>::type
        from_(Into& bytes, const jsoncons::basic_string_view<CharT>& s, semantic_tag tag, std::error_code& ec) const
        {
            std::string u;
            auto retval = unicode_traits::convert(s.data(), s.size(), u);
            if (retval.ec != unicode_traits::conv_errc())
            {
                ec = conv_errc::not_utf8;
                return;
            }
            from_(bytes, jsoncons::string_view(u), tag, ec);
        }
    };

    // Into string
    template <class Into>
    class converter<Into,typename std::enable_if<type_traits::is_basic_string<Into>::value>::type>
    {
        using char_type = typename Into::value_type;
        using allocator_type = typename Into::allocator_type;
        int dummy_;
    public:
        explicit converter(int dummy = int())
            : dummy_(dummy)
        {}
        template<class Integer>
        JSONCONS_CPP14_CONSTEXPR 
        typename std::enable_if<type_traits::is_integer<Integer>::value,Into>::type
        from(Integer val, semantic_tag, std::error_code&) const
        {
            Into s;
            jsoncons::detail::from_integer(val, s);
            return s;
        }

        template<class Integer>
        JSONCONS_CPP14_CONSTEXPR 
        typename std::enable_if<type_traits::is_integer<Integer>::value,Into>::type
        from(Integer val, semantic_tag, const allocator_type& alloc, std::error_code&) const
        {
            Into s(alloc);
            jsoncons::detail::from_integer(val, s);
            return s;
        }

        Into from(double val, semantic_tag, std::error_code&) const
        {
            Into s;
            jsoncons::detail::write_double f{float_chars_format::general,0};
            f(val, s);
            return s;
        }

        Into from(double val, semantic_tag, const allocator_type& alloc, std::error_code&) const
        {
            Into s(alloc);
            jsoncons::detail::write_double f{float_chars_format::general,0};
            f(val, s);
            return s;
        }

        Into from(half_arg_t, uint16_t val, semantic_tag, std::error_code&) const
        {
            Into s;
            jsoncons::detail::write_double f{float_chars_format::general,0};
            double x = binary::decode_half(val);
            f(x, s);
            return s;
        }

        Into from(half_arg_t, uint16_t val, semantic_tag, const allocator_type& alloc, std::error_code&) const
        {
            Into s(alloc);
            jsoncons::detail::write_double f{float_chars_format::general,0};
            double x = binary::decode_half(val);
            f(x, s);
            return s;
        }

        template <class ChT = char_type>
        JSONCONS_CPP14_CONSTEXPR
        Into from(const byte_string_view& bytes, semantic_tag tag, std::error_code& ec) const
        {
            Into s;
            from_(s, bytes, tag, ec);
            return s;
        }

        template <class ChT = char_type>
        JSONCONS_CPP14_CONSTEXPR
        Into from(const byte_string_view& bytes, semantic_tag tag, const allocator_type& alloc, std::error_code& ec) const
        {
            Into s(alloc);
            from_(s, bytes, tag, ec);
            return s;
        }

        constexpr 
        Into from(const jsoncons::basic_string_view<char_type>& s, semantic_tag, std::error_code&) const
        {
            return Into(s.data(), s.size());
        }

        constexpr 
        Into from(const jsoncons::basic_string_view<char_type>& s, semantic_tag, const allocator_type& alloc, std::error_code&) const
        {
            return Into(s.data(), s.size(), alloc);
        }

        JSONCONS_CPP14_CONSTEXPR 
        Into from(bool val, semantic_tag, std::error_code&) const
        {
            constexpr const char_type* true_constant = JSONCONS_CSTRING_CONSTANT(char_type,"true"); 
            constexpr const char_type* false_constant = JSONCONS_CSTRING_CONSTANT(char_type,"false"); 

            return val ? Into(true_constant,4) : Into(false_constant,5);
        }

        JSONCONS_CPP14_CONSTEXPR 
        Into from(bool val, semantic_tag, const allocator_type& alloc, std::error_code&) const
        {
            constexpr const char_type* true_constant = JSONCONS_CSTRING_CONSTANT(char_type,"true"); 
            constexpr const char_type* false_constant = JSONCONS_CSTRING_CONSTANT(char_type,"false"); 

            return val ? Into(true_constant,4,alloc) : Into(false_constant,5,alloc);
        }

        JSONCONS_CPP14_CONSTEXPR 
        Into from(null_type, semantic_tag, std::error_code&) const
        {
            constexpr const char_type* null_constant = JSONCONS_CSTRING_CONSTANT(char_type,"null"); 

            return Into(null_constant,4);
        }

        JSONCONS_CPP14_CONSTEXPR 
        Into from(null_type, semantic_tag, const allocator_type& alloc, std::error_code&) const
        {
            constexpr const char_type* null_constant = JSONCONS_CSTRING_CONSTANT(char_type,"null"); 

            return Into(null_constant,4,alloc);
        }
    private:

        template <class ChT = char_type>
        JSONCONS_CPP14_CONSTEXPR
        typename std::enable_if<type_traits::is_byte<ChT>::value>::type
        from_(Into& s, const byte_string_view& bytes, semantic_tag tag, std::error_code&) const
        {
            switch (tag)
            {
                case semantic_tag::base64:
                    encode_base64(bytes.begin(), bytes.end(), s);
                    break;
                case semantic_tag::base16:
                    encode_base16(bytes.begin(), bytes.end(), s);
                    break;
                default:
                    encode_base64url(bytes.begin(), bytes.end(), s);
                    break;
            }
        }

        template <class ChT = char_type>
        typename std::enable_if<!type_traits::is_byte<ChT>::value>::type
        from_(Into& s, const byte_string_view& bytes, semantic_tag tag, std::error_code& ec) const
        {
            converter<std::string> convert{ dummy_ };
            std::string u = convert.from(bytes, tag, ec);

            auto retval = unicode_traits::convert(u.data(), u.size(), s);
            if (retval.ec != unicode_traits::conv_errc())
            {
                ec = conv_errc::not_wide_char;
            }
        }

    };

} // namespace jsoncons

#endif