FLiT
FLiT copied to clipboard
Replace Variant with JSON
Feature Request
Describe the new feature:
So far, we have implemented our own flit::Variant
type that can take on different types of values. This has proven to be difficult to add new types and time consuming to develop and test. Now that we have it, it is stable and works well.
We see that many of the goals and types supported by flit::Variant
are also supported by JSON. The JSON type is quite standard and found everywhere. It may be good to move to having JSON be our backend type to add more flexibility and stability. With a JSON type, you can have dictionaries, lists, and compose them in each other seamlessly. This would add significant capabilities to the use of FLiT.
Suggested change: I propose that we specifically use this implementation of JSON in C++:
https://github.com/nlohmann/json
for the following reasons:
- A single
json.hpp
file - Easy to use
- Extensively tested
It may not be the fastest implementation of JSON in C++, but ease of use and a single file that can be copied into the FLiT repository are enticing.
We could incorporate a JSON library in the following ways:
- Allow
json
objects to be another type stored inside offlit::Variant
- The least amount of obtrusiveness.
- Supplies backward compatibility with both source code and output
*.dat
files
- Change
flit::Variant
to usejson
as its backend implementation
- Lightly breaks backward compatibility of the
*.dat
output files - Perhaps
json.hpp
could be modified to allow long double types
- Replace
flit::Variant
withjson
to be returned from flit tests
- Significantly breaks backward compatibility requiring change of test source code
- Would increase the stability of FLiT and perhaps make it easier to use
- Perhaps
json.hpp
could be modified to allow long double types
Note: This library is under the MIT license. I recommend moving to a 3rdparty
directory along with tinydir.h
. Since we would only copy over the single header file (of 22,000 lines of code), we would not need to copy over the LICENSE file since it has the license declaration at the beginning of the file.
Alternative approaches: One thing we could do is implement our own JSON parser and output. We would be able to customize the output to also support long double and float.
Besides making our own implementation, there are many other JSON libraries out there for use by C++. Here are some choices:
-
json-c: (MIT License) A JSON library in C. Available from package managers. For Ubuntu, known as
libjson-c-dev
. -
jsoncpp: (MIT License) A JSON library written in C++. Available from package managers. For Ubuntu, known as
libjsoncpp-dev
. -
yajl: (ISC License) "Yet Another Json Library" - implemented in C. Available from package managers. For Ubuntu, known as
libyajl-dev
-
jansson: (MIT License) A C library for streaming JSON. Available from package managers. For Ubuntu, known as
libjansson-dev
. -
rapidjson: (MIT License) Fast JSON parser/generator for C++ with both SAX/DOM style API. Available from package managers. For Ubuntu, known as
rapidjson-dev
.
This would add another external dependency rather than just a single header file pulled in.
If speed is considered, here is a benchmark to compare 41 different implementations. It seems rapidjson
is one of the fastest with a large conformance (significantly faster than others).