-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Introduce try_get() and try_deserialize() #2288
base: develop
Are you sure you want to change the base?
Conversation
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
After thinking about this PR again, I decided that it requires some more work. Perhaps design could be better. I don't have much free time at the moment, so I'll come back later and think about this PR again. Do I need to close PR for this time or can I leave PR as a draft? |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
In addition, it would be handy to have the macros such as: NLOHMANN_DEFINE_TYPE_INTRUSIVE use a version which is ok if the data isn't found. try/catching everything can be expensive if "not found" is an acceptable state. Expanding even further to the "optional is ok", bool from_json(.......) whereby validation can be reported as a warning, or diagnostic, but not as an error that is try/catch. It means, NLOHMANN_DEFINE_TYPE_INTRUSIVE isn't as useful, or jsons become bigger with required params being null instead of leaving them out. An example additional macro: Additionally, having from_json (and thus, macro) return true/false, or count of missing params, or a tuple of found / maxcount would be helpful and allow for custom detection. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Would it make sense extending this to have an optional default value? I am in the process of porting over a project from jsoncpp to nlohmann-json, and the former has this as a very convenient feature (for us at least), i.e. Is there a best practice for this in the current library? |
The |
@nlohmann oh wow, that certainly looks like it, thank you! |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
@oficsu Any progress here? |
@gregmarr, no, I couldn't come up with a better design |
I propose new method
json.try_get<MyType>()
, which works similar tojson.get<MyType>()
. It can be extended by providingadl_serializer<MyType>::try_deserialize()
function. Unlikeget
method, the return type oftry_get
is assumed to have a different return type toMyType
and can bestd::optional
,tl::expected
or other wrapperWhy is it needed?
Why not just allow
json.get<MyType>()
to return any type, different fromMyType
?json.get<MyType>()
andjson.try_get<MyType>()
, or even have bothWhat about
json.get<MyMonad<MyType>>()
? Why not?try_get()
with custom return type once and don't bother about return type anymore every timeget()
is called. This is quite important in a large codebase whereget()
calls many times for same typejson.try_get<MyType>()
is easier to read and is a good abstraction compared tojson.get<MyMonad<MyType>>()
json.try_get<T>()
is the better way to return optional thanjson.get<std::optional<T>>()
from Fix conversion from empty json to std::optional<> #2229So, is noexcept our goal? Does
json.try_get<X>()
throw exceptions?try_deserialize()
and it is the responsibility of the users to marktry_deserialize()
asnoexcept(true)
ornoexcept(false)
.Should we implement
try_deserialize()
for basic types?tl::expected
orstd::optional
could be suitable for this, but most likely, the user will want to write their own monadic type or wrapper.Also, there may be a problem when users include two libraries that use nlohman_json and specializing different
adl_serializer
for the same basic type. Maybe we can implementtry_deserialize
lookup through tag dispatching with adl and allow additional arguments to be passed totry_get()
, then users can create their own unambiguous overloads. More research is needed here and right now I think the answer is noAbout naming
try_deserialize()
is too contrast tofrom_json()
, but I don't know what name is better