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

Is it possible to add support for custom types? #175

Open
CJCombrink opened this issue Oct 20, 2022 · 5 comments
Open

Is it possible to add support for custom types? #175

CJCombrink opened this issue Oct 20, 2022 · 5 comments
Assignees
Labels
feature New feature or enhancement request

Comments

@CJCombrink
Copy link

Is your feature request related to a problem? Please describe.

Is it possible to add TOML support for custom types, example:

struct Point {
    int32_t x = 10;
    int32_t y = 20;
}

Which would probably look like the following in TOML

[custom_point]
x = 10
y = 20

Describe the solution you'd like

I would like to use it as follow:

const Point my_point = tbl["custom_point"].value<Point>();

Additional context

I am not yet using the library but working thought the API docs I don't see any mention of such options.

@CJCombrink CJCombrink added the feature New feature or enhancement request label Oct 20, 2022
@marzer
Copy link
Owner

marzer commented Oct 20, 2022

Nope, there's no way to do that currently. It has been vaguely on my to-do list for a while, but I'm unlikely to get time to add the plumbing for this any time soon. I believe toml11 has something for that though; if you're deserializing a lot of different struct types you may find that library a bit more flexible in this area.

@CJCombrink
Copy link
Author

Thanks for the pointer. I have looked at toml11.
But, I like the API of this library a lot more.
I can probably get around this "limitation" for the sake of the cleaner API and then hope in future I can throw away boilerplate code when this gets added.

I am used to JSON for Modern C++ and this interface is quite familiar.
Perhaps there are some lessens to be learned from their support for Custom data sources in my code or this library.
(I am guessing you are familiar with this nlohmann-json)

@marzer
Copy link
Owner

marzer commented Oct 20, 2022

(I am guessing you are familiar with this nlohmann-json)

Yup, the way that library handles this particular bit of functionality is very nice. One thing to be mindful of though is that TOML is chiefly a config language, whereas JSON is also aimed at serialization, so being able to convert between TOML and structs of arbitrary complexity isn't stricly a design goal I'm ever going to work towards.

Having said that, I would still like to make it easier for obvious common cases like points, vectors et cetera - I've had to come up with my own workarounds for that in my own code that uses this library 😅

@bobfang1992
Copy link

I wonder how this is possible without proper reflection support in C++.

@marzer
Copy link
Owner

marzer commented Mar 21, 2023

It's not in the general case; it always boils down to manually-written operator overloads or ADL tricks. You can use template machinery (e.g. SFINAE, partial specialisation) to cover some predefined cases pretty easily, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or enhancement request
Projects
None yet
Development

No branches or pull requests

3 participants