Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream error #815

Closed
ghost opened this issue Nov 2, 2017 · 29 comments
Closed

stream error #815

ghost opened this issue Nov 2, 2017 · 29 comments

Comments

@ghost
Copy link

ghost commented Nov 2, 2017

I've been trying to implement this solution and I have been consistently getting the same stream error, regardless of the json file or example I use/follow. I know I'm probably doing something silly but I'm a lowly junior in my CS degree. Here is my code:

#include "json.hpp"

#include <fstream>

using json = nlohmann::json;

int main() {
    std::ifstream ifs{"file.json"};
    json parsed_json = json::parse(ifs);
    return 0;
}

and here is the json I've been using:

[
  {
    "stringKey": "Nathen",
    "intKey": 236
  },
  {
    "stringKey": "Emerald",
    "intKey": 739
  },
  {
    "stringKey": "Franco",
    "intKey": 544
  }
]

and here is the error:
/home/bry/CLionProjects/jsonexample/cmake-build-debug/jsonexample terminate called after throwing an instance of 'std::invalid_argument' what(): stream error Process finished with exit code 134 (interrupted by signal 6: SIGABRT)

@nlohmann
Copy link
Owner

nlohmann commented Nov 2, 2017

Could you please try with the develop version. There, the exception should contain more information where the parse error occurs (though the file looks OK).

@ghost
Copy link
Author

ghost commented Nov 2, 2017

How would I go about doing that?

Also, I ran the same program from my linux command line and the program executed perfectly. The issue I've reported happens when using CLion.

@nlohmann
Copy link
Owner

nlohmann commented Nov 4, 2017

@nlohmann
Copy link
Owner

nlohmann commented Nov 6, 2017

Any news on this?

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Nov 6, 2017
@ghost
Copy link
Author

ghost commented Nov 6, 2017

Sorry, I was out deer hunting this past weekend and I've had some other projects to work on. I will try it tonight.

@ghost
Copy link
Author

ghost commented Nov 6, 2017

I still receive an error, here's the output

