Add a shorthand to masquerade a type parsing/serializing
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
- Issue type
- Feature
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- cpp
- Domain
- backend-api-design
Research direction
Start by reading the existing tag_invoke, value_to, value_from, and described-struct conversion entry points and tests. Compare the proposed JsonAs shorthand with the enum, invariant-wrapper, and string-convertible examples; done requires an agreed public API and coverage for the supported conversions.
Written by the indexing model from the issue text.
Description
Hi, there are two cases where I couldn't avoid implementing one-line tag_invokes. I'd love boost::json to do it for me!
The first one is an enum that only has ints. Its an enum class because why not but I don't care about specific values, its just here to help referencing interesting ones.
enum class Version { _202201 = 202201, _202301 = 202301, _202302 = 202302};
Version tag_invoke(boost::json::value_to_tag<Version>, const boost::json::value& json) {
return static_cast<Version>(json.as_int64());
}
void tag_invoke(boost::json::value_from_tag, boost::json::value& json, Version a) {
json = static_cast<int>(a);
}
Second case is probably more interesting, following a lot of back and forth the best way I found to handle a type with invariants is to make a described struct.
struct MyClass {
struct Params {
...
};
MyClass(Params params);
...
};
And MyClass c = value_to<MyClass::Params>() just works (with implicit conversion), but if I want to include it inside a described struct boost::json doesn't know that to decode a MyClass it needs a MyClass::Params.
I also had the same problem when I tried to parse/serialize a type implicitly convertible to and from a std::string. Even overriding is_string_like<> was not enough.
So I made a little helper and it works for me, but I think boost::json should have something similar (or better!) built-in:
template <typename>
struct JsonAs;
namespace boost::json {
template <typename T>
requires requires { typename JsonAs<T>::type; }
T tag_invoke(boost::json::value_to_tag<T>, const boost::json::value& json) {
return static_cast<T>(boost::json::value_to<typename JsonAs<T>::type>(json));
}
template <typename T>
requires requires { typename JsonAs<std::remove_cvref_t<T>>::type; }
void tag_invoke(boost::json::value_from_tag, boost::json::value& json, T&& a) {
json = boost::json::value_from(static_cast<JsonAs<std::remove_cvref_t<T>>::type>(std::forward<T>(a)));
}
}
template <>
struct JsonAs<Version> : std::type_identity<int> {};
- Dominant language
- C++
- Stars
- 478
- Forks
- 110
- Avg merge
- 8d 12h
- Merged PRs (30d)
- 6
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from boostorg/json
-
Difficulty 3/5 1-2 days Newbie friendliness 72/100
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
-
sanitizer warning Open
Difficulty 3/5 1-2 days Newbie friendliness 45/100
-
Difficulty 5/5 Over a week Newbie friendliness 20/100
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
AXERA-TECH/ax-llm#77 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
games-on-whales/wolf#509 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
bug-unconfirmed
Difficulty 2/5 1-3 hours Newbie friendliness 76/100