1# Parsing and exceptions 2 3When the input is not valid JSON, an exception of type [`parse_error`](../../home/exceptions.md#parse-errors) is thrown. This exception contains the position in the input where the error occurred, together with a diagnostic message and the last read input token. The exceptions page contains a [list of examples for parse error exceptions](../../home/exceptions.md#parse-errors). In case you process untrusted input, always enclose your code with a `#!cpp try`/`#!cpp catch` block, like 4 5```cpp 6json j; 7try 8{ 9 j = json::parse(my_input); 10} 11catch (json::exception::parse_error& ex) 12{ 13 std::cerr << "parse error at byte " << ex.byte << std::endl; 14} 15``` 16 17In case exceptions are undesired or not supported by the environment, there are different ways to proceed: 18 19 20## Switch off exceptions 21 22The `parse()` function accepts as last parameter a `#!cpp bool` variable `allow_exceptions` which controls whether an exception is thrown when a parse error occurs (`#!cpp true`, default) or whether a discarded value should be returned (`#!cpp false`). 23 24```cpp 25json j = json::parse(my_input, nullptr, false); 26if (j.is_discarded()) 27{ 28 std::cerr << "parse error" << std::endl; 29} 30``` 31 32Note there is no diagnostic information available in this scenario. 33 34## Use accept() function 35 36Alternatively, function `accept()` can be used which does not return a `json` value, but a `#!cpp bool` indicating whether the input is valid JSON. 37 38```cpp 39if (!json::accept(my_input)) 40{ 41 std::cerr << "parse error" << std::endl; 42} 43``` 44 45Again, there is no diagnostic information available. 46 47 48## User-defined SAX interface 49 50Finally, you can implement the [SAX interface](sax_interface.md) and decide what should happen in case of a parse error. 51 52This function has the following interface: 53 54```cpp 55bool parse_error(std::size_t position, 56 const std::string& last_token, 57 const json::exception& ex); 58``` 59 60The return value indicates whether the parsing should continue, so the function should usually return `#!cpp false`. 61 62??? example 63 64 ```cpp 65 #include <iostream> 66 #include "json.hpp" 67 68 using json = nlohmann::json; 69 70 class sax_no_exception : public nlohmann::detail::json_sax_dom_parser<json> 71 { 72 public: 73 sax_no_exception(json& j) 74 : nlohmann::detail::json_sax_dom_parser<json>(j, false) 75 {} 76 77 bool parse_error(std::size_t position, 78 const std::string& last_token, 79 const json::exception& ex) 80 { 81 std::cerr << "parse error at input byte " << position << "\n" 82 << ex.what() << "\n" 83 << "last read: \"" << last_token << "\"" 84 << std::endl; 85 return false; 86 } 87 }; 88 89 int main() 90 { 91 std::string myinput = "[1,2,3,]"; 92 93 json result; 94 sax_no_exception sax(result); 95 96 bool parse_result = json::sax_parse(myinput, &sax); 97 if (!parse_result) 98 { 99 std::cerr << "parsing unsuccessful!" << std::endl; 100 } 101 102 std::cout << "parsed value: " << result << std::endl; 103 } 104 ``` 105 106 Output: 107 108 ``` 109 parse error at input byte 8 110 [json.exception.parse_error.101] parse error at line 1, column 8: syntax error while parsing value - unexpected ']'; expected '[', '{', or a literal 111 last read: "3,]" 112 parsing unsuccessful! 113 parsed value: [1,2,3] 114 ``` 115