cpp-json
cpp-json copied to clipboard
string_view and constexpr-able objects
Any chance of string_view
support?
And, though it would be a ton of work on your part, I'd love to be able to (somehow) get a constexpr std::string_view
out of a json::object
. I prefer using json::object
over plain string literals but std::string
does malloc
after (I think) 15 bytes on MSVC so there's occasionally a lot of unnecessary overhead.
For example, I'm communicating with a server with JSON and I have something like this:
std::string json_get_thing(std::string const& guid,
std::string const& data,
int const an_int) {
json::object to_send = {
{ "command", "PUT" },
{ "guid", guid },
{ "data", data },
{ "an_int", an_int },
};
return json::stringify(to_send);
}
At least some of the json::value
s (e.g., command
) are just string literals and, at times, all three function params are known at compile time. I'd like to be able to do this:
constexpr std::string_view json_get_thing(std::string_view guid,
std::string_view data,
int const an_int) {
return json::string_viewify( {
{ "command", "PUT" },
{ "guid", guid },
{ "data", data },
{ "an_int", an_int },
});
}
Agreed that this would be awesome. It would be a lot of work and also would need to be wrapped in conditional logic since string_view
is part of c++17 and constexpr
is very restricted until c++14. I like the idea though, so I may work it in there eventually.