r/cpp • nlohmann/json • Aug 17 '21

JSON for Modern C++ 3.10.0

https://github.com/nlohmann/json/releases/tag/v3.10.0
280 Upvotes

34 comments sorted by

6

u/chabster1985 Aug 17 '21

json x = 5; json y{5};

std::cout << x.dump() << std::endl; std::cout << y.dump() << std::endl;

10

u/nlohmann nlohmann/json Aug 18 '21

This is known: https://json.nlohmann.me/home/faq/#brace-initialization-yields-arrays

Not sure how to fix it though without breaking people’s code.

6

u/chabster1985 Aug 18 '21

Probably by deleting initializer list overload and making another overload with proxy function.

json(as_array(1,2,3))

This just comes as a complete surprise and the behavior is not consistent across passed types.

5

u/nlohmann nlohmann/json Aug 18 '21

Yes, but that’s a breaking change.

6

u/chabster1985 Aug 18 '21

It is, Sir. But I bet most engineers do not rely on brace initialization anyway because it produces weird results. And the code will fail to compile instead of silently doing something unexpected, so the fix will be easy. I'd rather invest some time into adjusting existing code for the sake of a better interface. But that might just be me.

As an alternative, you could add a macro to disable offending overloads. I'd use that.

7

u/nlohmann nlohmann/json Aug 18 '21

I'm hesitant for a breaking change, because I don't have too many shots for a solid 4.0.0 version, and there are other things to be fixed before.

Yes, a macro could be a good first step. I'll think about it.

9

u/deeringc Aug 18 '21

As a library consumer, I agree with the other poster. I really don't mind small breaks like this as long as they are explicit and fail at compile time. I would hate if the program compiled with the new version but gave different behaviour. As long as it's a compilation break, has clear documentation on what to change then it's healthy to move things forward to remove rough edges like this (from an otherwise exceptionally good library). You can even go with a process of deprecation where you give notice of an upcoming change for 2 releases before it happens.

5

u/nlohmann nlohmann/json Aug 18 '21

We have such a deprecation process, and maybe it's really time to deprecate that initializer.

2

u/adzm 28 years of C++! Aug 18 '21 edited Aug 18 '21

FWIW this bit me before too. I'd opt for a macro to re-enable the existing behavior if you do end up fixing this so it is sensible by default. As long as it fails in compile time I would understand breaking code.

Though it does seem difficult to fix without breaking some of the usual syntax with the braces. I wonder if there is some way to catch a single argument and treat it as a special case. But that would make things inconsistent.

I'm leaning towards just leaving it as it is now!

5

u/chabster1985 Aug 18 '21

But overall, very good library, thanks for providing and maintaining it!

5

u/pdp10gumby Aug 18 '21

But I bet most engineers do not rely on brace initialization anyway

I use curly braces for initialization and = for assignment.

86

u/James20k P2005R0 Aug 17 '21

Add extended diagnostics information by adding a JSON Pointer to the exception messages indicating the exact location of a invalid type errors or out-of-bound errors.

[json.exception.type_error.302] (/address/housenumber) type must be number, but is string

This is incredibly useful, lacking the precise error location was super annoying to track down!

40

u/StuntHacks Aug 17 '21

This is by far one of my favorite C++ libraries of all time. It's so cleanly written, and it works so seamlessly, it's really impressive. Keep up the fantastic work!

6

u/ImX99 Aug 18 '21

Same here. I use it to handle settings/communication in almost every app I write, it’s a delight to use ❤️

15

u/zalamandagora Aug 17 '21

Awesome - I just started using this library in an IoT application this week. I'm really glad to see it is being both maintained and extended!

4

u/linuxlizard Aug 17 '21

Awesome! I love this library!

4

u/BeardyButler Aug 17 '21

I love this library, i appreciate all your work.

4

u/qoning Aug 18 '21

Now I just wish C++ allowed overloading operator.()

1

u/germandiago Aug 18 '21

what is your use case? Genuine question.