terminate called after throwing an instance of 'nlohmann::detail::parse_error' what(): [json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input; expected '[', '{', or a literal

@nlohmann
Copy link
Owner

nlohmann commented Nov 7, 2017

It seems the file could not be opened - the first thing the parser sees is the end of file. In your example, can you make sure it is properly opened? Can you std::cout << ifs << std::endl;?

@ghost
Copy link
Author

ghost commented Nov 7, 2017

So I did manage to figure it out, I simply added the whole file path instead of just the file name. Thanks for all your help, this newbie appreciates it!

@nlohmann
Copy link
Owner

nlohmann commented Nov 7, 2017

No worries. Thanks for checking back!

@nlohmann nlohmann closed this as completed Nov 7, 2017
@feugen
Copy link

feugen commented Nov 13, 2017

Hello, I have the same issue, its working only for the whole file path but its not a solution for me. :-(

@ghost
Copy link
Author

ghost commented Nov 13, 2017

This isn't an issue related to this repository, it's an issue connected to your IDE

@nlohmann
Copy link
Owner

@feugen Could you please provide more information?

@feugen
Copy link

feugen commented Nov 15, 2017

What information do you need? I am using QtCreator and I dont thinnk its an IDE issue. The output of the binary is wrong, it says

terminate called after throwing an instance of 'std::invalid_argument'
what(): stream error

If I use the full path to my json file, I dont get this error, the output of the binary is the expected one.

@gregmarr
Copy link
Contributor

That's because if you don't use the full path to the file, then the part you use is combined with the working directory to form the full path. If you just include the filename and not the path, then it will only find your file if your working directory is the directory containing the file. That depends on how you're running the program. It is not related at all to this library. You'll find that this error does not change even if you remove all traces of the JSON library from your program.

@feugen
Copy link

feugen commented Nov 15, 2017

Ah ok, just moved my json file to a working directory and the error is gone, lol. Thanks

@nlohmann nlohmann removed the state: needs more info the author of the issue needs to provide more details label Nov 26, 2017
@abkalyan
Copy link

abkalyan commented Jul 10, 2018

Hi, I'm still getting the same error as @ghost, where my error is:

terminate called after throwing an instance of 'nlohmann::detail::parse_error' what(): [json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input; expected '[', '{', or a literal

How would I approach it? I tried using the whole path, but that doesn't seem to be fixing it.

-- Adit

@nlohmann
Copy link
Owner

Can you access the content of the file without passing it to the JSON library?

@abkalyan
Copy link

abkalyan commented Jul 10, 2018

So apparently my issue has something to do with initializer lists, dsteeam and command line args. When I pass a command line argument [: jsonFileIn(argv[2])] through an fstream initializer list, it didn't read anything in the file. But when I explicitly write[ : jsonFileIn("./result.json")] it works. I still don't get why this works, but I guess it does?

Long story short, it had nothing to do with your library.

@abkalyan
Copy link

Thanks for responding so promptly though

@LucaMarconato
Copy link

I having a similar problem and I think it is related to the one described in this topic. I am using macOS.

If I open a terminal, go to the folder in which the executable is located and run the executable, everything works. But if I run open -a Termina.app name_of_executable (which is equivalent to double clicking to the executable from the Finder) I got the following error:

libc++abi.dylib: terminating with uncaught exception of type nlohmann::detail::parse_error: [json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input; expected '[', '{', or a literal

The argv[0] parameters coincides for both the way of executing the program.

@LucaMarconato
Copy link

Looking at boost::filesystem::path full_path(boost::filesystem::current_path()); I noticed that this path changes depending on the way I am executing the program, so the .json file was not found in one case.

@nhule11
Copy link

nhule11 commented Nov 25, 2019

I have the same problem. I'm getting Json string from TCP protocol. I get some packets perfect but then I get an error "
terminate called after throwing an instance of 'std::invalid_argument'
what(): parse error - unexpected '"'; expected string literal
Aborted
"

this is how I convert my char into string then into Json "
std::string s = (string(buf, 0, bytesReceived));
json j = json::parse(s);
cout << j.dump(4) << endl;
"
I'm a newbie to c++ and raspberrypi. I really appreciate if anyone can help.

@nlohmann
Copy link
Owner

The exception says the received JSON is invalid. Try to print s it to std::cout before parsing and check if the value is indeed valid JSON.

@nhule11
Copy link

nhule11 commented Dec 2, 2019

@nlohmann You are right the string which I was receiving was incomplete. Your library works perfect! Thank you.

@Wangyouai
Copy link

@nlohmann Hello When I used this code ./surfelwarp_app /path/to/config under ubuntu16.04 to run the surfelwarp_app file in application/x-executable format, I encountered the same error: terminate called after throwing an instance of'nlohmann::detail: :parse_error' what(): [json.exception.parse_error.101] parse error at 1: syntax error-unexpected end of input; expected'[','{', or a literal
The error seems to be unable to open the json file, because after I delete the json file, I still encounter this error when I run it again. I think it may be necessary to read the absolute path,
However, currently I cannot view and modify how surfelwarp_app reads the json code through the compiler, so do you have other ways to solve this problem?

@nlohmann
Copy link
Owner

The error means in 99.9% of the time that the stream is broken somehow and the parser could not read a single byte. Please double check that the stream is OK, e.g., by trying to print it to stdout.

@Wangyouai
Copy link

Looking at boost::filesystem::path full_path(boost::filesystem::current_path()); I noticed that this path changes depending on the way I am executing the program, so the .json file was not found in one case.

So I want to know how you solved this problem? please

@Wangyouai
Copy link

The error means in 99.9% of the time that the stream is broken somehow and the parser could not read a single byte. Please double check that the stream is OK, e.g., by trying to print it to stdout.
Is this stdout?

wya@wya:~/project/SW/surfelwarp/test_data$ cat boxing_config.json
{
    "clip_far":1300,
    "clip_near":10,
    "data_prefix":"/home/wei/Documents/programs/surfelwarp/test_data/boxing",
    "depth_focal_x":570.0,"depth_focal_y":570.0,"depth_principal_x":320.0,"depth_principal_y":240.0,
    "gpc_model_path":"/home/wei/Documents/programs/surfelwarp/test_data/sintel_small_speed",
    "image_cols":640,"image_rows":480,
    "start_frame":0,
    "num_frame":310,
    "rgb_focal_x":570.0,"rgb_focal_y":570.0,"rgb_principal_x":320.0,"rgb_principal_y":240.0,
    "use_periodic_reinit":false,
    "reinit_period":30,
    "use_density":true,
    "use_foreground":false,
    "use_offline_foreground":false
}

@nlohmann
Copy link
Owner

What I meant is that you need to make sure the stream you pass to the library is valid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants