2

I have a lot of settings which are represented by key-value pair. Key is a string, value can be string or double. These settings are divided by categories with string names. The question is - I want to store settings in human-readable format, the file should be easily read and changed by user. (that's why e.g. boost serialization is not so good). What libraries can you recommend?

3
  • 5
    What's wrong with key.value pairs? Or even good old .INI files? Are they not human-readable? Nov 4 '17 at 11:46
  • 1
    @Mawg: there is no "the INI file format". I know at least 3 different formats. And they cannot save multiline data. Jan 16 '18 at 0:00
  • Interesting (+1). I honestly thought that there was only one .INI file format. I will admit to having used address-line_X entries. though. Perhaps JSON, or encoding newlines (0x0D0x0A)? I prefer ..INI/JSON to XML for being slightly more human readable. Jan 16 '18 at 7:20
2

Aside from the obvious option of just using INI style files (which you can find literally dozens of parsing libraries for by just searching google, but may not be an option if you need nested tables of settings), you might consider:

  • YAML: Really easy to read as a human, and pretty trivial to parse for a computer. Allows comments (very useful if you plan on a user editing the file directly). Their site has links for parsers in at least a dozen different languages, including 3 for C++. While not as popular as the other options, it's probably the easiest to learn other than INI files, and it's also one of the hardest to mess up in terms of both parsing the file, and writing it by hand.

  • JSON: Pretty ubiquitous, but not quite as easy for 'normal' (read as non-technical) users to modify as INI files or YAML. No comment support. There exist quite literally dozens of parsers for most languages, my favorite for C++ is JSONCPP, because it's tiny and doesn't even need an external library.

  • XML: In general, I would avoid this for configuration as it's hard to work with by hand (yes, it's easy with a dedicated editor, but you often won't have that luxury), is extremely verbose compared to most other options, and takes a lot of effort to parse securely and reliably, but a lot of people still use it. Just like JSON, there are bunches of options for parsing it in most languages, though the parsing is often quite complicated compared to YAML or JSON. libxml is the one I know the most about (largely because most of the programming I do is on Linux and BSD), there's a separate project for C++ bindings called libxml++.

3
  • Good answer (+1), but, just curious ... "XML: In general, I would avoid this for configuration" ... I am a freelance embedded device programmer, so change companies every 18 months or so, and I can't remember the last time that I worked a project which did not use XML, and only XML, for configuration files. Why do you advise against them? Enquiring minds want to know :-) Jan 16 '18 at 7:24
  • 1
    @Mawg I've edited things to include a more concrete explanation of why I don't like XML for configuration files. Jan 16 '18 at 13:41
  • I appreciate the feedback (+1), as I am always looking to learn from others. As it happens, I agree with you - now, if only I could convince my bosses. Jan 16 '18 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.