jansson icon indicating copy to clipboard operation
jansson copied to clipboard

[feature request] Function printing the error location in source json would be beneficial

Open yurivict opened this issue 12 years ago • 6 comments

Here is an example what it might print:

                                 {["a","b","c",]}
 (expecting array element) --------------------^

This would be very convenient for quick identification of problems in jsons.

yurivict avatar Jan 08 '14 12:01 yurivict

True, but this would require caching the original JSON data in the error struct, which is not practical at least for json_load_callback() and json_loadf() if the input file is not seekable.

It would also be very hard to get the caret to point to the correct spot when complex unicode characters are included in the input JSON.

akheron avatar Jan 08 '14 12:01 akheron

Re: [jansson] [feature request] Function printing

the erro Perhaps this could be done as a Jansson helper function, which the caller hands the source JSON that caused the error, the previously reported error struct, and a file descriptor for the output. There might also have to be arguments constraining the output (e.g. how much context is to be shown), since JSON strings can be very long.

Or, this could be handled by the caller, leaving Jansson out of the loop.

Dan

At 4:11 AM -0800 1/8/14, Petri Lehtinen wrote: True, but this would require caching the original JSON data in the error struct, which is not practical at least for json_load_callback() and json_loadf() if the input file is not seekable. It would also be very hard to get the caret to point to the correct spot when complex unicode characters are

included in the input JSON.

Reply to this email directly or view it on GitHub.

Daniel T. Griscom             [email protected] Suitable Systems              http://www.suitable.com/ 1 Centre Street, Suite 204    (781) 665-0053 Wakefield, MA  01880-2400

dtgriscom avatar Jan 08 '14 12:01 dtgriscom

A helper function sounds good to me. It could also optionally return the data as a heap-allocated string.

The unicode formatting problem remains, though.

akheron avatar Jan 08 '14 12:01 akheron

I added a proof of concept. The errors are not as specific as described in this ticket, but that's inherent from how Jansson parses I guess. Anyway, it simply uses the contents of json_error_t

JoakimSoderberg avatar Jan 14 '15 22:01 JoakimSoderberg

Oh and btw, does error.position refer to the bytes or character points for UTF-8?

JoakimSoderberg avatar Jan 14 '15 22:01 JoakimSoderberg

error.position is just plain bytes.

akheron avatar Jan 15 '15 05:01 akheron