-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add to_json method for C arrays #508
add to_json method for C arrays #508
Conversation
src/json.hpp.re2c
Outdated
@@ -800,6 +800,11 @@ void to_json(BasicJsonType& j, const CompatibleObjectType& arr) | |||
external_constructor<value_t::object>::construct(j, arr); | |||
} | |||
|
|||
template <typename BasicJsonType, typename T, std::size_t N, enable_if_t<not std::is_constructible<typename BasicJsonType::string_t, T(&)[N]>::value, int> = 0> | |||
void to_json(BasicJsonType &j, T (&tab)[N]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could exploit the knowledge of the size and call a reserve on the internal vector before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, I shall add an overload to external_constructor
47f2db0
to
8ef0498
Compare
src/json.hpp.re2c
Outdated
@@ -536,6 +536,19 @@ struct external_constructor<value_t::array> | |||
} | |||
j.assert_invariant(); | |||
} | |||
|
|||
template<typename BasicJsonType, typename T, std::size_t N> | |||
static void construct(BasicJsonType& j, T (&arr)[N]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we not use the same implementation as line 523? std::begin and std::end work just fine on built-in arrays.
If the array can be converted to basic_json::string_t, the overload in this commit is not chosen.
8ef0498
to
dbebf8d
Compare
Indeed, I've been sloppy since this morning, good catch |
Thanks a lot!!! |
Hi, this commit intends to fix #502.
I don't think there is a way to initialize a C-array from a json object, hence the only
to_json
overload in this commit.