nlohmann-json

Which exception should be caught for nlohmann::json::to_cbor?


I'm using to_cbor. I can't find any hint on what exceptions to catch in its documentation. I compared it with the documentation for from_cbor where you can find some exceptions that can occur.

I guess when using to_cbor there still can be problems when for example the JSON is not valid.

I never really had an exception happening for to_cbor but would want to be on the safe side.

Would appreciate some help on that. Thanks!


Solution

  • For an invalid JSON, it does throw a parse_error exception so you might want to handle that.

    See this example with invalid JSON here throwing parse_error exception. You might want to take a look at the implementation of to_cbor here.

    Also, you might want to create an issue on the repo for its inclusion in the docs (if there isn't one already).


    Regarding

    I never really had an exception happening for to_cbor but would want to be on the safe side.

    That might be because you never had an invalid JSON i.e. you most probably be validating it before the flow reaches to_cbor.