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

Error: out_of_range #1348

Closed
david9472 opened this issue Nov 9, 2018 · 14 comments
Closed

Error: out_of_range #1348

david9472 opened this issue Nov 9, 2018 · 14 comments
Labels
state: needs more info the author of the issue needs to provide more details

Comments

@david9472
Copy link

I'm trying to read in a .txt file which is packed with JSON Objects. Afterwards I want to get a specific object out of the document, but everytime I try to run my problem I get an Error-Message like:

untreated exception at 0x778B17D2 in "programm_name.exe": Microsoft C++-exception:nlohmann:detail:parse_error at memorylocation 0x008FE65C

(Sorry for bad english);
However this is just for testing purpose. Later I want to parse the return string of an function.
But for this problem my code looks like this:

`string text;
json temp;

ifstream read;
read.open("response_enumerate.txt");
getline(read, text);
read.close();
json in = json::parse(text);

temp = in["/device/command/0"_json_pointer];
cout << temp;`

and my .txt file with the JSON Objects looks like this (just a part of it because otherwise nobody would look at this post because it would be to long)

{"device":[{ "command":"enumerate", "statusCode":0, "wait":0, "deviceMake":"Digilent", "deviceModel":"OpenScope MZ", "firmwareVersion":{ "major":1, "minor":301, "patch":0}, "macAddress":"00:7E:G0:8B:6C:CC", "calibrationSource":"flash", "nics":{ "wlan0":{ "macAddress":"00:1E:B0:8B:5B:AC"}}, "requiredCalibrationVer":17, "requiredWiFiParameterVer":8, "awg":{ "numChans":1,

@nlohmann
Copy link
Owner

nlohmann commented Nov 9, 2018

The problem seems to be that your input is invalid JSON. At least this is the output you mention. I am confused, because the title of this issue mentions out_of_range. Are these two individual problems?

The parse error should also contain a position in the code (since 3.4.0 with a line and column), and a detailed error message (like "unexepected end of file" or "invalid Unicode byte"). Can you please double check whether you can get the complete error message? You could also catch json::parse_error and output the result of .what().

Furthermore, you could use tools like https://jsonlint.com to see where the parse error is.

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Nov 9, 2018
@david9472
Copy link
Author

No I think that these problems are linked together. It's not like an "classic" error which appears in the Error-Section. While debugging it just stops the process, opens the "json_sax.hpp" file, and indicates an error in line 253 which says:

case 4: JSON_THROW(reinterpret_cast<const detail::out_of_range>(&ex)) // LCOV_EXCL_START

Thats why I mentioned "out_of_range" in the title of this issue.

And it says, as I mentioned above:

untreated exception at 0x778B17D2 in "programm_name.exe": Microsoft C++ exception:nlohmann:detail:parse_error at memorylocation 0x008FE65C

I'm sorry if this is an stupid error or mistake but I'm pretty new to programming.

@nlohmann
Copy link
Owner

The parser only throws an out_of_range error in case of number overflow. Can you share your JSON file, or check whether it contains numbers that cannot be expressed (e.g., really really large exponents)?

@david9472
Copy link
Author

david9472 commented Nov 11, 2018

I tried different JSON files with different content but there is still the same error.
The JSON file I used looks like this:

{
  "device": [
    {
      "command": "resetInstruments",
      "statusCode": 0,
      "wait": 1000
    }
  ]
}

So there isn't any large number but it still throws an out_of_range error.

Just to make sure, this is the c++ code I use for printing out the JSON Object:

#include "pch.h"
#include "OwnUtil.h"
#include <iostream>
#include <sstream>
#include <fstream>
#include <Windows.h>
#include <stdio.h>
#include <SFML/Network.hpp>
#include <nlohmann/json.hpp>
#include <nlohmann/adl_serializer.hpp>

using json = nlohmann::json;
using namespace std;
int main()
{
	string text;
	json temp;
	ifstream read;
	read.open("response_resetinstruments.txt");
	getline(read, text);
	read.close();
	json in = json::parse(text);
	temp = in["/device/command/0"_json_pointer];
	cout << temp;
	```
I expected to get "resetInstruments" as an output in my console.

@nlohmann
Copy link
Owner

Firstly, you can immediately parse from an std::ifstream - just pass it to the parser like this:

std::ifstream read("response_resetinstruments.txt");
json in = json::parse(read);

Secondly, JSON Pointer /device/command/0 cannot be applied to the JSON value:

  • /device accesses the array [ { "command": "resetInstruments", "statusCode": 0, "wait": 1000 } ]
  • /command is then invalid, because in an array, you can only use integers.

To access "resetInstruments", the correct JSON Pointer would be /device/0/command.

@david9472
Copy link
Author

Ok. I'm new to this so I want it as clear as possible. But still thank you for the improvement.

However, I applied what you mentioned above, and there is still the same error. It points to the out_of_range part in the json_sax headerfile.

@nlohmann
Copy link
Owner

This code works for me:

#include "json.hpp"
#include <iostream>
#include <fstream>

using json = nlohmann::json;

int main()
{
    std::ifstream read("response_resetinstruments.txt");
    json in = json::parse(read);
    auto temp = in["/device/0/command"_json_pointer];
    std::cout << temp << std::endl;
}

and outputs

"resetInstruments"

Can you try this code?

@david9472
Copy link
Author

Copied your code, inserted it in my programm and I still get the same error. I just had to include "pch.h" and the headerfiles are one folder above, so I have to include #include "nlohmann/json.hpp". But the rest is exactly the same.

@nlohmann
Copy link
Owner

To rule out issues with the input file, can you try

#include "json.hpp"
#include <iostream>

using json = nlohmann::json;

int main()
{
    json in = R"({ "device": [ { "command": "resetInstruments", "statusCode": 0, "wait": 1000 } ] })"_json;
    auto temp = in["/device/0/command"_json_pointer];
    std::cout << temp << std::endl;
}

@david9472
Copy link
Author

Sorry for not responting.
I had to add "pch.h" again but then it finally worked. Why isn't there any error message if I store the string directly into the JSON Obejct? And, even more important, what do I have to change in order to use some sort of .txt file or an response from another function as an imput?
Did I make some sort of mistake or did I overlook something?
Thanks in advance you really helpt me.

@nlohmann
Copy link
Owner

I do not understand your questions. Can you please be more specific?

@david9472
Copy link
Author

I very appreciate that the code works if I directly store the String in the JSON variable.
However I need to parse a string which is just the return of a function. But if I use a variable I can‘t write R“(...);.
So do you have any additional Information or tips on how I can parse from a string variable instead of a string directly?

@nlohmann
Copy link
Owner

You can call json j = json::parse(your_string);

@david9472
Copy link
Author

Thank you so much. You solved my problem now I can go to the next step in my project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: needs more info the author of the issue needs to provide more details
Projects
None yet
Development

No branches or pull requests

2 participants