nlohmann / json

JSON for Modern C++
https://json.nlohmann.me
MIT License
41.46k stars 6.59k forks source link

Add minimal annotation support #4082

Closed marekpiotrowski closed 9 months ago

marekpiotrowski commented 11 months ago

Hey @nlohmann !

Hope you're having a great day!

After reading the guidelines and your discussion around comments in JSON, I got a feeling that this PR has no chance at all :D So, I was about to start writing documentation and considering I'd like to avoid redundant work, I though it'd make sense to check with you first.

Long story short, it's about annotating custom classes (annotations known at compile time) and dumping those when serializing (please see the tests or the example):

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");
};

would produce:

{
    /* comment两两 */
    "property1": 1,
    /* multiline */
    /* comment2 */
    "property2": 2.5,
    /* comment3 */
    "property3": "test",
    /* comment4 */
    "property4": {
        "x": 1,
        "y": 2
    },
    /* comment5 */
    "property5": [
        1.5,
        5.4,
        3.2
    ]
}

If you'd ever consider merging this PR and allowing attachment of annotations known at compile time, I'll make sure that the documentation is consistent, code is amalgamated, will check the coverage, make sure redundant files are cleaned up etc. as per the guidelines. However, if you think it's a bad idea to introduce such functionality, I'm happy to keep this as a fork. I'll occasionally merge to your latest changes I guess.

The reason is that we've got plenty of JSON configuration files which are supposed to be filled by non-technical people. We use your brilliant plugin cus it's very robust and allows for ignoring comments when parsing. We've been annotating the dumped JSONs with some hard-core text manipulations in "post-processing", but it's horrible. Maintaining such documentation outside of the code is pretty much impossible so we had no choice though.

But now I thought that I could slightly extend your serializer and convenient macros for annotating the properties of custom classes.

Please let me know if there's any chance we could merge such functionality. If that's not the case, I'll have to prepare the documentation differently ;)

Cheers, Marek

github-actions[bot] commented 11 months ago

🔴 Amalgamation check failed! 🔴

The source code has not been amalgamated. @marekpiotrowski Please read and follow the Contribution Guidelines.

coveralls commented 11 months ago

Coverage Status

coverage: 98.848% (-1.2%) from 100.0% when pulling bff36b4ea4917b15d8f4363164886965a967a934 on marekpiotrowski:add-minimal-annotation-support into 5d2754306d67d1e654a1a34e1d2e74439a9d53b3 on nlohmann:develop.

github-actions[bot] commented 9 months ago

🔴 Amalgamation check failed! 🔴

The source code has not been amalgamated. @marekpiotrowski Please read and follow the Contribution Guidelines.