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

I may not understand #507

Closed
TParis opened this issue Mar 15, 2017 · 26 comments
Closed

I may not understand #507

TParis opened this issue Mar 15, 2017 · 26 comments

Comments

@TParis
Copy link

TParis commented Mar 15, 2017

Hello, I'm new to C++.

I'm using Baylor's GNU licensed socket example to pull from omdbapi.com a JSON object with information about movies. This is for a non-academic class and it's a side project just to keep me busy while the class goes over the basics.

I have a std::string JSON and it has the value of:

{"Title":"Star Trek Beyond","Year":"2016","Rated":"PG-13","Released":"22 Jul 2016","Runtime":"122 min","Genre":"Action, Adventure, Sci-Fi","Director":"Justin Lin","Writer":"Simon Pegg, Doug Jung, Gene Roddenberry (based upon "Star Trek" created by)","Actors":"Chris Pine, Zachary Quinto, Karl Urban, Zoe Saldana","Plot":"The USS Enterprise crew explores the furthest reaches of uncharted space, where they encounter a new ruthless enemy who puts them and everything the Federation stands for to the test.","Language":"English","Country":"USA, Hong Kong, China","Awards":"Nominated for 1 Oscar. Another 1 win & 17 nominations.","Poster":"https://images-na.ssl-images-amazon.com/images/M/MV5BODgzN2E1YjctODg5Yi00YzYwLWJjZjAtNDg2MGE2Y2MyYjBmXkEyXkFqcGdeQXVyNjM1MTQ0NTQ@._V1_SX300.jpg","Metascore":"68","imdbRating":"7.1","imdbVotes":"154,036","imdbID":"tt2660888","Type":"movie","Response":"True"}

This is my code:

    string amazonData = getAmazonData("Star Trek Beyond");
string JSON = isolateJSON(&amazonData);
try {
	movieObject = json::parse(JSON);
}
catch (exception& e) {
	printf("%s\n", e.what());
}

VS has highlighted the json::parse and said "parse no instance of overloaded function". It appears to accept std::string so I'm not sure what the issue is.

Any help is appreciated.

@gregmarr
Copy link
Contributor

Does it build even though intellisense is giving you an error? The intellisense compiler is different than the main compiler, so it may just be a problem with the intellisense compiler.

@TParis
Copy link
Author

TParis commented Mar 15, 2017

It compiles but it errors. This is the error stack:

Unhandled Exception: System.Runtime.InteropServices.SEHException: External component has thrown an exception.
at _CxxThrowException(Void* , _s__ThrowInfo* )
at nlohmann.detail.from_json<class nlohmann::basic_json<class std::map,class std::vector,class std::basic_string<char,struct std::char_traits,class std::allocator >,bool,__int64,unsigned __int64,double,class std::allocator,struct nlohmann::adl_serializer>,int,0>(basic_json<std::map,std::vector,std::basic_string<char,std::char_traits,std::allocator >,bool,__int64,unsigned __int64,double,std::allocator,nlohmann::adl_serializer>* j, Int32* val) in c:\users\dotlaptop\documents\visual studio 2015\projects\consoleapplication7\consoleapplication7\json.hpp:line 817
at nlohmann.detail.from_json_fn.call<class nlohmann::basic_json<class std::map,class std::vector,class std::basic_string<char,struct std::char_traits,class std::allocator >,bool,__int64,unsigned __int64,double,class std::allocator,struct nlohmann::adl_serializer>,int>(from_json_fn* , basic_json<std::map,std::vector,std::basic_string<char,std::char_traits,std::allocator >,bool,__int64,unsigned __int64,double,std::allocator,nlohmann::adl_serializer>* j, Int32* val, priority_tag<1> __unnamed002) in c:\users\dotlaptop\documents\visual studio 2015\projects\consoleapplication7\consoleapplication7\json.hpp:line 856
at nlohmann.detail.from_json_fn.operator()<class nlohmann::basic_json<class std::map,class std::vector,class std::basic_string<char,struct std::char_traits,class std::allocator >,bool,__int64,unsigned __int64,double,class std::allocator,struct nlohmann::adl_serializer>,int>(from_json_fn* , basic_json<std::map,std::vector,std::basic_string<char,std::char_traits,std::allocator >,bool,__int64,unsigned __int64,double,std::allocator,nlohmann::adl_serializer>* j, Int32* val) in c:\users\dotlaptop\documents\visual studio 2015\projects\consoleapplication7\consoleapplication7\json.hpp:line 871

