Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Variant with JSON #300

Open
mikebentley15 opened this issue Nov 25, 2019 · 0 comments
Open

Replace Variant with JSON #300

mikebentley15 opened this issue Nov 25, 2019 · 0 comments
Labels
c++ Involves touching c++ code documentation Involves touching documentation enhancement tests Involves touching tests

Comments

@mikebentley15
Copy link
Collaborator

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:

  1. Allow json objects to be another type stored inside of flit::Variant
  • The least amount of obtrusiveness.
  • Supplies backward compatibility with both source code and output *.dat files
  1. Change flit::Variant to use json as its backend implementation
  • Lightly breaks backward compatibility of the *.dat output files
  • Perhaps json.hpp could be modified to allow long double types
  1. Replace flit::Variant with json 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).

@mikebentley15 mikebentley15 added c++ Involves touching c++ code documentation Involves touching documentation enhancement tests Involves touching tests labels Nov 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Involves touching c++ code documentation Involves touching documentation enhancement tests Involves touching tests
Projects
None yet
Development

No branches or pull requests

1 participant