-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
Fix Issue#1813: user defined input adapters #2145
Conversation
Reviewing this further, I realized that there IS actually a non-trivial API breakage in there. the previous I see three possible resolutions:
The easiest fix is 1., the cleanest is 3., as it maintains backwards compatibility, and the API inconsistency is counterbalanced by the deprecation. (And I would ideally take the opportunity to mark the third overload: If you'd like, I can hold my nose and go for 2., but I think this unnecessarily increases the API surface since you can do The one thing I'm really against is bringing back the overload just for edit: I've amended the PR with option 3, as well as adding deprecation warnings to the third overload. Feel free to ask for them to be removed if you think that's unreasonable. |
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.
Looks good to me.
Thanks so much!!! |
The code looks great - I may add some more examples and tests in the next days. |
🔖 Release itemThis issue/PR will be part of the next release of the library. This template helps preparing the release notes. Type
Description
|
That's awesome! I wasn't expecting this to actually make it in 3.8.0. |
This is an attempt to address #1813
This change adds generalized Iterator support for inputs. In effect, this is just 2 changes:
a) loosen the restriction on iterators having to be of continuous storage
b) harmonize the
parse()
API with thefrom_*()
API.All
parse()
andfrom_*()
functions now have 3 (or 4, see below) overloads:parse(InputType)
: This will go into the specialized input adapters forFILE
,istream
or null-terminated string. As a fallback, it will perform a std-using ADL-lookup onbegin(input)
andend(input)
to invoke 2.parse(iterator, iterator)
: This will instantiate an input adapter for that specific iterator type. This will also handle all of the contiguous memory scenarios, (usingconst char*
as the iterator type). The compiler has enough information to optimize this appropriately. If thevalue_type
of the iterator is a multi-byte character type, wchar to char conversion will be performed.parse(span_input_adapter)
: This exists specifically to support the existingparse({ptr, len})
syntax, but is marked as deprecated, as it's redundant with 2. apart from minor syntactic sugar.User defined input is now a matter of implementing an arbitrary iterator type. It can also be done by having
begin(T)
andend(T)
return an existing iterator type (likeconst char*
)From a very pedantic POV, this might be "technically" API breaking, because a lot of the SFINAE stuff that was in place to ensure that data is continuous in memory are now gone. Code that would have failed to compile will now result in functional, but not as optimized as it could be program. i.e. If someone tries to parse a
std::list<char>
, it'll work, and this may or may not be what was desired.Pull request checklist
Read the Contribution Guidelines for detailed information.
include/nlohmann
directory, runmake amalgamate
to create the single-header filesingle_include/nlohmann/json.hpp
. The whole process is described here.