diff --git a/include/jsoncons/json_traits_macros.hpp b/include/jsoncons/json_traits_macros.hpp index 2d2e0e479..0fa35d49a 100644 --- a/include/jsoncons/json_traits_macros.hpp +++ b/include/jsoncons/json_traits_macros.hpp @@ -268,12 +268,12 @@ namespace jsoncons #define JSONCONS_TO_JSON(Prefix, P2, P3, Member, Count) JSONCONS_TO_JSON_LAST(Prefix, P2, P3, Member, Count) #define JSONCONS_TO_JSON_LAST(Prefix, P2, P3, Member, Count) if ((num_params-Count) < num_mandatory_params2) \ - {ajson.try_emplace(json_traits_macro_names::Member##_str(char_type{}),class_instance.Member);} \ + {Json jitem{class_instance.Member, alloc}; ajson.try_emplace(json_traits_macro_names::Member##_str(char_type{}),std::move(jitem));} \ else {json_traits_helper::set_optional_json_member(json_traits_macro_names::Member##_str(char_type{}),class_instance.Member, ajson);} #define JSONCONS_ALL_TO_JSON(Prefix, P2, P3, Member, Count) JSONCONS_ALL_TO_JSON_LAST(Prefix, P2, P3, Member, Count) #define JSONCONS_ALL_TO_JSON_LAST(Prefix, P2, P3, Member, Count) \ - ajson.try_emplace(json_traits_macro_names::Member##_str(char_type{}),class_instance.Member); + {Json jitem{class_instance.Member, alloc}; ajson.try_emplace(json_traits_macro_names::Member##_str(char_type{}),std::move(jitem));} #define JSONCONS_MEMBER_TRAITS_BASE(AsT,ToJ,NumTemplateParams,ClassType,NumMandatoryParams1,NumMandatoryParams2, ...) \ namespace jsoncons \ @@ -373,7 +373,7 @@ namespace jsoncons \ #define JSONCONS_N_MEMBER_NAME_TO_JSON(P1, P2, P3, Seq, Count) JSONCONS_N_MEMBER_NAME_TO_JSON_LAST(P1, P2, P3, Seq, Count) #define JSONCONS_N_MEMBER_NAME_TO_JSON_LAST(P1, P2, P3, Seq, Count) if ((num_params-Count) < num_mandatory_params2) JSONCONS_EXPAND(JSONCONS_CONCAT(JSONCONS_N_MEMBER_NAME_TO_JSON_,JSONCONS_NARGS Seq) Seq) #define JSONCONS_N_MEMBER_NAME_TO_JSON_2(Member, Name) \ - {ajson.try_emplace(Name,class_instance.Member);} \ + {Json jitem{class_instance.Member, alloc}; ajson.try_emplace(Name,std::move(jitem));} \ else \ {json_traits_helper::set_optional_json_member(Name,class_instance.Member, ajson);} #define JSONCONS_N_MEMBER_NAME_TO_JSON_3(Member, Name, Mode) JSONCONS_N_MEMBER_NAME_TO_JSON_2(Member, Name) @@ -386,7 +386,7 @@ else \ #define JSONCONS_ALL_MEMBER_NAME_TO_JSON(P1, P2, P3, Seq, Count) JSONCONS_ALL_MEMBER_NAME_TO_JSON_LAST(P1, P2, P3, Seq, Count) #define JSONCONS_ALL_MEMBER_NAME_TO_JSON_LAST(P1, P2, P3, Seq, Count) JSONCONS_EXPAND(JSONCONS_CONCAT(JSONCONS_ALL_MEMBER_NAME_TO_JSON_,JSONCONS_NARGS Seq) Seq) -#define JSONCONS_ALL_MEMBER_NAME_TO_JSON_2(Member, Name) ajson.try_emplace(Name,class_instance.Member); +#define JSONCONS_ALL_MEMBER_NAME_TO_JSON_2(Member, Name) {Json jitem{class_instance.Member}; ajson.try_emplace(Name,std::move(jitem));} #define JSONCONS_ALL_MEMBER_NAME_TO_JSON_3(Member, Name, Mode) JSONCONS_ALL_MEMBER_NAME_TO_JSON_2(Member, Name) #define JSONCONS_ALL_MEMBER_NAME_TO_JSON_4(Member, Name, Mode, Match) JSONCONS_ALL_MEMBER_NAME_TO_JSON_6(Member, Name, Mode, Match,,) #define JSONCONS_ALL_MEMBER_NAME_TO_JSON_5(Member, Name, Mode, Match, Into) JSONCONS_ALL_MEMBER_NAME_TO_JSON_6(Member, Name, Mode, Match, Into, ) diff --git a/include/jsoncons/json_type_traits.hpp b/include/jsoncons/json_type_traits.hpp index 02a1db4fb..2ad7d8118 100644 --- a/include/jsoncons/json_type_traits.hpp +++ b/include/jsoncons/json_type_traits.hpp @@ -1185,7 +1185,9 @@ has_can_convert = extension_traits::is_detected; !std::is_polymorphic::value >::type> { - static bool is(const Json& j) noexcept + using allocator_type = typename Json::allocator_type; + + static bool is(const Json& j) noexcept { return j.is_null() || j.template is(); } @@ -1195,11 +1197,12 @@ has_can_convert = extension_traits::is_detected; return j.is_null() ? std::shared_ptr(nullptr) : std::make_shared(j.template as()); } - static Json to_json(const std::shared_ptr& ptr) + static Json to_json(const std::shared_ptr& ptr, + const allocator_type& alloc = allocator_type()) { if (ptr.get() != nullptr) { - Json j(*ptr); + Json j(*ptr, alloc); return j; } else @@ -1215,7 +1218,9 @@ has_can_convert = extension_traits::is_detected; !std::is_polymorphic::value >::type> { - static bool is(const Json& j) noexcept + using allocator_type = typename Json::allocator_type; + + static bool is(const Json& j) noexcept { return j.is_null() || j.template is(); } @@ -1225,11 +1230,12 @@ has_can_convert = extension_traits::is_detected; return j.is_null() ? std::unique_ptr(nullptr) : jsoncons::make_unique(j.template as()); } - static Json to_json(const std::unique_ptr& ptr) + static Json to_json(const std::unique_ptr& ptr, + const allocator_type& alloc = allocator_type()) { if (ptr.get() != nullptr) { - Json j(*ptr); + Json j(*ptr, alloc); return j; } else @@ -1243,6 +1249,7 @@ has_can_convert = extension_traits::is_detected; struct json_type_traits, typename std::enable_if>::value>::type> { + using allocator_type = typename Json::allocator_type; public: static bool is(const Json& j) noexcept { @@ -1254,9 +1261,10 @@ has_can_convert = extension_traits::is_detected; return j.is_null() ? jsoncons::optional() : jsoncons::optional(j.template as()); } - static Json to_json(const jsoncons::optional& val) + static Json to_json(const jsoncons::optional& val, + const allocator_type& alloc = allocator_type()) { - return val.has_value() ? Json(*val) : Json::null(); + return val.has_value() ? Json(*val, alloc) : Json::null(); } }; @@ -1288,6 +1296,7 @@ has_can_convert = extension_traits::is_detected; struct json_type_traits> { public: + using allocator_type = typename Json::allocator_type; using char_type = typename Json::char_type; static bool is(const Json& j) noexcept @@ -1332,6 +1341,13 @@ has_can_convert = extension_traits::is_detected; val.write_string(s); return Json(s,semantic_tag::bigint); } + + static Json to_json(const basic_bigint& val, const allocator_type&) + { + std::basic_string s; + val.write_string(s); + return Json(s,semantic_tag::bigint); + } }; // std::valarray