Skip to content

what is "what()" function and why is jsoncpp throwing an exception about invalid syntax of it? #1325

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

Closed
creativegamer03 opened this issue Sep 5, 2021 · 2 comments

Comments

@creativegamer03
Copy link

So I was trying to test out this json parsing library with a sample JSON file I made. After some hiccups and some help from here and the codeblocks forum (I use Code::Blocks), I was able to build the program. However, when it ran, it returned an error/status code 3.

CONSOLE OUTPUT:

terminate called after throwing an instance of 'Json::RuntimeError'
  what():  * Line 1, Column 1
  Syntax error: value, object or array expected.

Process returned 3 (0x3)   execution time : 7.575 s
Press any key to continue.

The sample JSON file has been checked and is in a valid JSON format.
The code used the Json::Value type only.

Full Code:

#include "json/json.h"
#include <fstream>
#include <iostream>

int main() {
    Json::Value sample;

    std::ifstream sample_file("sample.json", std::ifstream::binary);
    sample_file >> sample;
    // cout << people; //This will print the entire json object.

    // The following lines will let you access the indexed objects.
    std::cout << "Bible version: ";
    std::cout << sample["version"]; // bible version
    std::cout << "Sample verse content";
    std::cout << sample["testaments"][0]["books"][2]["chapters"][1]["verses"][1]["verse"]; //sample bible verse selection from JSON file
    std::cout << "Sample footnote content";
    std::cout << sample["testaments"][0]["books"][2]["chapters"][1]["footnotes"][1]["reference"];
    std::cout << sample["testaments"][0]["books"][2]["chapters"][1]["footnotes"][1]["footnotes"]; //sample bible footnote selection from JSON file

    return 0;
}

Please help, or maybe fix it... Thank you.

@BillyDonahue
Copy link
Contributor

BillyDonahue commented Sep 5, 2021

You've taken this pretty far! I admire your persistence.

Okay what's going on now is not a jsoncpp bug. It's more of a general programming help request at this point.

This Json::RuntimeError exception is what you get when your code tries to treat a Json::Value as if it was a JSON object or array, when it is really holding some other type (or holding nothing which is like holding the "Null" type).

What's happening here is that all those [key] operators are expressing an expectation of a schema (data shape) and the input data doesn't conform to that schema. I mean I see it goes 9 levels deep into the sample here but the program does not look before it relies on these returned Values.

I recommend breaking these deep accessors into simpler expressions (maybe with a helper function),
and doing some data validation. That is, after you parse the document, run a function that validates that the Json::Value returned from the parse expression satisfies your expectations on its nodal structure. Make sure it has an array of "testaments" and so on all the way down. Make sure each "testaments" element has an array of "books", each "books" element has an array of "chapters", etc... Do this before trying to read specific nodes out of it in the following lines.

Good luck.

PS: To your original question, [what](https://en.cppreference.com/w/cpp/error/exception/what)() is a virtual member function of std::exception that tries to summarize what went wrong as a string. The C++ termination handler (a built-in language feature) is trying to be helpful here by printing out the what() value of the uncaught exception that's killing the process.

@creativegamer03
Copy link
Author

ah, ok... I'll try to do that.
Btw, I did manage to get it run perfectly using a simple sample JSON (without the lists yet) just to get to baby step the process. Also I used the header files under master/include/json (which means I don't need to use the vckpkg method of installing which is beautiful), switching from #include "json/json.h" to #include <json/json.h> (I did try #include <json/value.h>, but it errored alot, idk what happened there lol) and it worked smoothly.

Here's the simple JSON file and my code rn. I did look up the docs and followed some of the code example.
JSON

{
    "n":1,
    "data":
    {
        "name":"Chrome",
        "description":"Browse the internet.",
        "isEnabled":true
    }
}

Code

#include <json/json.h>
#include <fstream>
#include <iostream>

int main() {
    Json::Value root;

    std::ifstream json_file("./sample.json", std::ifstream::binary);
    json_file >> root;

    // std::cout << root;

    std::cout << "\n\n";

    std::cout << "Desktop " << root["n"] << "\n";
    std::cout << "=========================\n";
    std::cout << "Application Details: \n";
    std::cout << "Name: " << root["data"].get("name", "n/a").asString() << "\n";
    std::cout << "Description: " << root["data"].get("description", "n/a").asString() << "\n";
    std::cout << "Enabled: " << root["data"].get("isEnabled", "true").asString() << "\n";

    return 0;
}

Will close this issue as it is solved. :)

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

2 participants