@nlohmann nlohmann added the platform: visual studio related to MSVC label Mar 15, 2017
@gregmarr
Copy link
Contributor

gregmarr commented Mar 15, 2017

Do you have an example of the JSON that's failing? Never mind, I see it.

@gregmarr
Copy link
Contributor

This might be your problem:

"Writer":"Simon Pegg, Doug Jung, Gene Roddenberry (based upon "Star Trek" created by)",

Those quotes around Star Trek should be escaped.

@TParis
Copy link
Author

TParis commented Mar 15, 2017

The json I posted appears not to be the raw data, hang on and I'll grab the raw values.

@TParis
Copy link
Author

TParis commented Mar 15, 2017

This is the actual data in the JSON string. The value before I was grabbing from VS' visualizer but apparently it doesn't handle it correctly.

{"Title":"Star Trek Beyond","Year":"2016","Rated":"PG-13","Released":"22 Jul 2016","Runtime":"122 min","Genre":"Action, Adventure, Sci-Fi","Director":"Justin Lin","Writer":"Simon Pegg, Doug Jung, Gene Roddenberry (based upon "Star Trek" created by)","Actors":"Chris Pine, Zachary Quinto, Karl Urban, Zoe Saldana","Plot":"The USS Enterprise crew explores the furthest reaches of uncharted space, where they encounter a new ruthless enemy who puts them and everything the Federation stands for to the test.","Language":"English","Country":"USA, Hong Kong, China","Awards":"Nominated for 1 Oscar. Another 1 win & 17 nominations.","Poster":"https://images-na.ssl-images-amazon.com/images/M/MV5BODgzN2E1YjctODg5Yi00YzYwLWJjZjAtNDg2MGE2Y2MyYjBmXkEyXkFqcGdeQXVyNjM1MTQ0NTQ@._V1_SX300.jpg","Metascore":"68","imdbRating":"7.1","imdbVotes":"154,036","imdbID":"tt2660888","Type":"movie","Response":"True"}

@gregmarr
Copy link
Contributor

This still has the same issue. It's invalid JSON.

@TParis
Copy link
Author

TParis commented Mar 15, 2017

Hmm, when I pasted it, it had the escape vals.
{"Title":"Star Trek Beyond","Year":"2016","Rated":"PG-13","Released":"22 Jul 2016","Runtime":"122 min","Genre":"Action, Adventure, Sci-Fi","Director":"Justin Lin","Writer":"Simon Pegg, Doug Jung, Gene Roddenberry (based upon \"Star Trek\" created by)","Actors":"Chris Pine, Zachary Quinto, Karl Urban, Zoe Saldana","Plot":"The USS Enterprise crew explores the furthest reaches of uncharted space, where they encounter a new ruthless enemy who puts them and everything the Federation stands for to the test.","Language":"English","Country":"USA, Hong Kong, China","Awards":"Nominated for 1 Oscar. Another 1 win & 17 nominations.","Poster":"https://images-na.ssl-images-amazon.com/images/M/MV5BODgzN2E1YjctODg5Yi00YzYwLWJjZjAtNDg2MGE2Y2MyYjBmXkEyXkFqcGdeQXVyNjM1MTQ0NTQ@._V1_SX300.jpg","Metascore":"68","imdbRating":"7.1","imdbVotes":"154,036","imdbID":"tt2660888","Type":"movie","Response":"True"}

Is that better?

Edit: It appears github was also messing with my escape characters.

@gregmarr
Copy link
Contributor

Yes, much.

@nlohmann
Copy link
Owner

You can add three backticks around your example, like this.

```js
{"foo": "bar"}
```

This is then rendered as

{"foo": "bar"}

@gregmarr
Copy link
Contributor

Do you know the release number or the commit hash of the version you're using, because it's not the head of master, the line numbers don't line up.

@TParis
Copy link
Author

TParis commented Mar 15, 2017

I can't be positive, but I think it's c7afb34

@gregmarr
Copy link
Contributor

gregmarr commented Mar 16, 2017

Unhandled Exception: System.Runtime.InteropServices.SEHException: External component has thrown an exception.
at _CxxThrowException(Void* , _s__ThrowInfo* )
at from_json<basic_json<>,int,0>(basic_json<>* j, Int32* val) in json.hpp:line 817
at from_json_fn.call<basic_json<>,int>(from_json_fn* , basic_json<>* j, Int32* val, priority_tag<1> __unnamed002) in json.hpp:line 856
at from_json_fn.operator()<basic_json<>,int>(from_json_fn* , basic_json<>* j, Int32* val) in json.hpp:line 871

