...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
For large inputs parsing into the library's containers followed by conversion
via value_to
might be prohibitively expensive. For these cases the library provides components
that allow parsing directly into user-provided objects.
The drawback of this approach is that fully custom type representations are
not supported, only the library-provided conversions are. Also all objects
that should be populated by parsing have to be default constructible types.
This includes not only the top-level object, but e.g. elements of containers,
members of described struct
s,
and alternatives of variants.
That being said, if your types are default-constructible and you don't need
the customisability allowed by value_to
, then you can get a significant
performance boost with direct parsing.
Direct parsing is performed by the parse_into
family of functions.
The library provides overloads that take either string_view
or std::istream
,
and can report errors either via throwing exceptions or setting an error
code.
std::map< std::string, std::vector<int> > vectors; string_view input = R"( { "even": [2,4,6], "odd": [1,3,5] } )"; parse_into(vectors, input);
Finally, if you need to combine incremental parsing with direct parsing,
you can resort to parser_for
. parser_for<T>
is an instantiation of basic_parser
that parses into an
object of type T
, and is
what parse_into
uses under the hood.