simdjson icon indicating copy to clipboard operation
simdjson copied to clipboard

and_then support in simdjson_result for monadic operations

Open madhur4127 opened this issue 1 year ago • 2 comments

Borrowing the idea of and_then from std::optional / std::expected, error handling for parsing multiple fields can be easy as it can be chained.

simdjson::ondemand::object o; o[key1].and_then(...).and_then(...);

Instead of checking for error twice in the case above.

madhur4127 avatar Apr 04 '24 09:04 madhur4127

We already support chaining in our main API (on demand). Screenshot from our documentation:

Screenshot 2024-04-04 at 11 36 48 AM

We do not support chaining arbitrary lambda expressions, although it could be added with ease if there is a use case for it.

lemire avatar Apr 04 '24 15:04 lemire

I think that chaining refers wrt to nesting of the fields but another way could be to parse fields at the same level of nesting.

I maybe wrong but there's no simple exception free way to check for error without checking each field.

{"a":1, "b":2}

If I want to parse a and then b, I have to check for error twice in each step. With and_then, it's easier as you can focus on error free path and handle errors at last.

One way is to have a variable for each key and then use get() in succession but feels terse than just using and_then

madhur4127 avatar Apr 04 '24 21:04 madhur4127