Reformatted, stripped namespaces, template params and paths, to make it easier to read.

@gregmarr
Copy link
Contributor

gregmarr commented Mar 16, 2017

Line 817 is:

            JSON_THROW(std::domain_error("type must be number, but is " + j.type_name()));

@gregmarr
Copy link
Contributor

Do you have any more of the call stack? It seems weird to me that this error would come up in parse().

@TParis
Copy link
Author

TParis commented Mar 16, 2017

When I get back to the office tomorrow, I'll give the rest of the call stack.

@TParis
Copy link
Author

TParis commented Mar 16, 2017

This is the entire stack

Unhandled Exception: System.Runtime.InteropServices.SEHException: External component has thrown an exception.
   at _CxxThrowException(Void* , _s__ThrowInfo* )
   at nlohmann.detail.from_json<class nlohmann::basic_json<class std::map,class std::vector,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,bool,__int64,unsigned __int64,double,class std::allocator,struct nlohmann::adl_serializer>,int,0>(basic_json<std::map\,std::vector\,std::basic_string<char\,std::char_traits<char>\,std::allocator<char> >\,bool\,__int64\,unsigned __int64\,double\,std::allocator\,nlohmann::adl_serializer>* j, Int32* val) in c:\users\dotlaptop\documents\visual studio 2015\projects\consoleapplication7\consoleapplication7\json.hpp:line 817
   at nlohmann.detail.from_json_fn.call<class nlohmann::basic_json<class std::map,class std::vector,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,bool,__int64,unsigned __int64,double,class std::allocator,struct nlohmann::adl_serializer>,int>(from_json_fn* , basic_json<std::map\,std::vector\,std::basic_string<char\,std::char_traits<char>\,std::allocator<char> >\,bool\,__int64\,unsigned __int64\,double\,std::allocator\,nlohmann::adl_serializer>* j, Int32* val, priority_tag<1> __unnamed002) in c:\users\dotlaptop\documents\visual studio 2015\projects\consoleapplication7\consoleapplication7\json.hpp:line 856
   at nlohmann.detail.from_json_fn.operator()<class nlohmann::basic_json<class std::map,class std::vector,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,bool,__int64,unsigned __int64,double,class std::allocator,struct nlohmann::adl_serializer>,int>(from_json_fn* , basic_json<std::map\,std::vector\,std::basic_string<char\,std::char_traits<char>\,std::allocator<char> >\,bool\,__int64\,unsigned __int64\,double\,std::allocator\,nlohmann::adl_serializer>* j, Int32* val) in c:\users\dotlaptop\documents\visual studio 2015\projects\consoleapplication7\consoleapplication7\json.hpp:line 871
   at nlohmann.adl_serializer<int,void>.from_json<class nlohmann::basic_json<class std::map,class std::vector,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,bool,__int64,unsigned __int64,double,class std::allocator,struct nlohmann::adl_serializer> const &,int>(basic_json<std::map\,std::vector\,std::basic_string<char\,std::char_traits<char>\,std::allocator<char> >\,bool\,__int64\,unsigned __int64\,double\,std::allocator\,nlohmann::adl_serializer>* j, Int32* val) in c:\users\dotlaptop\documents\visual studio 2015\projects\consoleapplication7\consoleapplication7\json.hpp:line 918
   at nlohmann.basic_json<std::map,std::vector,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,bool,__int64,unsigned __int64,double,std::allocator,nlohmann::adl_serializer>.get<int,int,0>(basic_json<std::map\,std::vector\,std::basic_string<char\,std::char_traits<char>\,std::allocator<char> >\,bool\,__int64\,unsigned __int64\,double\,std::allocator\,nlohmann::adl_serializer>* ) in c:\users\dotlaptop\documents\visual studio 2015\projects\consoleapplication7\consoleapplication7\json.hpp:line 3193
   at nlohmann.basic_json<std::map,std::vector,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,bool,__int64,unsigned __int64,double,std::allocator,nlohmann::adl_serializer>.operator<int,0>(basic_json<std::map\,std::vector\,std::basic_string<char\,std::char_traits<char>\,std::allocator<char> >\,bool\,__int64\,unsigned __int64\,double\,std::allocator\,nlohmann::adl_serializer>* ) in c:\users\dotlaptop\documents\visual studio 2015\projects\consoleapplication7\consoleapplication7\json.hpp:line 3451
   at main() in c:\users\dotlaptop\documents\visual studio 2015\projects\consoleapplication7\consoleapplication7\consoleapplication7.cpp:line 54
   at _mainCRTStartup()

