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

Add a convenience member function to call value_to #983

Open
kiwixz opened this issue Feb 13, 2024 · 5 comments
Open

Add a convenience member function to call value_to #983

kiwixz opened this issue Feb 13, 2024 · 5 comments

Comments

@kiwixz
Copy link

kiwixz commented Feb 13, 2024

Hi, first of all thanks for the great library fast yet easy to use.

I'm currently writing tag_invoke functions to convert JSON values from/to a class, and I have something like this:

MyClass{json.at("superint1").to_number<int>(),
        json.at("superint2").to_number<int>(),
        json.at("superbool").as_bool(),
        json.at("superstring").as_string(),
        boost::json::value_to<std::vector<int>>(json.at("supervectorofint"))}

It's a class with private values and a constructor, so I cant just rely on Boost Describe for this.

If there was a to<T>() member function (just calling boost::json::value_to under the hood), I could have arguably more consistent code:

MyClass{json.at("superint1").to_number<int>(),
        json.at("superint2").to_number<int>(),
        json.at("superbool").as_bool(),
        json.at("superstring").as_string(),
        json.at("supervectorofint").to<std::vector<int>>()}

And it could also allow something ever better (to my eyes that is!):

MyClass{json.at("superint1").to<int>(),
        json.at("superint2").to<int>(),
        json.at("superbool").to<bool>(),
        json.at("superstring").to<std::string>(),
        json.at("supervectorofint").to<std::vector<int>>()}

I like the fact that there is a fixed set of as_* functions, guaranteeing direct access to the underlying value; but value_to seems to be the "do whatever you need but get me this" convenience method I usually want.

Related to #418.

@pdimov
Copy link
Member

pdimov commented Feb 13, 2024

You can still use Describe if you declare an intermediate struct holding the parameters.

@pdimov
Copy link
Member

pdimov commented Feb 17, 2024

On second thought, isn't

MyClass{json.at("superint1").to<int>(),
        json.at("superint2").to<int>(),
        json.at("superbool").to<bool>(),
        json.at("superstring").to<std::string>(),
        json.at("supervectorofint").to<std::vector<int>>()}

possible to write today as

MyClass{
        value_to<int>(json.at("superint1")),
        value_to<int>(json.at("superint2")),
        value_to<bool>(json.at("superbool")),
        value_to<std::string>(json.at("superstring")),
        value_to<std::vector<int>>(json.at("supervectorofint"))
}

?

@grisumbras
Copy link
Member

It is. I think, @kiwixz considers that syntax too cumbersome.

To be honest, I'm not fond of adding just another way to call value_to.

@kiwixz
Copy link
Author

kiwixz commented Feb 17, 2024

I didn't think of using namespace here which helps a lot.

It still feels weird to me to use it as a free function, especially when we have to_number as a member.

@grisumbras
Copy link
Member

grisumbras commented Feb 19, 2024

True, but making an equivalent of value_to member function of value is much more intrusive.

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

No branches or pull requests

3 participants