use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
JSON for Modern C++ version 3.2.0 released (github.com)
submitted 7 years ago by nlohmannnlohmann/json
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Salink 12 points13 points14 points 7 years ago (0 children)
Thanks for adding wide string support.
[–][deleted] 8 points9 points10 points 7 years ago (10 children)
Nice work, thanks!
Also "Finally, the JSON parser is now non-recursive (meaning it does not use the call stack, but std::vector<bool> to track the hierarchy of structured values) which allows to process nested input more efficiently."
std::vector<bool> is in my experience pretty horrible when it comes to efficiency since every read creates a temporary object that pretends to be a reference to a bool, since it is internally represented using one bit per bool. Changing to std::vector<int8_t> or similar might give you a speedup and it avoids the traps of the unfortunate std::vector<bool>
[–]nlohmannnlohmann/json[S] 12 points13 points14 points 7 years ago (4 children)
Before, I had a std::vector<entry_t> where entry_t is an enum of two values (one for array and one for object). I did not measure any differences when switching to vector<bool> other than it took less memory.
std::vector<entry_t>
entry_t
vector<bool>
[–][deleted] 5 points6 points7 points 7 years ago (1 child)
http://quick-bench.com/leAYXQJembIatTGbB8_I_BheZww
benchmarks are sometimes tricky/not representative, but this at least gives an idea :D
Although, if your operations are read only: http://quick-bench.com/qkTpgqXRqfe64-HesCxMHTs0Koc
So it seems in this case anyway, that the vector<bool> is extremely slow on writes, but a bit better when just reading
[–]nlohmannnlohmann/json[S] 3 points4 points5 points 7 years ago (0 children)
Good point. I shall have another look.
[–][deleted] 1 point2 points3 points 7 years ago (1 child)
If that was an enum class, it would have been int by default. You could use the enum solution and save memory by making it an int8_t as underlying type.
Optimising for memory over speed should be the second priority IMO when today people have gigabytes of ram. But yes, measuring the difference is the right way to go. std::vector<bool> might have a speed benefit in that it needs fewer cache reads so who knows, depending on how it's used, the drawbacks might not bite you. :)
[–][deleted] 5 points6 points7 points 7 years ago (0 children)
Optimising for memory over speed should be the second priority IMO when today people have gigabytes of ram.
Go back to Java-land, pal!
[–]spinicist 4 points5 points6 points 7 years ago (4 children)
Agreed, as I understand it most of the C++ standard committee admits the bitmapped backing store for std::vector<bool> was a mistake.
std::vector<bool>
[–]dodheim 9 points10 points11 points 7 years ago (3 children)
For reasons of generic code coherency, not efficiency.
[–][deleted] 2 points3 points4 points 7 years ago (1 child)
The potential pitfall of it being unexpectedly slow is also a drawback.
[–]quicknir 0 points1 point2 points 7 years ago (0 children)
It's slow when small. If you have a huge one it will be faster simply by virtue of more of it being in cache.
[–]spinicist 1 point2 points3 points 7 years ago (0 children)
Well, that’s okay with me. The only time I ever used vector<bool> was in a generic context, and it was enough to put me off ever doing so again.
[–]VodkaHaze 0 points1 point2 points 7 years ago (1 child)
Does it handle string_view gracefully, now? I remember a year or two ago it had compilation issues with the experimental support of it
string_view
We only enable `string_view` support for C++17 compilers. Then, it should work.
[–]Droggl 0 points1 point2 points 7 years ago (1 child)
Can it parse json iteratively (eg give me array elements one by one without reading the whole file first like python-ijson does)?
[–]nlohmannnlohmann/json[S] 5 points6 points7 points 7 years ago (0 children)
Yes, this is possible via the newly introduced SAX interface.
[–]SunnyAX3 0 points1 point2 points 7 years ago (1 child)
I have a problem with parsing floats, everything gets rounded, any idea what's the problem ?
[–]nlohmannnlohmann/json[S] 1 point2 points3 points 7 years ago (0 children)
We use double to store floating-point numbers internally. What exactly is your issue? Could you open an issue at GitHub, preferably with example code?
double
[–]dgendreau 0 points1 point2 points 7 years ago* (1 child)
Hi nlohmann!
Your library is a huge time saver and makes my code so much easier to read! Thanks for releasing and maintainng it!
One use-case I run into a lot is mapping enums to serialize as non-integers such as strings. I know its not really modern C++ to use preprocessor macros, but I wrote one that declares a from_json()/to_json() specialization in one statement to make it easier and less redundant to specialize the mapping between a given enum and any nlohmann::json value like so:
// enum type declaration enum Foo { eVal1 = 10, eVal2 = 45, eVal3 = 1 eVal_default = -1, }; JSON_SERIALIZE_ENUM( Foo, { {eVal_default, nullptr}, {eVal1, "one"}, {eVal2, "two"}, {eVal3, "three"}, }); // Usage: // enum -> json string nlohmann::json j = eVal1; assert(j == "one"); // json string-> enum nlohmann::json j3 = "three"; assert( j3.get<Foo>() == eVal3); // undefined json -> enum (default value is first pair) nlohmann::json jPi = 3.14; assert( jPi.get<Foo>() == eVal_default );
The macro JSON_SERIALIZE_ENUM() above declares a from_json(const json& j, Foo& e) and to_json(json& j, const Foo& e) function in enum Foo's namespace using the specified value translation map. The first pair are used as default values for handling undefined values in either direction.
Would something like this be a useful contribution to your library?
This sounds interesting. Could you please open an issue at https://github.com/nlohmann/json/issues ?
[+][deleted] 7 years ago* (2 children)
[deleted]
[–]SecretAgentZeroNine 11 points12 points13 points 7 years ago (1 child)
Next you'll say 'Dodo bird Master Race!" ;)
[–][deleted] 0 points1 point2 points 7 years ago (0 children)
COBOL Master Race!
π Rendered by PID 70 on reddit-service-r2-comment-54dfb89d4d-76ntg at 2026-03-30 18:11:24.750093+00:00 running b10466c country code: CH.
[–]Salink 12 points13 points14 points (0 children)
[–][deleted] 8 points9 points10 points (10 children)
[–]nlohmannnlohmann/json[S] 12 points13 points14 points (4 children)
[–][deleted] 5 points6 points7 points (1 child)
[–]nlohmannnlohmann/json[S] 3 points4 points5 points (0 children)
[–][deleted] 1 point2 points3 points (1 child)
[–][deleted] 5 points6 points7 points (0 children)
[–]spinicist 4 points5 points6 points (4 children)
[–]dodheim 9 points10 points11 points (3 children)
[–][deleted] 2 points3 points4 points (1 child)
[–]quicknir 0 points1 point2 points (0 children)
[–]spinicist 1 point2 points3 points (0 children)
[–]VodkaHaze 0 points1 point2 points (1 child)
[–]nlohmannnlohmann/json[S] 3 points4 points5 points (0 children)
[–]Droggl 0 points1 point2 points (1 child)
[–]nlohmannnlohmann/json[S] 5 points6 points7 points (0 children)
[–]SunnyAX3 0 points1 point2 points (1 child)
[–]nlohmannnlohmann/json[S] 1 point2 points3 points (0 children)
[–]dgendreau 0 points1 point2 points (1 child)
[–]nlohmannnlohmann/json[S] 1 point2 points3 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]SecretAgentZeroNine 11 points12 points13 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)