Skip to content

Commit 61b429a

Browse files
committed
try_decode_json
1 parent 9901b9a commit 61b429a

File tree

13 files changed

+88
-88
lines changed

13 files changed

+88
-88
lines changed

include/jsoncons/conv_error.hpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,6 @@ namespace jsoncons {
116116
not_base16
117117
};
118118

119-
template <typename InputIt>
120-
struct decode_result
121-
{
122-
InputIt it;
123-
conv_errc ec;
124-
};
125-
126119
} // namespace jsoncons
127120

128121
namespace std {

include/jsoncons/item_event_visitor.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,13 +1127,13 @@ namespace jsoncons {
11271127
switch (tag)
11281128
{
11291129
case semantic_tag::base64:
1130-
encode_base64(value.begin(), value.end(), key_);
1130+
bytes_to_base64(value.begin(), value.end(), key_);
11311131
break;
11321132
case semantic_tag::base16:
1133-
encode_base16(value.begin(), value.end(),key_);
1133+
bytes_to_base16(value.begin(), value.end(),key_);
11341134
break;
11351135
default:
1136-
encode_base64url(value.begin(), value.end(),key_);
1136+
bytes_to_base64url(value.begin(), value.end(),key_);
11371137
break;
11381138
}
11391139
}
@@ -1188,7 +1188,7 @@ namespace jsoncons {
11881188
if (level_stack_.back().is_key() || level_stack_.back().target() == target_t::buffer)
11891189
{
11901190
key_.clear();
1191-
encode_base64url(value.begin(), value.end(),key_);
1191+
bytes_to_base64url(value.begin(), value.end(),key_);
11921192
}
11931193

11941194
if (level_stack_.back().is_key())

include/jsoncons/json_encoder.hpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -761,23 +761,23 @@ namespace detail {
761761
case byte_string_chars_format::base16:
762762
{
763763
sink_.push_back('\"');
764-
std::size_t length = encode_base16(b.begin(),b.end(),sink_);
764+
std::size_t length = bytes_to_base16(b.begin(),b.end(),sink_);
765765
sink_.push_back('\"');
766766
column_ += (length + 2);
767767
break;
768768
}
769769
case byte_string_chars_format::base64:
770770
{
771771
sink_.push_back('\"');
772-
std::size_t length = encode_base64(b.begin(), b.end(), sink_);
772+
std::size_t length = bytes_to_base64(b.begin(), b.end(), sink_);
773773
sink_.push_back('\"');
774774
column_ += (length + 2);
775775
break;
776776
}
777777
case byte_string_chars_format::base64url:
778778
{
779779
sink_.push_back('\"');
780-
std::size_t length = encode_base64url(b.begin(),b.end(),sink_);
780+
std::size_t length = bytes_to_base64url(b.begin(),b.end(),sink_);
781781
sink_.push_back('\"');
782782
column_ += (length + 2);
783783
break;
@@ -991,7 +991,7 @@ namespace detail {
991991
sink_.push_back('~');
992992
++column_;
993993
}
994-
std::size_t length = encode_base64(v.begin(), v.end(), sink_);
994+
std::size_t length = bytes_to_base64(v.begin(), v.end(), sink_);
995995
sink_.push_back('\"');
996996
column_ += (length+2);
997997
break;
@@ -1014,7 +1014,7 @@ namespace detail {
10141014
sink_.push_back('~');
10151015
++column_;
10161016
}
1017-
std::size_t length = encode_base64url(v.begin(), v.end(), sink_);
1017+
std::size_t length = bytes_to_base64url(v.begin(), v.end(), sink_);
10181018
sink_.push_back('\"');
10191019
column_ += (length+2);
10201020
break;
@@ -1310,7 +1310,7 @@ namespace detail {
13101310
{
13111311
sink_.push_back('~');
13121312
}
1313-
encode_base64(v.begin(), v.end(), sink_);
1313+
bytes_to_base64(v.begin(), v.end(), sink_);
13141314
sink_.push_back('\"');
13151315
break;
13161316
}
@@ -1331,7 +1331,7 @@ namespace detail {
13311331
{
13321332
sink_.push_back('~');
13331333
}
1334-
encode_base64url(v.begin(), v.end(), sink_);
1334+
bytes_to_base64url(v.begin(), v.end(), sink_);
13351335
sink_.push_back('\"');
13361336
break;
13371337
}
@@ -1445,21 +1445,21 @@ namespace detail {
14451445
case byte_string_chars_format::base16:
14461446
{
14471447
sink_.push_back('\"');
1448-
encode_base16(b.begin(),b.end(),sink_);
1448+
bytes_to_base16(b.begin(),b.end(),sink_);
14491449
sink_.push_back('\"');
14501450
break;
14511451
}
14521452
case byte_string_chars_format::base64:
14531453
{
14541454
sink_.push_back('\"');
1455-
encode_base64(b.begin(), b.end(), sink_);
1455+
bytes_to_base64(b.begin(), b.end(), sink_);
14561456
sink_.push_back('\"');
14571457
break;
14581458
}
14591459
case byte_string_chars_format::base64url:
14601460
{
14611461
sink_.push_back('\"');
1462-
encode_base64url(b.begin(),b.end(),sink_);
1462+
bytes_to_base64url(b.begin(),b.end(),sink_);
14631463
sink_.push_back('\"');
14641464
break;
14651465
}

include/jsoncons/json_type_traits.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,7 +1819,7 @@ namespace variant_detail
18191819
{
18201820
jsoncons::string_view sv = j.as_string_view();
18211821
null_back_insertable_byte_container cont;
1822-
auto result = decode_base16(sv.begin(), sv.end(), cont);
1822+
auto result = base16_to_bytes(sv.begin(), sv.end(), cont);
18231823
return result.ec == conv_errc::success ? true : false;
18241824
}
18251825
return false;
@@ -1844,7 +1844,7 @@ namespace variant_detail
18441844
else
18451845
{
18461846
jsoncons::string_view sv = j.as_string_view();
1847-
auto result = decode_base16(sv.begin(), sv.end(), bits);
1847+
auto result = base16_to_bytes(sv.begin(), sv.end(), bits);
18481848
if (result.ec != conv_errc::success)
18491849
{
18501850
JSONCONS_THROW(conv_error(conv_errc::not_bitset));

include/jsoncons/reflect/conv_result.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,22 +154,22 @@ class conv_result
154154
JSONCONS_THROW(std::runtime_error("Bad conv_result access"));
155155
}
156156

157-
constexpr const T* operator->() const
157+
const T* operator->() const
158158
{
159159
return std::addressof(this->value_);
160160
}
161161

162-
constexpr T* operator->()
162+
T* operator->()
163163
{
164164
return std::addressof(this->value_);
165165
}
166166

167-
constexpr const T& operator*() const&
167+
const T& operator*() const&
168168
{
169169
return value();
170170
}
171171

172-
constexpr T& operator*() &
172+
T& operator*() &
173173
{
174174
return value();
175175
}

include/jsoncons/reflect/decode_result.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,22 +195,22 @@ class decode_result
195195
JSONCONS_THROW(std::runtime_error("Bad decode_result access"));
196196
}
197197

198-
constexpr const T* operator->() const
198+
const T* operator->() const
199199
{
200200
return std::addressof(this->value_);
201201
}
202202

203-
constexpr T* operator->()
203+
T* operator->()
204204
{
205205
return std::addressof(this->value_);
206206
}
207207

208-
constexpr const T& operator*() const&
208+
const T& operator*() const&
209209
{
210210
return value();
211211
}
212212

213-
constexpr T& operator*() &
213+
T& operator*() &
214214
{
215215
return value();
216216
}

include/jsoncons/utility/byte_string.hpp

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,20 @@
2727

2828
namespace jsoncons {
2929

30+
template <typename InputIt>
31+
struct to_bytes_result
32+
{
33+
InputIt it;
34+
conv_errc ec;
35+
};
36+
3037
// Algorithms
3138

3239
namespace detail {
3340

3441
template <typename InputIt,typename Container>
3542
typename std::enable_if<std::is_same<typename std::iterator_traits<InputIt>::value_type,uint8_t>::value,size_t>::type
36-
encode_base64_generic(InputIt first, InputIt last, const char alphabet[65], Container& result)
43+
bytes_to_base64_generic(InputIt first, InputIt last, const char alphabet[65], Container& result)
3744
{
3845
std::size_t count = 0;
3946
unsigned char a3[3];
@@ -92,8 +99,8 @@ namespace detail {
9299
}
93100

94101
template <typename InputIt,typename F,typename Container>
95-
typename std::enable_if<ext_traits::is_back_insertable_byte_container<Container>::value,decode_result<InputIt>>::type
96-
decode_base64_generic(InputIt first, InputIt last,
102+
typename std::enable_if<ext_traits::is_back_insertable_byte_container<Container>::value,to_bytes_result<InputIt>>::type
103+
base64_to_bytes_generic(InputIt first, InputIt last,
97104
const uint8_t reverse_alphabet[256],
98105
F f,
99106
Container& result)
@@ -106,7 +113,7 @@ namespace detail {
106113
{
107114
if (!f(*first))
108115
{
109-
return decode_result<InputIt>{first, conv_errc::conversion_failed};
116+
return to_bytes_result<InputIt>{first, conv_errc::conversion_failed};
110117
}
111118

112119
a4[i++] = static_cast<uint8_t>(*first++);
@@ -144,14 +151,14 @@ namespace detail {
144151
result.push_back(a3[j]);
145152
}
146153
}
147-
return decode_result<InputIt>{last, conv_errc::success};
154+
return to_bytes_result<InputIt>{last, conv_errc::success};
148155
}
149156

150157
} // namespace detail
151158

152159
template <typename InputIt,typename Container>
153160
typename std::enable_if<std::is_same<typename std::iterator_traits<InputIt>::value_type,uint8_t>::value,size_t>::type
154-
encode_base16(InputIt first, InputIt last, Container& result)
161+
bytes_to_base16(InputIt first, InputIt last, Container& result)
155162
{
156163
static constexpr char characters[] = "0123456789ABCDEF";
157164

@@ -166,24 +173,24 @@ namespace detail {
166173

167174
template <typename InputIt,typename Container>
168175
typename std::enable_if<std::is_same<typename std::iterator_traits<InputIt>::value_type,uint8_t>::value,size_t>::type
169-
encode_base64url(InputIt first, InputIt last, Container& result)
176+
bytes_to_base64url(InputIt first, InputIt last, Container& result)
170177
{
171178
static constexpr char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
172179
"abcdefghijklmnopqrstuvwxyz"
173180
"0123456789-_"
174181
"\0";
175-
return detail::encode_base64_generic(first, last, alphabet, result);
182+
return detail::bytes_to_base64_generic(first, last, alphabet, result);
176183
}
177184

178185
template <typename InputIt,typename Container>
179186
typename std::enable_if<std::is_same<typename std::iterator_traits<InputIt>::value_type,uint8_t>::value,size_t>::type
180-
encode_base64(InputIt first, InputIt last, Container& result)
187+
bytes_to_base64(InputIt first, InputIt last, Container& result)
181188
{
182189
static constexpr char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
183190
"abcdefghijklmnopqrstuvwxyz"
184191
"0123456789+/"
185192
"=";
186-
return detail::encode_base64_generic(first, last, alphabet, result);
193+
return detail::bytes_to_base64_generic(first, last, alphabet, result);
187194
}
188195

189196
template <typename Char>
@@ -207,8 +214,8 @@ namespace detail {
207214
// decode
208215

209216
template <typename InputIt,typename Container>
210-
typename std::enable_if<ext_traits::is_back_insertable_byte_container<Container>::value,decode_result<InputIt>>::type
211-
decode_base64url(InputIt first, InputIt last, Container& result)
217+
typename std::enable_if<ext_traits::is_back_insertable_byte_container<Container>::value,to_bytes_result<InputIt>>::type
218+
base64url_to_bytes(InputIt first, InputIt last, Container& result)
212219
{
213220
static constexpr uint8_t reverse_alphabet[256] = {
214221
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
@@ -228,15 +235,15 @@ namespace detail {
228235
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
229236
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
230237
};
231-
auto retval = jsoncons::detail::decode_base64_generic(first, last, reverse_alphabet,
238+
auto retval = jsoncons::detail::base64_to_bytes_generic(first, last, reverse_alphabet,
232239
is_base64url<typename std::iterator_traits<InputIt>::value_type>,
233240
result);
234-
return retval.ec == conv_errc::success ? retval : decode_result<InputIt>{retval.it, conv_errc::not_base64url};
241+
return retval.ec == conv_errc::success ? retval : to_bytes_result<InputIt>{retval.it, conv_errc::not_base64url};
235242
}
236243

237244
template <typename InputIt,typename Container>
238-
typename std::enable_if<ext_traits::is_back_insertable_byte_container<Container>::value,decode_result<InputIt>>::type
239-
decode_base64(InputIt first, InputIt last, Container& result)
245+
typename std::enable_if<ext_traits::is_back_insertable_byte_container<Container>::value,to_bytes_result<InputIt>>::type
246+
base64_to_bytes(InputIt first, InputIt last, Container& result)
240247
{
241248
static constexpr uint8_t reverse_alphabet[256] = {
242249
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
@@ -256,20 +263,20 @@ namespace detail {
256263
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
257264
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
258265
};
259-
auto retval = jsoncons::detail::decode_base64_generic(first, last, reverse_alphabet,
266+
auto retval = jsoncons::detail::base64_to_bytes_generic(first, last, reverse_alphabet,
260267
is_base64<typename std::iterator_traits<InputIt>::value_type>,
261268
result);
262-
return retval.ec == conv_errc::success ? retval : decode_result<InputIt>{retval.it, conv_errc::not_base64};
269+
return retval.ec == conv_errc::success ? retval : to_bytes_result<InputIt>{retval.it, conv_errc::not_base64};
263270
}
264271

265272
template <typename InputIt,typename Container>
266-
typename std::enable_if<ext_traits::is_back_insertable_byte_container<Container>::value,decode_result<InputIt>>::type
267-
decode_base16(InputIt first, InputIt last, Container& result)
273+
typename std::enable_if<ext_traits::is_back_insertable_byte_container<Container>::value,to_bytes_result<InputIt>>::type
274+
base16_to_bytes(InputIt first, InputIt last, Container& result)
268275
{
269276
std::size_t len = std::distance(first,last);
270277
if (len & 1)
271278
{
272-
return decode_result<InputIt>{first, conv_errc::not_base16};
279+
return to_bytes_result<InputIt>{first, conv_errc::not_base16};
273280
}
274281

275282
InputIt it = first;
@@ -287,7 +294,7 @@ namespace detail {
287294
}
288295
else
289296
{
290-
return decode_result<InputIt>{first, conv_errc::not_base16};
297+
return to_bytes_result<InputIt>{first, conv_errc::not_base16};
291298
}
292299

293300
auto b = *it++;
@@ -301,12 +308,12 @@ namespace detail {
301308
}
302309
else
303310
{
304-
return decode_result<InputIt>{first, conv_errc::not_base16};
311+
return to_bytes_result<InputIt>{first, conv_errc::not_base16};
305312
}
306313

307314
result.push_back(val);
308315
}
309-
return decode_result<InputIt>{last, conv_errc::success};
316+
return to_bytes_result<InputIt>{last, conv_errc::success};
310317
}
311318

312319
struct byte_traits

0 commit comments

Comments
 (0)