Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…to jsonschema
  • Loading branch information
danielaparker committed Feb 15, 2025
2 parents 5eeae6b + f5f1258 commit 03de6f3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
8 changes: 4 additions & 4 deletions include/jsoncons/json_traits_macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<char_type,class_type>::Member##_str(char_type{}),class_instance.Member);} \
{Json jitem{class_instance.Member, alloc}; ajson.try_emplace(json_traits_macro_names<char_type,class_type>::Member##_str(char_type{}),std::move(jitem));} \
else {json_traits_helper<Json>::set_optional_json_member(json_traits_macro_names<char_type,class_type>::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<char_type,class_type>::Member##_str(char_type{}),class_instance.Member);
{Json jitem{class_instance.Member, alloc}; ajson.try_emplace(json_traits_macro_names<char_type,class_type>::Member##_str(char_type{}),std::move(jitem));}

#define JSONCONS_MEMBER_TRAITS_BASE(AsT,ToJ,NumTemplateParams,ClassType,NumMandatoryParams1,NumMandatoryParams2, ...) \
namespace jsoncons \
Expand Down Expand Up @@ -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<Json>::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)
Expand All @@ -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, )
Expand Down
32 changes: 24 additions & 8 deletions include/jsoncons/json_type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,9 @@ has_can_convert = extension_traits::is_detected<traits_can_convert_t, Json, T>;
!std::is_polymorphic<ValueType>::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<ValueType>();
}
Expand All @@ -1195,11 +1197,12 @@ has_can_convert = extension_traits::is_detected<traits_can_convert_t, Json, T>;
return j.is_null() ? std::shared_ptr<ValueType>(nullptr) : std::make_shared<ValueType>(j.template as<ValueType>());
}

static Json to_json(const std::shared_ptr<ValueType>& ptr)
static Json to_json(const std::shared_ptr<ValueType>& ptr,
const allocator_type& alloc = allocator_type())
{
if (ptr.get() != nullptr)
{
Json j(*ptr);
Json j(*ptr, alloc);
return j;
}
else
Expand All @@ -1215,7 +1218,9 @@ has_can_convert = extension_traits::is_detected<traits_can_convert_t, Json, T>;
!std::is_polymorphic<ValueType>::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<ValueType>();
}
Expand All @@ -1225,11 +1230,12 @@ has_can_convert = extension_traits::is_detected<traits_can_convert_t, Json, T>;
return j.is_null() ? std::unique_ptr<ValueType>(nullptr) : jsoncons::make_unique<ValueType>(j.template as<ValueType>());
}

static Json to_json(const std::unique_ptr<ValueType>& ptr)
static Json to_json(const std::unique_ptr<ValueType>& ptr,
const allocator_type& alloc = allocator_type())
{
if (ptr.get() != nullptr)
{
Json j(*ptr);
Json j(*ptr, alloc);
return j;
}
else
Expand All @@ -1243,6 +1249,7 @@ has_can_convert = extension_traits::is_detected<traits_can_convert_t, Json, T>;
struct json_type_traits<Json, jsoncons::optional<T>,
typename std::enable_if<!is_json_type_traits_declared<jsoncons::optional<T>>::value>::type>
{
using allocator_type = typename Json::allocator_type;
public:
static bool is(const Json& j) noexcept
{
Expand All @@ -1254,9 +1261,10 @@ has_can_convert = extension_traits::is_detected<traits_can_convert_t, Json, T>;
return j.is_null() ? jsoncons::optional<T>() : jsoncons::optional<T>(j.template as<T>());
}

static Json to_json(const jsoncons::optional<T>& val)
static Json to_json(const jsoncons::optional<T>& val,
const allocator_type& alloc = allocator_type())
{
return val.has_value() ? Json(*val) : Json::null();
return val.has_value() ? Json(*val, alloc) : Json::null();
}
};

Expand Down Expand Up @@ -1288,6 +1296,7 @@ has_can_convert = extension_traits::is_detected<traits_can_convert_t, Json, T>;
struct json_type_traits<Json, basic_bigint<Allocator>>
{
public:
using allocator_type = typename Json::allocator_type;
using char_type = typename Json::char_type;

static bool is(const Json& j) noexcept
Expand Down Expand Up @@ -1332,6 +1341,13 @@ has_can_convert = extension_traits::is_detected<traits_can_convert_t, Json, T>;
val.write_string(s);
return Json(s,semantic_tag::bigint);
}

static Json to_json(const basic_bigint<Allocator>& val, const allocator_type&)
{
std::basic_string<char_type> s;
val.write_string(s);
return Json(s,semantic_tag::bigint);
}
};

// std::valarray
Expand Down

0 comments on commit 03de6f3

Please # to comment.