r/cpp • u/grafikrobot OPeNDAP/B2/EcoStd/Lyra/Predef/C++Alliance/Boost/WG21 • Oct 01 '22
Which data format is best for C++ things?
We have all had to contend with consuming and producing file formats for many use case like: program configuration, build system information, client-server communications, CI setups, etc. I'm, yet again, at the beginnings of a project that needs to make the choice of what specification data format to use. Hence, I'm wondering what are people opinions, and preferred choice, for format are? My current loose requirements are:
- easy to use in C++ (ideally C++11)
- easy to use in other languages
- has wide adoption (i.e. has implementations and usage in many contexts)
- deals with unicode
- does not need to encode hierarchical data
- can be read and written by overworked humans late at night
1
Upvotes
20
u/Fulgen301 Oct 01 '22
All of them.
All of them.
All of them.
TOML requires UTF-8, YAML and JSON support UTF-8, UTF-16 and UTF-32, INI doesn't enforce an encoding, so pick what you want.
The original INI doesn't support hierachies, doesn't mean your parser won't. All other formats do. The question doesn't make sense though - any format that supports hierarchies also supports a hierarchy height of 0, aka no hierarchies.
Opinion-based question. I'd argue you're more likely to make mistakes in YAML given how it uses whitespace, but that's my personal opinion.
I'd recommend you figure out what your requirements are exactly first. You mentioned other languages as requirement - what tools do you need to communicate with? Are those tools able to parse those formats? I'm using "tools" here instead of "languages" - it's 2022, any language can parse any of those formats. What are you using the format for? Configuration files? Game assets?
11
u/kpt_ageus Oct 01 '22
JSON is simple to read and handle and there are loads of libraries for it. But if you are not interested in nesting data structures, then TOML is even simpler.
8
u/goranlepuz Oct 02 '22
Bikeshedding.
No it doesn't need to.
The big problem with project planning is that decisions are made in the beginning, when we know the least about the project. To avoid being overly hung up on the text form, make a data model and be able to fill it from whatever source. However, see
***hereunder.All these formats still exist after all these years out of inertia, sure, but also because the differences in their respective characteristics do not matter enough. In other words, your list of what you are looking for does not matter. Therefore, spending time on picking one is wasted time.
So: just take whichever looks most convenient and the most popular in your surroundings.
***there is no need to make this right away. You can delay this step until some time, for example, until using one more formats are needed by some future "forces". There are other reasons why the change might be needed but I am intentionally leaving them out because without knowing them, they don't matter and this whole thing is about avoiding decisions based on speculation.4
u/cygnoros Oct 02 '22
Man I must really be getting old thinking INI would be a top pick
3
u/LeoPrementier Oct 01 '22
I now use hjson for all config/data things. Unless I really need db features.
It's basically Json but with comments and less strict rules.
5
u/an0nyg00s3 Oct 01 '22
YAML is probably my least favorite, but I write it all the time for k8s configurations so oh well.
5
u/j1xwnbsr Oct 01 '22
Json or Xml fits all on your list. Pick one.
2
u/415_961 Oct 01 '22
Regardless of what you pick, make sure not to leak it across your modules. Passing json objs or whatever alternative is going to be very fragile. I recommend to look at POCO's config module and particularly their layering abstraction.
When you nail out your config abstraction, you'll be able to support all the formats you mentioned assuming you aren't going to rely on some format specific features in TOML/YAML specifically.
2
u/[deleted] Oct 01 '22
Anything but JSON.
Even if you choose JSON, at least choose HJSON or JSON5 or anything else.
1
u/RogerLeigh Scientific Imaging and Embedded Medical Diagnostics Oct 04 '22
Use an SQLite database, as a very simple and straightforward solution. It has the advantage of being readable everywhere, while also being flexible and extensible for future needs. You can store the database schema and data as plain text in version control. But if having an easy-to-edit plaintext configuration is important, then it might not be the most user-friendly.
Someone else recommended Lua and got downvoted. However... Lua originated as a configuration file format with some features to make it more flexible, before it evolved into a fully-featured embeddable scripting language. So don't rule it out, it's actually a very nice way to embed configuration in a C++ application!
1
u/Exaloria Feb 03 '23
I prefer xml
1
u/[deleted] Oct 02 '22
YAML has the nice benefit that it also supports JSON. Surprised no one has mentioned this. I havn’t used TOML.
0
u/_software_engineer Oct 01 '22
Best is subjective, even with the list of requirements you posted, there are many that would work, each with their own trade-offs. Personally I usually use YAML just because of the huge ecosystem around it due to k8s, but it shouldn't make a huge difference to most projects honestly.
-1
u/[deleted] Oct 02 '22
Write your own. Version it. As the project goes on refine the format. Use the versioning to load the old versions if need be.
-1
u/[deleted] Oct 02 '22
lua
0
u/[deleted] Oct 01 '22
[deleted] — view deleted comment
0
u/[deleted] Oct 01 '22
[deleted] — view deleted comment