Skip to content

Commit 9eef819

Browse files
committed
try_encode
1 parent dc5130e commit 9eef819

File tree

10 files changed

+298
-301
lines changed

10 files changed

+298
-301
lines changed

include/jsoncons/basic_json.hpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4718,7 +4718,7 @@ namespace jsoncons {
47184718
}
47194719
}
47204720

4721-
expected<void,std::error_code> try_dump_noflush(basic_json_visitor<char_type>& visitor, std::error_code& ec) const
4721+
expected<void,std::error_code> try_dump_noflush(basic_json_visitor<char_type>& visitor) const
47224722
{
47234723
std::error_code ec;
47244724
const ser_context context{};
@@ -4769,20 +4769,36 @@ namespace jsoncons {
47694769
{
47704770
visitor.key(string_view_type(((*it).key()).data(),(*it).key().length()), context, ec);
47714771
(*it).value().dump_noflush(visitor, ec);
4772+
if (JSONCONS_UNLIKELY(ec))
4773+
{
4774+
return expected<void,std::error_code>{unexpect, ec};
4775+
}
47724776
}
47734777
visitor.end_object(context, ec);
4774-
return ec ? expected<void,std::error_code>{unexpect, ec} : expected<void,std::error_code>{};
4778+
if (JSONCONS_UNLIKELY(ec))
4779+
{
4780+
return expected<void,std::error_code>{unexpect, ec};
4781+
}
4782+
return expected<void,std::error_code>{};
47754783
}
47764784
case json_storage_kind::array:
47774785
{
47784786
visitor.begin_array(size(), tag(), context, ec);
47794787
const array& o = cast<array_storage>().value();
47804788
for (const_array_iterator it = o.begin(); it != o.end(); ++it)
47814789
{
4782-
(*it).dump_noflush(visitor, ec);
4790+
auto r = (*it).try_dump_noflush(visitor);
4791+
if (JSONCONS_UNLIKELY(!r))
4792+
{
4793+
return r;
4794+
}
47834795
}
47844796
visitor.end_array(context, ec);
4785-
return ec ? expected<void,std::error_code>{unexpect, ec} : expected<void,std::error_code>{};
4797+
if (JSONCONS_UNLIKELY(ec))
4798+
{
4799+
return expected<void,std::error_code>{unexpect, ec};
4800+
}
4801+
return expected<void,std::error_code>{};
47864802
}
47874803
case json_storage_kind::json_const_ref:
47884804
return cast<json_const_reference_storage>().value().try_dump_noflush(visitor);

include/jsoncons/encode_json.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,9 @@ try_encode_json(const allocator_set<Alloc,TempAlloc>& aset,
201201
template <typename T,typename CharT>
202202
expected<void,std::error_code> try_encode_json(const T& val, basic_json_visitor<CharT>& encoder)
203203
{
204-
std::error_code ec;
205-
reflect::encode_traits<T>::try_encode(make_alloc_set(), val, encoder, ec);
204+
auto r = reflect::encode_traits<T>::try_encode(make_alloc_set(), val, encoder);
206205
encoder.flush();
207-
return ec ? expected<void, std::error_code>{jsoncons::unexpect, ec} : expected<void, std::error_code>{};
206+
return r;
208207
}
209208

210209
// encode_json_pretty

include/jsoncons/reflect/decode_traits.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ struct decode_traits
5858
{
5959
return result_type(jsoncons::unexpect, r1.error().code(), r1.error().message_arg(), line, column);
6060
}
61+
std::cout << "try_decode: " << *r1 << "\n";
6162
auto r2 = (*r1).template try_as<T>(aset);
6263
if (JSONCONS_UNLIKELY(!r2))
6364
{

0 commit comments

Comments
 (0)