Go to file
marek.piotrowski 2fb234dbcb Modify readme
2023-08-04 23:56:14 +02:00
.github Add to CONTRIBUTING.md that make pretty is required for test updates. (#4045) 2023-06-08 18:47:44 +02:00
.reuse Use official Clang/GCC containers (#3703) 2022-08-27 17:28:50 +02:00
cmake Fix CI + new Doctest (#3985) 2023-05-21 17:23:18 +02:00
docs Use template get instead of get in examples (#4039) 2023-06-11 10:06:24 +02:00
example Add more tests 2023-07-23 23:20:49 +02:00
include/nlohmann Add more tests 2023-07-23 23:20:49 +02:00
LICENSES Use REUSE framework (#3546) 2022-07-20 12:38:07 +02:00
single_include/nlohmann Fix Clang-Tidy warnings (#4047) 2023-06-08 18:46:48 +02:00
tests Add more tests 2023-07-23 23:20:49 +02:00
tools PrettyPrinter: Check if match is valid before accessing group (#3920) 2023-01-14 16:07:49 +01:00
.cirrus.yml Prevent memory leak when exception is thrown in adl_serializer::to_json (#3901) 2023-03-08 13:43:45 +01:00
.clang-format 🚧 add Clang-Tidy configuration 2020-07-26 22:38:14 +02:00
.clang-tidy Fix Clang-Tidy warnings (#4047) 2023-06-08 18:46:48 +02:00
.gitignore More documentation updates for 3.11.0 (#3553) 2022-07-31 14:05:58 +02:00
.lgtm.yml Miscellaneous small fixes (#3643) 2022-08-07 13:52:43 +02:00
BUILD.bazel Add Bazel build support (#3709) 2022-09-18 08:15:12 +02:00
ChangeLog.md 🔖 set version to 3.11.2 2022-08-12 15:04:06 +02:00
CITATION.cff 🔖 set version to 3.11.2 2022-08-12 15:04:06 +02:00
CMakeLists.txt Adding minimal annotation support and a simple example 2023-07-19 16:56:01 +02:00
LICENSE.MIT 🔖 set version to 3.10.5 2022-01-02 22:35:35 +01:00
Makefile Add missing files to release artifacts (#3728) 2022-09-25 12:00:44 +02:00
meson.build 🔖 set version to 3.11.2 2022-08-12 15:04:06 +02:00
nlohmann_json.natvis Fix natvis XML (#3863) 2022-12-09 16:34:34 +01:00
README.md Modify readme 2023-08-04 23:56:14 +02:00
WORKSPACE.bazel Add Bazel build support (#3709) 2022-09-18 08:15:12 +02:00
wsjcpp.yml 🔖 set version to 3.11.2 2022-08-12 15:04:06 +02:00

Purpose

Suppose that your application has a complex configuration and:

  • that configuration is represented as a set of JSON files
  • the files are supposed to be edited by the end-users
  • the application on its own provides/dumps only a "default" set of files somehow annotating the dumped JSON makes much more sense than maintaining a separate documentation for the allowed values and expected keys, which will get out of date on the nearest occasion.

As per the original JSON's RFC comments are not allowed in JSON. But, knowing that the world is not ideal, nlohmann's JSON library does support parsing JSON files with so-called "comments", encoded as follows:

{
  /* this is a comment */
  "property_1": 5
}

A specific overload of the parse method has to be called, as per this part of the reference:

json j = json::parse(s,
                     /* callback */ nullptr,
                     /* allow exceptions */ true,
                     /* ignore_comments */ true);

(so, more specifically, the library allows ignoring them)

However, the library does not allow for annotating (adding the comments) when serializing your custom classes or structures. Best you could possibly do is post-process the dumped string or file through raw string processing, which is error-prone and problematic.

This fork of the nlohmann's JSON library adds a possibility to add annotations (known at compile-time) to your classes and structures. I'll try to keep the fork up to date with the original repository, potentially "shadowing" the releases in the future as well1.

How to use

Currently, the extension applies exclusively2 to NLOHMANN_DEFINE_TYPE_INTRUSIVE macro:

class ExampleClass {
private:
    int property1{1};
    double property2{2.5};
    std::string property3{"test"};
    std::map<std::string, int> property4{{"x", 1}, {"y", 2}};
    std::vector<double> property5{1.5, 5.4, 3.2};
public:
    ExampleClass() = default;

    NLOHMANN_DEFINE_TYPE_INTRUSIVE_ANNOTATED(ExampleClass, property1, "comment两两", 
                                                           property2, "multiline\ncomment2", 
                                                           property3, "comment3",
                                                           property4, "comment4",
                                                           property5, "comment5");
};

Dumping is as simple as that:

int main() {
    ExampleClass ec;
    std::ofstream example_file;
    example_file.open("example_1.json");

    nlohmann::json j = ec;
    example_file << j.dump_annotated<ExampleClass>(4) << std::endl;
    // instead of the original:
    // example_file << j.dump(4) << std::endl;

    return 0;
}

  1. Tried creating a pull request in the original repository, but I doubt it's ever going to be considered. ↩︎

  2. If you look closely at the new macro, it's relatively easy to figure out how to actually avoid using the macro, but I need to document that to make it officially supported. ↩︎