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

Added to_string and added basic tests #1585

Merged
merged 3 commits into from
May 24, 2019
Merged

Added to_string and added basic tests #1585

merged 3 commits into from
May 24, 2019

Conversation

Macr0Nerd
Copy link
Contributor

I created the to_string function for the nlohmann namespace and JSON. It uses the template

template <class T>
inline std::string to_string(const T& j){
    return j.dump();
}

to easily accept any JSON or basic_json data type. This is in reference to Issue #916 to fully add to_string.

Note: As with all additives to the std namespace using ADL, it is required to declare using std::to_string otherwise the feature won't work.


Pull request checklist

Read the Contribution Guidelines for detailed information.

  • Changes are described in the pull request, or an existing issue is referenced.
  • The test suite compiles and runs without error.
  • Code coverage is 100%. Test cases can be added by editing the test suite.
  • The source code is amalgamated; that is, after making changes to the sources in the include/nlohmann directory, run make amalgamate to create the single-header file single_include/nlohmann/json.hpp. The whole process is described here.

Please don't

  • The C++11 support varies between different compilers and versions. Please note the list of supported compilers. Some compilers like GCC 4.7 (and earlier), Clang 3.3 (and earlier), or Microsoft Visual Studio 13.0 and earlier are known not to work due to missing or incomplete C++11 support. Please refrain from proposing changes that work around these compiler's limitations with #ifdefs or other means.
  • Specifically, I am aware of compilation problems with Microsoft Visual Studio (there even is an issue label for these kind of bugs). I understand that even in 2016, complete C++11 support isn't there yet. But please also understand that I do not want to drop features or uglify the code just to make Microsoft's sub-standard compiler happy. The past has shown that there are ways to express the functionality such that the code compiles with the most recent MSVC - unfortunately, this is not the main objective of the project.
  • Please refrain from proposing changes that would break JSON conformance. If you propose a conformant extension of JSON to be supported by the library, please motivate this extension.
  • Please do not open pull requests that address multiple issues.

@Macr0Nerd
Copy link
Contributor Author

Macr0Nerd commented Apr 26, 2019 via email

@theodelrieu
Copy link
Contributor

to the best of my knowledge to_string can’t be overwritten or specialized so that’s how you define the ADL.

That's not exactly it. The details are explained in this paper, but here's a short exlanation:

There is a flaw in the design of customization points. The wanted behavior would be to always call std::to_string, which would pick ADL customization points, if any, and then consider the overloads in std.

This will be fixed in C++20 with ranges::begin, ranges::end, etc... Note that nlohmann::to_json and nlohmann::from_json follow the same design.

Meanwhile, we must rely on using unqualified calls:

// Pre C++20
template <typename T> void f(T const& container) {
  using std::begin;
  // If a free function begin exists in T's namespace, it will be picked up
  // If not, std::begin will be used as a fallback 
  auto it = begin(container);
}

// C++20
template <typename T> void f(T const& container) {
  // ranges::begin has the same behavior than the pre-C++20 mechanism.
  auto it = ranges::begin(container);
}

Anyway, the using std::thing idiom is only necessary in generic code, and it's up to users to write it.

@Macr0Nerd
Copy link
Contributor Author

Macr0Nerd commented Apr 26, 2019 via email

@theodelrieu
Copy link
Contributor

Oh, I don't know why, but my brain made me believe you had put using std::to_string in the library's code... It's fine to let it in the tests :)

@Macr0Nerd
Copy link
Contributor Author

Macr0Nerd commented Apr 26, 2019 via email

@Macr0Nerd
Copy link
Contributor Author

@theodelrieu how do I use the ugly macro. I’m a bit confused on that front. Once I get that implemented I’ll close this and make a new PR

@theodelrieu
Copy link
Contributor

No need to open a new PR, you can just rebase/squash your commits instead.

Here is an example taken from the code:

NLOHMANN_BASIC_JSON_TPL_DECLARATION
struct is_basic_json<NLOHMANN_BASIC_JSON_TPL> : std::true_type {};

so basically you want:

NLOHMANN_BASIC_JSON_TPL_DECLARATION
std::string to_string(const NLOHMANN_BASIC_JSON_TPL &j) { /* ... */ }

@Macr0Nerd
Copy link
Contributor Author

@theodelrieu I updated and squashed and rebased. I apologize for the very last commit, rebasing caused some conflicts. But they are all resolved and ready to go!

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 2695250 on Macr0Nerd:iss916 into ee4028b on nlohmann:develop.

Copy link
Owner

@nlohmann nlohmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@nlohmann nlohmann self-assigned this May 24, 2019
@nlohmann nlohmann added this to the Release 3.6.2 milestone May 24, 2019
@nlohmann
Copy link
Owner


🔖 Release item

This issue/PR will be part of the next release of the library. This template helps preparing the release notes.

Type

  • ✨ New Feature
  • 🐛 Bug Fix
  • ⚡️ Improvement
  • 🔨 Further Change
  • 🔥 Deprecated function

Description

Contributor entry

  • Gabe Ron implemented the to_string method.

@nlohmann nlohmann merged commit 17c0849 into nlohmann:develop May 24, 2019
@nlohmann nlohmann modified the milestones: Release 3.6.2, Release 3.7.0 Jul 9, 2019
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.

4 participants