Содержание
- Exceptions¶
- Overview¶
- Base type¶
- Switch off exceptions¶
- Extended diagnostic messages¶
- Parse errors¶
- json.exception.parse_error.101¶
- json.exception.parse_error.102¶
- json.exception.parse_error.103¶
- json.exception.parse_error.104¶
- json.exception.parse_error.105¶
- json.exception.parse_error.106¶
- json.exception.parse_error.107¶
- json.exception.parse_error.108¶
- json.exception.parse_error.109¶
- json.exception.parse_error.110¶
- json.exception.parse_error.112¶
- json.exception.parse_error.101 when parsing data received over a socket #3313
- Comments
- What is the issue you have?
- The Error
- Please describe the steps to reproduce the issue.
- What is the expected behavior?
- And what is the actual behavior instead?
- Which compiler and operating system are you using?
- Which version of the library did you use?
- [json.exception.parse_error.101] #3582
- Replies: 2 suggested answers · 3 replies
- <>’s edit
- <>’s edit
- Incorrect parse error with binary data in keys? #838
- Comments
- [json.exception.parse_error.101] #3582
- Replies: 2 suggested answers · 3 replies
- <>’s edit
- <>’s edit
Exceptions¶
Overview¶
Base type¶
All exceptions inherit from class json::exception (which in turn inherits from std::exception ). It is used as the base class for all exceptions thrown by the basic_json class. This class can hence be used as «wildcard» to catch 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 by abort() calls. You can further control this behavior by defining JSON_THROW_USER (overriding throw ), JSON_TRY_USER (overriding try ), and JSON_CATCH_USER (overriding catch ).
Note that JSON_THROW_USER should leave the current scope (e.g., by throwing or aborting), as continuing after it may yield undefined behavior.
The code below switches off exceptions and creates a log entry with a detailed error message in case of errors.
Note the explanatory what() string of exceptions is not available for MSVC if exceptions are disabled, see #2824.
Extended diagnostic messages¶
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.
This exception can be hard 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 lead to the exception) can be created. That global context is provided as JSON Pointer.
As 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 by default. They can, however, be enabled by defining the preprocessor symbol JSON_DIAGNOSTICS to 1 before including json.hpp .
Now the exception message contains a JSON Pointer /address/housenumber that indicates which value has the wrong type.
Parse errors¶
This exception is thrown by the library when a parse error occurs. Parse errors can occur during the deserialization of JSON text, CBOR, MessagePack, as well as when using JSON Patch.
Exceptions have ids 1xx.
Member byte holds the byte index of the last read character in the input file.
For an input with n bytes, 1 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).
The following code shows how a parse_error exception can be caught.
json.exception.parse_error.101¶
This error indicates a syntax error while deserializing a JSON text. The error message describes that an unexpected token (character) was encountered, and the member byte indicates the error position.
Input ended prematurely:
Control character was not escaped:
String was not closed:
Invalid number format:
u was not be followed by four hex digits:
Invalid UTF-8 surrogate pair:
Invalid UTF-8 byte:
- Make sure the input is correctly read. Try to write the input to standard output to check if, for instance, 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. Code points above 0xFFFF are split into two uxxxx entries («surrogate pairs»). This error indicates that the surrogate pair is incomplete or contains an invalid code point.
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 up to 0x10FFFF. Code points above 0x10FFFF are invalid.
This exception is not used any more. Instead json.exception.parse_error.101 with a more detailed description is used.
json.exception.parse_error.104¶
RFC 6902 requires a JSON Patch document to be a JSON document that represents an array of objects.
json.exception.parse_error.105¶
An operation of a JSON Patch document must contain exactly one «op» member, whose value indicates the operation to perform. Its value must be one of «add», «remove», «replace», «move», «copy», or «test»; other values are errors.
json.exception.parse_error.106¶
An array index in a JSON Pointer (RFC 6901) may be 0 or any number without a leading 0 .
json.exception.parse_error.107¶
A JSON Pointer must be a Unicode string containing a sequence of zero or more reference tokens, each prefixed by a / character.
json.exception.parse_error.108¶
In a JSON Pointer, only
1 are valid escape sequences.
json.exception.parse_error.109¶
A JSON Pointer array index must be a number.
json.exception.parse_error.110¶
When parsing CBOR or MessagePack, the byte vector ends before the complete value has been read.
json.exception.parse_error.112¶
An unexpected byte was read in a binary format or length information is invalid (BSON).
Источник
json.exception.parse_error.101 when parsing data received over a socket #3313
What is the issue you have?
Getting a parse error when parsing data received via a read loop over a tcp socket.
The Error
Please describe the steps to reproduce the issue.
- Set up a tcp socket and read data from the socket stream with a read loop
- Set the buffer size to be smaller than the amount of data coming over the socket so the data has to be read in a loop
- json parse the data after it’s been read.
I have a string that’s 95 chars long, I set my read buffer to 55 to make sure that all data gets read via the loop. I can confirm that I receive all of the data with a std::cout of the buffer, but when I try to parse the buffer, I get a parse error. The parse function thinks it only received 55 chars. Changing the buffer size to a number larger than the message text fixes the issue. But I need to be able to read the data in a loop as I will be transferring large json dumps. My code is below:
What is the expected behavior?
The json should get parsed because I’m providing a full json string to the parse() method.
And what is the actual behavior instead?
libc++abi: terminating with uncaught exception of type nlohmann::detail::parse_error:
[json.exception.parse_error.101] parse error at line 1, column 56: syntax error while parsing
value — invalid string: missing closing quote; last read: ‘»127.0’
Which compiler and operating system are you using?
- Compiler: /usr/bin/g++ -Wall -std=c++17
- Operating system: OSX 12.1
Which version of the library did you use?
The text was updated successfully, but these errors were encountered:
Источник
[json.exception.parse_error.101] #3582
The below is an error I get when I run the code given hereafter,
terminate called after throwing an instance of ‘nlohmann::detail::parse_error’
what(): [json.exception.parse_error.101] parse error at line 2, column 1: syntax error while parsing value — unexpected end of input; expected ‘[‘, ‘ <‘, or a literal
Aborted
What is causing this error, and how can I resolve it?
Please note that this code an excerpt of the offending code, pulled out of a constructor of a class in my project.
#include
#include
#include
#include
int main(int argc, char** argv)
<
[[maybe_unused]] const auto i = argc;
Beta Was this translation helpful? Give feedback.
1 You must be logged in to vote
terminate called after throwing an instance of ‘nlohmann::detail::parse_error’ what(): [json.exception.parse_error.101] parse error at line 2, column 1: syntax error while parsing value — unexpected end of input; expected ‘[‘, ‘<‘, or a literal Aborted
What is causing this error,
The last read from the stream fails because the stream has not yet reached the end but does so while reading a JSON value.
You can catch the exception, but you won’t be able to tell if parsing failed due to a syntax error or because the end of the stream was reached.
<>’s edit
<>’s edit
terminate called after throwing an instance of ‘nlohmann::detail::parse_error’ what(): [json.exception.parse_error.101] parse error at line 2, column 1: syntax error while parsing value — unexpected end of input; expected ‘[‘, ‘<‘, or a literal Aborted
What is causing this error,
The last read from the stream fails because the stream has not yet reached the end but does so while reading a JSON value.
You can catch the exception, but you won’t be able to tell if parsing failed due to a syntax error or because the end of the stream was reached.
Reading multiple JSON values from one input is not well supported and requires some work. I don’t have a ready solution using a stream as input.
Beta Was this translation helpful? Give feedback.
Источник
Incorrect parse error with binary data in keys? #838
Bug Report
What is the issue you have?
I get a parse error after deserializing a map , the serialised message was created by same version of library. The key is binary data. The error message is not helpful to find what went wrong.
Please describe the steps to reproduce the issue. Can you provide a small but working code example?
m_message below contains the data to parse, the library seems to have done some escaping of my data but a quick inspection of the data suggests it’s valid.
._| 00000010 22 ae 28 47 26 df 86 f7 7f 22 3a 22 4b 65 79 55 |».(G&. «:»KeyU| 00000020 73 61 62 6c 65 22 2c 22 7b 5c 75 30 30 31 33 28 |sable»,»<u0013(| 00000030 eb 61 b5 54 e2 93 f7 5b 5c 75 30 30 31 65 3e 94 |.a.T. [u001e>.| 00000040 cc 3b 22 3a 22 4b 65 79 55 73 61 62 6c 65 22 2c |.;»:»KeyUsable»,| 00000050 22 93 78 99 20 e8 d6 52 5c 75 30 30 30 30 98 57 |».x. Ru0000.W| 00000060 7d f8 f2 dd 55 46 22 3a 22 4b 65 79 55 73 61 62 |>. UF»:»KeyUsab| 00000070 6c 65 22 2c 22 f0 92 9b 5c 75 30 30 30 65 5c 75 |le»,». u000eu| 00000080 30 30 31 32 c8 5e 5d 95 fe 3d 5f da e6 fb a8 22 |0012.^]..=_. «| 00000090 3a 22 4b 65 79 55 73 61 62 6c 65 22 7d |:»KeyUsable»> | terminate called after throwing an instance of ‘std::invalid_argument’ what(): parse error — unexpected ‘»‘; expected string literal»>
I provide hexdump of message above the given error. That error isn’t enough for me to understand what went wrong.
- What is the expected behavior?
- And what is the actual behavior instead?
A crash, please see above - Which compiler and operating system are you using? Is it a supported compiler?
arm-buildroot-linux-gnueabihf-g++.br_real (Buildroot 2017.05-git-13055-g71f35cff8-dirty) 5.4.0 - Did you use a released version of the library or the version from the develop branch?
ersion 2.1.1 - If you experience a compilation error: can you compile and run the unit tests?
No compilation error
The text was updated successfully, but these errors were encountered:
Источник
[json.exception.parse_error.101] #3582
The below is an error I get when I run the code given hereafter,
terminate called after throwing an instance of ‘nlohmann::detail::parse_error’
what(): [json.exception.parse_error.101] parse error at line 2, column 1: syntax error while parsing value — unexpected end of input; expected ‘[‘, ‘ <‘, or a literal
Aborted
What is causing this error, and how can I resolve it?
Please note that this code an excerpt of the offending code, pulled out of a constructor of a class in my project.
#include
#include
#include
#include
int main(int argc, char** argv)
<
[[maybe_unused]] const auto i = argc;
Beta Was this translation helpful? Give feedback.
1 You must be logged in to vote
terminate called after throwing an instance of ‘nlohmann::detail::parse_error’ what(): [json.exception.parse_error.101] parse error at line 2, column 1: syntax error while parsing value — unexpected end of input; expected ‘[‘, ‘<‘, or a literal Aborted
What is causing this error,
The last read from the stream fails because the stream has not yet reached the end but does so while reading a JSON value.
You can catch the exception, but you won’t be able to tell if parsing failed due to a syntax error or because the end of the stream was reached.
<>’s edit
<>’s edit
terminate called after throwing an instance of ‘nlohmann::detail::parse_error’ what(): [json.exception.parse_error.101] parse error at line 2, column 1: syntax error while parsing value — unexpected end of input; expected ‘[‘, ‘<‘, or a literal Aborted
What is causing this error,
The last read from the stream fails because the stream has not yet reached the end but does so while reading a JSON value.
You can catch the exception, but you won’t be able to tell if parsing failed due to a syntax error or because the end of the stream was reached.
Reading multiple JSON values from one input is not well supported and requires some work. I don’t have a ready solution using a stream as input.
Beta Was this translation helpful? Give feedback.
Источник
I’m trying to parse Json file and store the data into 2D array or vector. The Json file looks like this:
{"n" : 2,
"x" : [[1,2],
[0,4]]}
And this is what my code looks like and but I keep getting «json.exception.parse_error.101» error
#include <iostream>
#include "json.hpp"
#include <fstream>
using json = nlohmann::json;
using namespace std;
int main(int argc, const char * argv[]) {
ifstream i("trivial.json");
json j;
i >> j;
return 0;
}
HolyBlackCat
72.9k8 gold badges121 silver badges193 bronze badges
asked Sep 16, 2019 at 21:44
3
In short, you need to take checking before processing, like below:
ifstream i("trivial.json");
if (i.good()) {
json j;
try {
i >> j;
}
catch (const std::exception& e) {
//error, log or take some error handling
return 1;
}
if (!j.empty()) {
// make further processing
}
}
answered Sep 16, 2019 at 23:07
BokiBoki
6203 silver badges12 bronze badges
I’d agree with the suggestion that what you’re seeing probably stems from failing to open the file correctly. For one obvious example of how to temporarily eliminate that problem so you can test the rest of your code, you might consider reading the data in from an istringstream
:
#include <iostream>
#include <iomanip>
#include <nlohmann/json.hpp>
#include <sstream>
using json = nlohmann::json;
using namespace std;
int main(int argc, const char * argv[]) {
std::istringstream i(R"(
{"n" : 2,
"x" : [[1,2],
[0,4]]}
)");
json j;
i >> j;
// Format and display the data:
std::cout << std::setw(4) << j << "n";
}
As an aside, also note how you’re normally expected to include the header. You give the compiler <json-install-directory>/include
as the directory to search, and your code uses #include <nlohmann/json.hpp>
to include the header.
answered Sep 16, 2019 at 23:25
Jerry CoffinJerry Coffin
467k79 gold badges617 silver badges1098 bronze badges
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.