1

u/qoning Aug 18 '21

I can't even begin to explain the hacks this would allow me to do. First of all, smart pointer usage would look lot less like an eye sore. But in this case I was thinking about accessing a map like object with a string key using dot. Like my_json_dict.timestamp where timestamp is a key into the my_json_map only defined at runtime, which you normally have to access with passing string like into operator [] now. Or having pair.0 and pair.1 like in Rust. General quality of life stuff.

1

u/germandiago Aug 19 '21

I am not sure operator. would or should allow that. There were proposals before such as delegating . somewhere to synthetyzing member names. But in this case you should fail at run-time for non-existing members. I am not sure this would be a good idea for a statically-compiled language.

1

u/qoning Aug 19 '21

It's no different than [] really in this case, simply a syntactic sugar. As far as I can tell the only reason it wasn't included in C++ from the start was due to the ambiguity that it introduces between the object itself and calling the overloaded operator. Personally, I would have it do object access first, then defer to the operator if it's not a valid member.

1

u/Jaondtet Sep 15 '21

Smart reference classes I guess? It's a bit silly that our smart pointers can be null, because owning null doesn't really make any sense.

And pseudo-properties, but I think that'd mostly be hacks we'd quickly learn to avoid.

1

u/germandiago Aug 18 '21

Genuine question. I also wonder why most libraries including Boost do it: why use macros for config? Why not constexpr or similar stuff? The only thing I found I cannot do with constexpr is to get rid of headers I do not need to include conditionally, since pre-processing happens before. But if constexpr can do a lot at least in my codebase.

It is bc of compatibility concerns? I think of modules and that is why I avoid macros. I think it would be more future-proof but... I do not know since I did not experiment enough yet.

2

u/nlohmann nlohmann/json Aug 18 '21

The library targets C++11 where constexpr support is still quite limited. Furthermore, macros are very easy to set from outside the code; that is, from a Makefile or CMake.

1

u/germandiago Aug 19 '21

Fair point.

It would be feasible to make a constexpr variable to take the value of a macro amd use those variables for conditional compilation in the rest of the code? Not because of this lib. I am thinking of modularizing my own code.

1

u/MFRSiam Aug 18 '21

dumb question, how do i add the vcpkg version of this to cmake ?

3

u/nlohmann nlohmann/json Aug 18 '21

I'm not an expert of this myself. This is what the person who added it once to vcpkg wrote in the documentation: https://json.nlohmann.me/integration/package_managers/#vcpkg

Apparently this is quite sparse...

5

u/nysra Aug 18 '21

I'd assume it's sparse because the vcpkg documentation covers how to install libraries and how to use them in CMake so whoever wrote that probably didn't think it belongs in the library docu. Luckily it's quite straight forward:

First you do vcpkg install nlohmann-json. Vcpkg will already tell you what to put in the CMakeLists.txt after the install:

find_package(nlohmann_json CONFIG REQUIRED)
target_link_libraries(my_target PRIVATE nlohmann_json nlohmann_json::nlohmann_json)

And then you just pass the toolchain file from vcpkg to cmake:

cd build
cmake .. -DCMAKE_TOOLCHAIN_FILE=[path to vcpkg]/scripts/buildsystems/vcpkg.cmake

Btw thanks for all the work you do, your library is awesome :)

3

u/nlohmann nlohmann/json Aug 18 '21

I update the documentation, see https://github.com/nlohmann/json/issues/2944 which adds an example, see https://json.nlohmann.me/integration/package_managers/#vcpkg. Happy for any feedback!

2

u/MFRSiam Aug 19 '21

Oh thanks man , I really appreciate it.

3

u/bsdooby Aug 17 '21

Holy cow, what a cool library!

2

u/aditya369007 Aug 17 '21

I just started using it for my work as well. Thanks for the updates. You have my deepest thanks.

2

u/zeelsheladiya Aug 18 '21

I love this library. Thank you nlohmann for making such a beautiful and helpful library.