Could Not Read Json: Invalid Utf-8 Start Byte 0xbf
Exceptions¶
Overview¶
Base blazon¶
All exceptions inherit from form json::exception (which in turn inherits from std::exception). It is used as the base course for all exceptions thrown past the basic_json class. This class tin can hence be used as "wildcard" to take hold of exceptions.
Switch off exceptions¶
Exceptions are used widely within the library. They can, however, be switched off with either using the compiler flag -fno-exceptions or by defining the symbol JSON_NOEXCEPTION. In this case, exceptions are replaced past arrest() calls. You tin can further command this beliefs by defining JSON_THROW_USER (overriding throw ), JSON_TRY_USER (overriding try ), and JSON_CATCH_USER (overriding grab ).
Note that JSON_THROW_USER should get out the electric current scope (eastward.g., by throwing or aborting), as continuing after it may yield undefined behavior.
Example
The lawmaking below switches off exceptions and creates a log entry with a detailed error message in case of errors.
#include <iostream> #define JSON_TRY_USER if(true) #define JSON_CATCH_USER(exception) if(false) #define JSON_THROW_USER(exception) \ {std::clog << "Error in " << __FILE__ << ":" << __LINE__ \ << " (function " << __FUNCTION__ << ") - " \ << (exception).what() << std::endl; \ std::abort();} #include <nlohmann/json.hpp> Note the explanatory what() string of exceptions is not bachelor for MSVC if exceptions are disabled, see #2824.
Extended diagnostic letters¶
Exceptions in the library are thrown in the local context of the JSON value they are detected. This makes detailed diagnostics messages, and hence debugging, difficult.
Case
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann :: json ; int main () { json j ; j [ "accost" ][ "street" ] = "Imitation Street" ; j [ "address" ][ "housenumber" ] = "12" ; try { int housenumber = j [ "address" ][ "housenumber" ]; } grab ( json :: exception & due east ) { std :: cout << e . what () << '\n' ; } } Output:
[json.exception.type_error.302] blazon must exist number, just is string This exception tin be difficult to debug if storing the value "12" and accessing it is further apart.
To create better diagnostics messages, each JSON value needs a pointer to its parent value such that a global context (i.e., a path from the root value to the value that atomic number 82 to the exception) can be created. That global context is provided as JSON Arrow.
Every bit this global context comes at the price of storing one additional pointer per JSON value and runtime overhead to maintain the parent relation, extended diagnostics are disabled past default. They can, however, be enabled past defining the preprocessor symbol JSON_DIAGNOSTICS to 1 before including json.hpp.
Example
#include <iostream> # define JSON_DIAGNOSTICS i #include <nlohmann/json.hpp> using json = nlohmann :: json ; int main () { json j ; j [ "address" ][ "street" ] = "Fake Street" ; j [ "accost" ][ "housenumber" ] = "12" ; attempt { int housenumber = j [ "address" ][ "housenumber" ]; } take hold of ( json :: exception & eastward ) { std :: cout << east . what () << '\n' ; } } Output:
[json.exception.type_error.302] (/accost/housenumber) blazon must be number, just is cord Now the exception message contains a JSON Pointer /accost/housenumber that indicates which value has the wrong blazon.
Parse errors¶
This exception is thrown past the library when a parse mistake occurs. Parse errors can occur during the deserialization of JSON text, CBOR, MessagePack, also every bit when using JSON Patch.
Exceptions have ids 1xx.
Byte index
Member byte holds the byte index of the concluding read character in the input file.
For an input with n bytes, i is the index of the first character and n+1 is the index of the terminating null byte or the end of file. This also holds true when reading a byte vector (CBOR or MessagePack).
Instance
The following lawmaking shows how a parse_error exception can exist caught.
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann :: json ; int main () { try { // parsing input with a syntax mistake json :: parse ( "[one,2,3,]" ); } grab ( json :: parse_error & e ) { // output exception information std :: cout << "message: " << e . what () << '\north' << "exception id: " << e . id << '\due north' << "byte position of error: " << e . byte << std :: endl ; } } Output:
message: [json.exception.parse_error.101] parse error at line ane, column eight: syntax error while parsing value - unexpected ']'; expected '[', '{', or a literal exception id: 101 byte position of fault: 8 json.exception.parse_error.101¶
This error indicates a syntax mistake while deserializing a JSON text. The fault message describes that an unexpected token (character) was encountered, and the fellow member byte indicates the error position.
Example message
Input concluded prematurely:
[json.exception.parse_error.101] parse error at 2: unexpected end of input; expected cord literal No input:
[json.exception.parse_error.101] parse fault at line 1, cavalcade 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal Control character was not escaped:
[json.exception.parse_error.101] parse error at line 1, column 2: syntax fault while parsing value - invalid string: control character U+0009 (HT) must be escaped to \u0009 or \\; last read: '"<U+0009>'" String was not closed:
[json.exception.parse_error.101] parse error at line 1, column 2: syntax fault while parsing value - invalid string: missing endmost quote; last read: '"' Invalid number format:
[json.exception.parse_error.101] parse fault at line 1, column 3: syntax error while parsing value - invalid number; expected '+', '-', or digit after exponent; terminal read: '1E' \u was not exist followed by 4 hex digits:
[json.exception.parse_error.101] parse error at line 1, column 6: syntax error while parsing value - invalid string: '\u' must be followed by 4 hex digits; last read: '"\u01"' Invalid UTF-8 surrogate pair:
[json.exception.parse_error.101] parse error at line 1, column 13: syntax error while parsing value - invalid string: surrogate U+DC00..U+DFFF must follow U+D800..U+DBFF; concluding read: '"\uD7FF\uDC00'" Invalid UTF-viii byte:
[json.exception.parse_error.101] parse error at line 3, column 24: syntax fault while parsing value - invalid string: ill-formed UTF-eight byte; final read: '"vous \352t' Tip
- Make sure the input is correctly read. Try to write the input to standard output to cheque if, for example, the input file was successfully opened.
- Paste the input to a JSON validator like http://jsonlint.com or a tool like jq.
json.exception.parse_error.102¶
JSON uses the \uxxxx format to describe Unicode characters. Lawmaking points above 0xFFFF are split into ii \uxxxx entries ("surrogate pairs"). This mistake indicates that the surrogate pair is incomplete or contains an invalid code point.
Case bulletin
parse error at 14: missing or wrong depression surrogate Note
This exception is not used any more. Instead json.exception.parse_error.101 with a more detailed description is used.
json.exception.parse_error.103¶
Unicode supports code points upwardly to 0x10FFFF. Code points above 0x10FFFF are invalid.
Example message
parse error: lawmaking points above 0x10FFFF are invalid Note
This exception is not used any more than. Instead json.exception.parse_error.101 with a more than detailed clarification is used.
json.exception.parse_error.104¶
RFC 6902 requires a JSON Patch document to be a JSON document that represents an assortment of objects.
Case message
[json.exception.parse_error.104] parse error: JSON patch must be an array of objects json.exception.parse_error.105¶
An operation of a JSON Patch document must incorporate exactly ane "op" member, whose value indicates the operation to perform. Its value must be 1 of "add", "remove", "replace", "motility", "re-create", or "exam"; other values are errors.
Example bulletin
[json.exception.parse_error.105] parse error: operation 'add' must take member 'value' [json.exception.parse_error.105] parse mistake: operation 'copy' must take string fellow member 'from' [json.exception.parse_error.105] parse error: operation value 'foo' is invalid json.exception.parse_error.106¶
An array alphabetize in a JSON Pointer (RFC 6901) may exist 0 or any number without a leading 0.
Example message
[json.exception.parse_error.106] parse error: array index '01' must not begin with '0' json.exception.parse_error.107¶
A JSON Arrow must be a Unicode cord containing a sequence of cypher or more reference tokens, each prefixed by a / character.
Example message
[json.exception.parse_error.107] parse error at byte one: JSON pointer must exist empty or begin with '/' - was: 'foo' json.exception.parse_error.108¶
In a JSON Pointer, only ~0 and ~1 are valid escape sequences.
Example message
[json.exception.parse_error.108] parse error: escape character '~' must be followed with '0' or '1' json.exception.parse_error.109¶
A JSON Pointer assortment index must be a number.
Instance bulletin
[json.exception.parse_error.109] parse error: array index 'one' is not a number [json.exception.parse_error.109] parse fault: array alphabetize '+1' is not a number json.exception.parse_error.110¶
When parsing CBOR or MessagePack, the byte vector ends before the consummate value has been read.
Example message
[json.exception.parse_error.110] parse fault at byte v: syntax mistake while parsing CBOR string: unexpected end of input [json.exception.parse_error.110] parse error at byte two: syntax error while parsing UBJSON value: expected cease of input; final byte: 0x5A json.exception.parse_error.112¶
Not all types of CBOR or MessagePack are supported. This exception occurs if an unsupported byte was read.
Example bulletin
[json.exception.parse_error.112] parse mistake at byte 1: syntax error while parsing CBOR value: invalid byte: 0x1C json.exception.parse_error.113¶
While parsing a map key, a value that is not a string has been read.
Example message
[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing CBOR string: expected length specification (0x60-0x7B) or indefinite cord type (0x7F); last byte: 0xFF [json.exception.parse_error.113] parse error at byte 2: syntax error while parsing MessagePack string: expected length specification (0xA0-0xBF, 0xD9-0xDB); terminal byte: 0xFF [json.exception.parse_error.113] parse mistake at byte 2: syntax error while parsing UBJSON char: byte after 'C' must be in range 0x00..0x7F; last byte: 0x82 json.exception.parse_error.114¶
The parsing of the corresponding BSON record blazon is not implemented (yet).
Example bulletin
[json.exception.parse_error.114] parse mistake at byte v: Unsupported BSON record blazon 0xFF json.exception.parse_error.115¶
A UBJSON loftier-precision number could not exist parsed.
Example bulletin
[json.exception.parse_error.115] parse fault at byte 5: syntax error while parsing UBJSON loftier-precision number: invalid number text: 1A Iterator errors¶
This exception is thrown if iterators passed to a library function do not match the expected semantics.
Exceptions have ids 2xx.
Case
The following code shows how an invalid_iterator exception can exist caught.
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann :: json ; int principal () { effort { // calling iterator::key() on not-object iterator json j = "string" ; json :: iterator it = j . brainstorm (); auto 1000 = it . primal (); } catch ( json :: invalid_iterator & e ) { // output exception information std :: cout << "message: " << eastward . what () << '\n' << "exception id: " << e . id << std :: endl ; } } Output:
message: [json.exception.invalid_iterator.207] cannot utilize key() for non-object iterators exception id: 207 json.exception.invalid_iterator.201¶
The iterators passed to constructor basic_json(InputIT first, InputIT last) are not compatible, meaning they do not belong to the same container. Therefore, the range (first, terminal) is invalid.
Instance message
[json.exception.invalid_iterator.201] iterators are not compatible json.exception.invalid_iterator.202¶
In the erase or insert role, the passed iterator pos does not vest to the JSON value for which the function was chosen. It hence does not ascertain a valid position for the deletion/insertion.
Example bulletin
[json.exception.invalid_iterator.202] iterator does not fit current value [json.exception.invalid_iterator.202] iterators first and terminal must bespeak to objects json.exception.invalid_iterator.203¶
Either iterator passed to part erase(IteratorType starting time, IteratorType terminal) does not belong to the JSON value from which values shall be erased. Information technology hence does not define a valid range to delete values from.
Case bulletin
[json.exception.invalid_iterator.203] iterators do not fit current value json.exception.invalid_iterator.204¶
When an iterator range for a archaic type (number, boolean, or cord) is passed to a constructor or an erase function, this range has to be exactly (begin(), end()), because this is the but manner the single stored value is expressed. All other ranges are invalid.
Example message
[json.exception.invalid_iterator.204] iterators out of range json.exception.invalid_iterator.205¶
When an iterator for a primitive type (number, boolean, or string) is passed to an erase function, the iterator has to be the begin() iterator, because it is the only way to address the stored value. All other iterators are invalid.
Case message
[json.exception.invalid_iterator.205] iterator out of range json.exception.invalid_iterator.206¶
The iterators passed to constructor basic_json(InputIT beginning, InputIT last) belong to a JSON naught value and hence to not define a valid range.
Case bulletin
[json.exception.invalid_iterator.206] cannot construct with iterators from null json.exception.invalid_iterator.207¶
The key() fellow member function can only be used on iterators belonging to a JSON object, considering other types do non have a concept of a key.
Example bulletin
[json.exception.invalid_iterator.207] cannot use key() for not-object iterators json.exception.invalid_iterator.208¶
The operator[] to specify a concrete commencement cannot be used on iterators belonging to a JSON object, because JSON objects are unordered.
Example message
[json.exception.invalid_iterator.208] cannot apply operator[] for object iterators json.exception.invalid_iterator.209¶
The outset operators (+, -, +=, -=) cannot be used on iterators belonging to a JSON object, because JSON objects are unordered.
Instance message
[json.exception.invalid_iterator.209] cannot use offsets with object iterators json.exception.invalid_iterator.210¶
The iterator range passed to the insert function are not uniform, meaning they practise not belong to the same container. Therefore, the range (first, last) is invalid.
Example message
[json.exception.invalid_iterator.210] iterators do not fit json.exception.invalid_iterator.211¶
The iterator range passed to the insert office must not exist a subrange of the container to insert to.
Example bulletin
[json.exception.invalid_iterator.211] passed iterators may not vest to container json.exception.invalid_iterator.212¶
When 2 iterators are compared, they must belong to the same container.
Example message
[json.exception.invalid_iterator.212] cannot compare iterators of different containers json.exception.invalid_iterator.213¶
The order of object iterators cannot be compared, because JSON objects are unordered.
Example message
[json.exception.invalid_iterator.213] cannot compare lodge of object iterators json.exception.invalid_iterator.214¶
Cannot get value for iterator: Either the iterator belongs to a cypher value or information technology is an iterator to a archaic blazon (number, boolean, or string), but the iterator is dissimilar to begin().
Instance message
[json.exception.invalid_iterator.214] cannot become value Type errors¶
This exception is thrown in example of a type error; that is, a library function is executed on a JSON value whose type does non match the expected semantics.
Exceptions have ids 3xx.
Example
The post-obit lawmaking shows how a type_error exception tin can be caught.
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann :: json ; int main () { effort { // calling push_back() on a cord value json j = "string" ; j . push_back ( "another string" ); } catch ( json :: type_error & e ) { // output exception information std :: cout << "message: " << e . what () << '\n' << "exception id: " << e . id << std :: endl ; } } Output:
message: [json.exception.type_error.308] cannot use push_back() with cord exception id: 308 json.exception.type_error.301¶
To create an object from an initializer list, the initializer list must consist only of a list of pairs whose first chemical element is a string. When this constraint is violated, an array is created instead.
Example message
[json.exception.type_error.301] cannot create object from initializer list json.exception.type_error.302¶
During implicit or explicit value conversion, the JSON blazon must be uniform to the target type. For instance, a JSON string can only be converted into string types, but not into numbers or boolean types.
Instance bulletin
[json.exception.type_error.302] blazon must exist object, but is aught [json.exception.type_error.302] type must be string, but is object json.exception.type_error.303¶
To call up a reference to a value stored in a basic_json object with get_ref, the type of the reference must match the value type. For instance, for a JSON array, the ReferenceType must be array_t &.
Example message
[json.exception.type_error.303] incompatible ReferenceType for get_ref, actual type is object [json.exception.type_error.303] incompatible ReferenceType for get_ref, actual type is number" json.exception.type_error.304¶
The at() member functions can only exist executed for certain JSON types.
Example message
[json.exception.type_error.304] cannot apply at() with cord [json.exception.type_error.304] cannot use at() with number json.exception.type_error.305¶
The operator[] member functions can just be executed for certain JSON types.
Case message
[json.exception.type_error.305] cannot use operator[] with a string statement with array [json.exception.type_error.305] cannot use operator[] with a numeric argument with object json.exception.type_error.306¶
The value() member functions tin just be executed for sure JSON types.
Example message
[json.exception.type_error.306] cannot use value() with number json.exception.type_error.307¶
The erase() member functions can only be executed for certain JSON types.
Case message
[json.exception.type_error.307] cannot use erase() with string json.exception.type_error.308¶
The push_back() and operator+= fellow member functions tin can only exist executed for sure JSON types.
Case bulletin
[json.exception.type_error.308] cannot apply push_back() with string json.exception.type_error.309¶
The insert() fellow member functions can only be executed for certain JSON types.
Example message
[json.exception.type_error.309] cannot utilise insert() with array [json.exception.type_error.309] cannot use insert() with number json.exception.type_error.310¶
The swap() member functions can simply exist executed for sure JSON types.
Example message
[json.exception.type_error.310] cannot use bandy() with number json.exception.type_error.311¶
The emplace() and emplace_back() fellow member functions tin can but be executed for certain JSON types.
Example bulletin
[json.exception.type_error.311] cannot apply emplace() with number [json.exception.type_error.311] cannot utilize emplace_back() with number json.exception.type_error.312¶
The update() member functions tin merely be executed for certain JSON types.
Example message
[json.exception.type_error.312] cannot use update() with assortment json.exception.type_error.313¶
The unflatten function converts an object whose keys are JSON Pointers back into an capricious nested JSON value. The JSON Pointers must not overlap, because and then the resulting value would not be well-defined.
Case message
[json.exception.type_error.313] invalid value to unflatten json.exception.type_error.314¶
The unflatten function only works for an object whose keys are JSON Pointers.
Example message
Calling unflatten() on an assortment [ 1 , 2 , 3 ] :
[json.exception.type_error.314] merely objects tin be unflattened json.exception.type_error.315¶
The unflatten() part only works for an object whose keys are JSON Pointers and whose values are primitive.
Example message
Calling unflatten() on an object { "/1" , [ 1 , 2 , 3 ]} :
[json.exception.type_error.315] values in object must be primitive json.exception.type_error.316¶
The dump() function simply works with UTF-eight encoded strings; that is, if you assign a std::cord to a JSON value, make sure it is UTF-8 encoded.
Example bulletin
Calling dump() on a JSON value containing an ISO 8859-i encoded string:
[json.exception.type_error.316] invalid UTF-8 byte at index xv: 0x6F Tip
- Shop the source file with UTF-8 encoding.
- Pass an error handler equally final parameter to the
dump()role to avoid this exception:-
json::error_handler_t::replacewill supplant invalid bytes sequences withU+FFFD -
json::error_handler_t::ignorewill silently ignore invalid byte sequences
-
json.exception.type_error.317¶
The dynamic type of the object cannot be represented in the requested serialization format (eastward.g. a raw true or nil JSON object cannot exist serialized to BSON)
Example message
Serializing null to BSON:
[json.exception.type_error.317] to serialize to BSON, meridian-level type must be object, but is null Serializing [ i , two , iii ] to BSON:
[json.exception.type_error.317] to serialize to BSON, top-level type must be object, just is array Tip
Encapsulate the JSON value in an object. That is, instead of serializing true , serialize { "value" : true }
Out of range¶
This exception is thrown in instance a library function is called on an input parameter that exceeds the expected range, for case in case of assortment indices or nonexisting object keys.
Exceptions take ids 4xx.
Example
The post-obit lawmaking shows how an out_of_range exception can be defenseless.
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann :: json ; int main () { endeavour { // calling at() for an invalid alphabetize json j = { ane , 2 , 3 , four }; j . at ( 4 ) = 10 ; } catch ( json :: out_of_range & eastward ) { // output exception information std :: cout << "bulletin: " << eastward . what () << '\due north' << "exception id: " << east . id << std :: endl ; } } Output:
message: [json.exception.out_of_range.401] array index iv is out of range exception id: 401 json.exception.out_of_range.401¶
The provided assortment alphabetize i is larger than size-i.
Instance message
array index three is out of range json.exception.out_of_range.402¶
The special assortment index - in a JSON Pointer never describes a valid element of the array, but the index by the end. That is, it tin only be used to add elements at this position, but not to read information technology.
Example bulletin
array index '-' (iii) is out of range json.exception.out_of_range.403¶
The provided primal was non found in the JSON object.
json.exception.out_of_range.404¶
A reference token in a JSON Pointer could not be resolved.
Instance bulletin
unresolved reference token 'foo' json.exception.out_of_range.405¶
The JSON Patch operations 'remove' and 'add together' can not be practical to the root element of the JSON value.
Example message
JSON arrow has no parent json.exception.out_of_range.406¶
A parsed number could non be stored as without irresolute it to NaN or INF.
Example message
number overflow parsing '10E1000' json.exception.out_of_range.407¶
UBJSON and BSON only back up integer numbers upwards to 9223372036854775807.
Instance bulletin
number overflow serializing '9223372036854775808' Annotation
Since version three.ix.0, integer numbers across int64 are serialized every bit high-precision UBJSON numbers, and this exception does not farther occur.
json.exception.out_of_range.408¶
The size (following #) of an UBJSON array or object exceeds the maximal capacity.
Example message
excessive array size: 8658170730974374167 json.exception.out_of_range.409¶
Key identifiers to be serialized to BSON cannot incorporate lawmaking indicate U+0000, since the key is stored every bit zero-terminated c-string.
Example bulletin
BSON cardinal cannot comprise lawmaking point U+0000 (at byte ii) Further exceptions¶
This exception is thrown in case of errors that cannot be classified with the other exception types.
Exceptions have ids 5xx.
Example
The post-obit code shows how an other_error exception can be caught.
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann :: json ; int main () { try { // executing a failing JSON Patch performance json value = R " ( { "best_biscuit": { "name": "Oreo" } } ) " _json ; json patch = R " ( [{ "op": "test", "path": "/best_biscuit/name", "value": "Choco Leibniz" }] ) " _json ; value . patch ( patch ); } catch ( json :: other_error & due east ) { // output exception information std :: cout << "message: " << e . what () << '\n' << "exception id: " << e . id << std :: endl ; } } Output:
message: [json.exception.other_error.501] unsuccessful: {"op":"examination","path":"/best_biscuit/name","value":"Choco Leibniz"} exception id: 501 json.exception.other_error.501¶
A JSON Patch operation 'test' failed. The unsuccessful performance is too printed.
Instance message
Executing { "op" : "exam" , "path" : "/baz" , "value" : "bar" } on { "baz" : "qux" } :
[json.exception.other_error.501] unsuccessful: {"op":"examination","path":"/baz","value":"bar"} Could Not Read Json: Invalid Utf-8 Start Byte 0xbf
Source: https://json.nlohmann.me/home/exceptions/
0 Response to "Could Not Read Json: Invalid Utf-8 Start Byte 0xbf"
Post a Comment