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 std::formatter for json::string_view and json::string #1010

Open
hj2000 opened this issue May 27, 2024 · 1 comment
Open

add std::formatter for json::string_view and json::string #1010

hj2000 opened this issue May 27, 2024 · 1 comment

Comments

@hj2000
Copy link

hj2000 commented May 27, 2024

We can print json::string and json::string_view with ostream,but we cann't use std::format to format them ,could we have std::formatterjson::string and std::formatterjson::string_view?

@grisumbras
Copy link
Member

For future reference:

std::format(fmt, args...) defers to std::vformat(fmt.get(), make_format_args(args...)).

template< class Context = std::format_context), class... Args >
format-arg-store make_format_args( Args&... args );

typename Context::template formatter_type<std::remove_cv<Argi>> should be a BasicFormatter. format_context::formatter_type<T> is std::formatter<T, char>. Curiously, BasicFormatters are only required to work with instantiations of std::basic_format_context as Context.

So, it seems like the formatter should be something like this:

namespace std
{
template<>
struct formatter<boost::json::string, char>
{
    template<class OIt>
    OIt
    format(boost::json::string const&, std::basic_format_context<OIt, char>&);
};
}

If we want to also support {fmt}, we need basically the same thing, only in a different namespace, so, better do:

namespace boost::json::detail
{
struct formatter<string>
{
    template<class Context>
    Context::iterator
    format(string const&, Context&);
};
}
namespace std { template<> struct formatter<boost::json::string, char> : boost::json::detail::formatter<boost::json::string> {}; }
namespace fmt { template<> struct formatter<boost::json::string> : boost::json::detail::formatter<boost::json::string> {}; }

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

2 participants