@gregmarr
Copy link
Contributor

   at from_json_fn.call<basic_json<>,int>(from_json_fn* , basic_json<>* j, Int32* val, priority_tag<1> __unnamed002) in json.hpp:line 856
   at from_json_fn.operator()<basic_json<>,int>(from_json_fn* , basic_json<>* j, Int32* val) in json.hpp:line 871
   at adl_serializer<int,void>.from_json<basic_json<> const &,int>(basic_json<>* j, Int32* val) in json.hpp:line 918
   at basic_json<>.get<int,int,0>(basic_json<>* ) in json.hpp:line 3193
   at basic_json<>.operator<int,0>(basic_json<>* ) in json.hpp:line 3451

@gregmarr
Copy link
Contributor

What is the type of movieObject? This is in operator int(), so it looks like it's the assignment after the json::parse() call.

@TParis
Copy link
Author

TParis commented Mar 16, 2017

movieObject is a json object. I believe I tried it with auto, but perhaps not. I'll give that a try after I post this but here is a larger code snippet:

	Movie movies[5];
	int i;
	json movieObject;

	loadData(movies);

	printf("\n\nMy favorite movies are:\n----------\n\n");
	string amazonData = getAmazonData("Star Trek Beyond");
	string JSON = isolateJSON(&amazonData);
	printf("%s", JSON.c_str());
	try {
		movieObject = json::parse(JSON);
	}
	catch (exception& e) {
		printf("%s\n", e.what());
	}

Edit: I'm unable to get it to work with auto but that may be because I have no idea how to implement the auto class.

@gregmarr
Copy link
Contributor

auto isn't a class, it means infer the type of the variable from the initializer. Since you don't have an initializer, you can't use auto. You would have to do

try {
    auto movieObject = json::parse(JSON);
}

but then you couldn't use movieObject after the try block.

Does the code above include line 54? If so, which line is that?

@TParis
Copy link
Author

TParis commented Mar 16, 2017

Huh, exactly like the readme? I think what happened there is that VS was giving me a red squiggly. In my troubleshooting, I must've split that away. But like you said earlier, the compiler may not understand and I should ignore that.

Anyway, now I get "type must be number, but is string"

Edit: And I moved line 54 into the try. It was this printf() that you see.

	Movie movies[5];
	int i;

	loadData(movies);

	printf("\n\nMy favorite movies are:\n----------\n\n");
	string amazonData = getAmazonData("Star Trek Beyond");
	string JSON = isolateJSON(&amazonData);

	try {
		auto movieObject = json::parse(JSON);
		printf("%d) %s.\n\tReleased in %d, received a rating of %.1f and is available on Amazon for $%.2f.\n\n", 1, movieObject["Title"], (int)movieObject["Year"], (double)movieObject["imdbRating"], 9.99);

	}
	catch (exception& e) {
		printf("%s\n", e.what());
	}

@gregmarr
Copy link
Contributor

gregmarr commented Mar 16, 2017

printf("%d) %s.\n\tReleased in %d, received a rating of %.1f and is available on Amazon for $%.2f.\n\n", 1, movieObject["Title"], (int)movieObject["Year"], (double)movieObject["imdbRating"], 9.99);

movieObject["Year"] and movieObject["imdbRating"] are both strings: "Year":"2016" "imdbRating":"7.1"

string title = movieObject["Title"];
string year = movieObject["Year"];
string rating = movieObject["imdbRating"];
printf("%d) %s.\n\tReleased in %s, received a rating of %s and is available on Amazon for $%.2f.\n\n", 1, title.c_str(), year.c_str(), rating.c_str(), 9.99);

@TParis
Copy link
Author

TParis commented Mar 16, 2017

gregmarr: Thank you so much, you've taught me a lot. We're not this far in the class yet, so this has been a great chance to learn. Thanks nlohmann for the library as well.

@TParis TParis closed this as completed Mar 16, 2017
@nlohmann
Copy link
Owner

Good observation, @gregmarr.

@nlohmann
Copy link
Owner

Thanks @gregmarr for the support!

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

No branches or pull requests

3 participants