Json: Precompiled JSON library?

Created on 17 Nov 2019  路  12Comments  路  Source: nlohmann/json

Hi,
Is there a way to precompile the JSON library, instead of including a 22k LOC header file?
The compilation times increased dramatically when trying to switch from cJSOn (300 LOC heeader file) to nlohmann JSON. If we never modify that code, I see no point of recompiling everytime 22k LOC if we can have it precompiled.
We love the library but the single-include header is too big, I know that Catch2 wants to drop also the single-header because of this issue.
There are multiple "solutions" to this, but none of them is elegant enough (frw declarations, etc).
Thanks! :+1:

question stale

Most helpful comment

The library is heavily using templates, both for the internal types and for features like creating an array from an iterator range of any STL containers. I have no idea how to make a shared library out of this.

However, though I could reproduce the size of the PCH, the binaries are usually much smaller binaries. For instance, a small pretty printer

#include <nlohmann/json.hpp>
#include <iostream>

int main()
{
    nlohmann::json j;
    std::cin >> j;
    std::cout << j.dump(2) << std::endl;
}

compiles to a 68 KB binary (-O3 -flto -DNDEBUG) on my machine using Clang.

All 12 comments

The library heavily relies on templates, so I am not sure how to achieve this. Any ideas?

Apart from this, the multi-header variant contains a header with forward declarations - maybe this can help in the meantime.

@tawmoto Does your compiler support precompiled headers? I've made good experience with that for getting the compilation times down. I have not used it with this library though.

@nlohmann I will try the forward declarations. It's very hard for me to convince my colleagues to switch from cJSON to nlohmann json for this reason, although I love your library, it's fantastic. We use a lot precompiled libraries (openssl, curl, json, etc) with great results in compilation time. Because of this problem (big header), we switched from Catch2 to google test, which we precompile.
Here is the link regarding Catch2 decision:
https://codingnest.com/the-future-of-catch2/
https://twitter.com/horenmar_ctu/status/1093219300410880000

I really think of a solution to make it work in both situations. :-)
I see that there is already an old request for this: https://github.com/nlohmann/json/issues/84

@t-b Yes, that is my idea also, I will try this. The problem is that we compile with 4 compilers (gcc 5.5, 8.3, clang and VS2019) and I have to implement PCH for all of them in order to convince my colleagues.

Thank you both for your answers! :+1:

I've used this library precompiled under MSVC, Clang, Objective C++.

I've used this library precompiled under MSVC, Clang, Objective C++.

Could you share some experiences regarding compile times?

I don't have any data, but the compile times are very reasonable.

I generated a PCH but unfortunetaly it's huge, 61MB!

g++ -c json.hpp -o json.hpp.gch

-rw-rw-r-- 1 taw taw 791K nov 19 12:11 json.hpp
-rw-rw-r-- 1 taw taw  61M nov 19 12:46 json.hpp.gch

Perhaps the library is better suited for big projects, I have multiple small projects: 8 sloc, 15 sloc, 50 sloc. The json.hpp itself is 20 sloc.

Thank you guys for all your help.:)

If you don't already have a PCH for , , , and the like, then you're going to get those included in this PCH. What effect did it have on your compile time? Are you comparing your compile time to something including all the standard headers with and without a PCH?

@gregmarr I could not make the PCH to work, but a 61MB PCH inside a 10MB project is not possible for my team, unfortunetaly.
Thanks again for the help, I will stick with cJSON at the moment.

We tried to use nlohmann::json across different executable and libraries in embedded system. It would save a lot of flash space if we could compile nlohmann::json into a shared library.

The library is heavily using templates, both for the internal types and for features like creating an array from an iterator range of any STL containers. I have no idea how to make a shared library out of this.

However, though I could reproduce the size of the PCH, the binaries are usually much smaller binaries. For instance, a small pretty printer

#include <nlohmann/json.hpp>
#include <iostream>

int main()
{
    nlohmann::json j;
    std::cin >> j;
    std::cout << j.dump(2) << std::endl;
}

compiles to a 68 KB binary (-O3 -flto -DNDEBUG) on my machine using Clang.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

afowles picture afowles  路  3Comments

mlund picture mlund  路  4Comments

MariaRamos89 picture MariaRamos89  路  4Comments

sqwunkly picture sqwunkly  路  3Comments

Fonger picture Fonger  路  4Comments