Skip to content
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

Merged
merged 1 commit into from
Mar 16, 2017

Conversation

theodelrieu
Copy link
Contributor

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.

@@ -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]) {
Copy link
Owner

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.

Copy link
Contributor Author

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

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.007%) to 99.857% when pulling 8ef0498 on theodelrieu:feature/c_array_conversion into 100bf3e on nlohmann:develop.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.007%) to 99.857% when pulling 8ef0498 on theodelrieu:feature/c_array_conversion into 100bf3e on nlohmann:develop.

@@ -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])
Copy link
Contributor

@gregmarr gregmarr Mar 16, 2017

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.
@theodelrieu
Copy link
Contributor Author

Indeed, I've been sloppy since this morning, good catch

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.007%) to 99.857% when pulling dbebf8d on theodelrieu:feature/c_array_conversion into 100bf3e on nlohmann:develop.

@nlohmann nlohmann added this to the Release 3.0.0 milestone Mar 16, 2017
@nlohmann nlohmann merged commit 773b33e into nlohmann:develop Mar 16, 2017
nlohmann added a commit that referenced this pull request Mar 16, 2017
@nlohmann
Copy link
Owner

Thanks a lot!!!

@theodelrieu theodelrieu deleted the feature/c_array_conversion branch March 13, 2019 08:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

conversion from/to C array is not supported
4 participants