You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the proposed feature
It would be nice to have basic_json::as_or, which would be like get_value_or for as instead of at (i.e. converting the current basic_json value to the specified type if it matches, or some default value otherwise). This could be useful with at_or_null, which would allow the user to perform their own type checking and handling.
What other libraries (C++ or other) have this feature?
None that I'm aware of.
Include a code fragment with sample data that illustrates the use of this feature
Example 1:
The following code will not throw if the type is incorrect, which may be beneficial in some cases.
(note: this is modified from the example for get_value_or)
(also it seems like the example there is missing some usings :)
#include<jsoncons/json.hpp>using jsoncons::json;
using jsoncons::json_object_arg;
using jsoncons::json_type;
intparse_category_string(std::string_view sv)
{
return0; // placeholder
}
intget_category_id(json j)
{
json category = j.at_or_null("category");
if (category.is_string())
{
returnparse_category_string(category.as<std::string>());
}
return category.as_or<int>(100);
}
intmain()
{
json j1(json_object_arg, {{"author","Evelyn Waugh"},{"title","Sword of Honour"}});
json j2(json_object_arg, {{"author", "J. R. R. Tolkien"}, {"title", "The Lord of the Rings"}, {"category", "fantasy"}});
json j3(json_object_arg, {{"author", "Robert Louis Stevenson"}, {"title", "Strange Case of Dr Jekyll and Mr Hyde"}, {"category", 123}});
std::cout << get_category_id(j1) << '\n';
std::cout << get_category_id(j2) << '\n';
std::cout << get_category_id(j3) << '\n';
}
Output:
100
0
123
Example 2:
This could also be used to massively simplify cases where invalid values are treated as a default value. In this example, if key is not found it will also use the default value (although this can be changed by using at instead of at_or_null). Provided j is a valid json object, this code will not throw.
Describe the proposed feature
It would be nice to have
basic_json::as_or
, which would be likeget_value_or
foras
instead ofat
(i.e. converting the current basic_json value to the specified type if it matches, or some default value otherwise). This could be useful withat_or_null
, which would allow the user to perform their own type checking and handling.What other libraries (C++ or other) have this feature?
None that I'm aware of.
Include a code fragment with sample data that illustrates the use of this feature
Example 1:
The following code will not throw if the type is incorrect, which may be beneficial in some cases.
(note: this is modified from the example for get_value_or)
(also it seems like the example there is missing some
using
s :)Output:
Example 2:
This could also be used to massively simplify cases where invalid values are treated as a default value. In this example, if
key
is not found it will also use the default value (although this can be changed by usingat
instead ofat_or_null
). Providedj
is a valid json object, this code will not throw.The text was updated successfully, but these errors were encountered: