I am implementing a json parser in C++. I have written some code and wanted to share it on Stack Overflow.While writing this program I assumed that the json will only have objects and arrays are only string arrays.Also I used a stack to find corresponding closing ']'  or  '}'.I used vectors to store arrays so that I could get specific elements using their index.Below is my code:#include<iostream>#include<stack>#include<vector>using namespace std;class JsonHelper {    wstring json_wstr;    stack<wstring> wstack;    vector<wstring> v;public:    JsonHelper(wstring input_json);    JsonHelper operator[](wstring wstr);    JsonHelper operator[](long index);};JsonHelper::JsonHelper(wstring input_json):json_wstr(input_json) {     //Validate Json Here }JsonHelper JsonHelper::operator [](std::wstring wstr){    if(json_wstr.empty()) {        throw LJson Is Empty;    }    long pos = json_wstr.find(wstr);    if(pos == std::wstring::npos) {        throw LObject not found in json;    }    pos = json_wstr.find(L:,pos);    pos++;    long last_pos = pos;    while(1){        if(json_wstr[pos]== L'}' && wstack.empty()) {            break;        }        if(json_wstr[pos] == L',' && wstack.empty()){            break;        }        if(json_wstr[pos] == L'{') {            wstack.push(L{);        }        if(json_wstr[pos] == L'}'){            if(wstack.top().find(L{) == wstring::npos)                throw LInvalid Json;            wstack.pop();        }        if(json_wstr[pos] == L'[') {            wstack.push(L[);        }        if(json_wstr[pos] == L']'){            if(wstack.top().find(L[) == wstring::npos)                throw LInvalid Json;            wstack.pop();        }        pos++;    }    wstring json_object = json_wstr.substr(last_pos,(pos-last_pos));    json_object.erase(json_object.find_last_not_of(L \n\r\t)+1);    json_object.erase(0,json_object.find_first_not_of(L \n\r\t));    return JsonHelper(json_object);}JsonHelper JsonHelper::operator [](long index) {    wstring tmp;    if(json_wstr.empty()) {        throw LJson Is Empty;    }    long pos = json_wstr.find_first_of(L[);    if(pos == wstring::npos)        throw LInvalid Json;    long last_pos = ++pos;    while(1) {        if(json_wstr[pos] == L',' && wstack.empty()) {            tmp = json_wstr.substr(last_pos,pos-last_pos);            tmp.erase(tmp.find_last_not_of(L \n\r\t)+1);            tmp.erase(0,tmp.find_first_not_of(L \n\r\t));            v.push_back(tmp);            last_pos= pos+1;        }        if(json_wstr[pos] == L'{') {            wstack.push(L{);        }        if(json_wstr[pos] == L'}'){            if(wstack.top().find(L{) == wstring::npos)                throw LInvalid Json;            wstack.pop();        }        if(json_wstr[pos] == L'[') {            wstack.push(L[);        }        if(json_wstr[pos] == L']' && wstack.empty()) {            tmp = json_wstr.substr(last_pos,pos-last_pos);            tmp.erase(tmp.find_last_not_of(L \n\r\t)+1);            tmp.erase(0,tmp.find_first_not_of(L \n\r\t));            v.push_back(tmp);            break;        }        if(json_wstr[pos] == L']'){            if(wstack.top().find(L[) == wstring::npos)                throw LInvalid Json;            wstack.pop();        }        pos++;    }    if(v.size() == 0) {        throw Invalid Array object or array is empty;    }    return v.at(index-1);}int main() {    JsonHelper j(L{\ID\: \SGML\,\SortAs\: \SGML\,\GlossTerm\: \Standard Generalized Markup Language\,\Acronym\: \SGML\,\Abbrev\: \ISO 8879:1986\,\GlossDef\: {\para\: \A meta-markup language, used to create markup languages such as DocBook.\,\GlossSeeAlso\: [\XML\, \GML\]},\GlossSee\: \markup\});    JsonHelper j1 = j[LGlossDef][LGlossSeeAlso][2];    return 0;}  Please suggest improvements for my code.A few improvements I am looking for areValidate the Json, I am thinking of using regx in c++Get a specific value like Float, Bool, String etc.Improved PerformanceShould be able to handle huge json
Generic Json parser c++
c++;c++11;json
QuestionsValidate the Json, I am thinking of using regx in c++Bad Idea. Regular expressions are not designed for this. You should implement your own parser.Get a specific value like Float, Bool, String etc.Yes. You need this. Currently you can't get a value out of the object or array.Improved PerformanceSure.Should be able to handle huge jsonFor this you will need a real parser. Regular expressions will just make this worse.Code Reviewusing namespaceDon't do this:using namespace std;There is a reason that the standard namespace is called std and not standard. Its to make it easy to prefix things without bloating the code to much.Importing everything from the standard namesapce into the current context is very error prone and can cause problems in larger programs. Don't get into bad habits now.For details read: Why is using namespace std considered bad practice?Wide Strings    wstring json_wstr;Are you sure you want to use wide strings?From: rfc7159JSON text SHALL be encoded in UTF-8, UTF-16, or UTF-32.  The default     encoding is UTF-8Interface    // Access    JsonHelper operator[](wstring wstr);    JsonHelper operator[](long index);That's fine as far as it goes. If your JSON is an object or an array. But what happens if it is just a string on integer? There is currently no way to access it.Object AccessesJsonHelper JsonHelper::operator [](std::wstring wstr){    // STUFFThis finds the key.    long pos = json_wstr.find(wstr);    if(pos == std::wstring::npos) {        throw LObject not found in json;    }But it also finds keys that don't match the string exactly and also values. Say I was searching for Key. This would also match KeyValue or LongKey. You should check that the proceeding and following character are ''    long pos = 0L;    bool found;    do {        found = true;        pos = json_wstr.find(wstr, pos);        if(pos == std::wstring::npos) {            break;        }        if ((pos == 0) || json_wstr[pos - 1] != '' || pos + wstr.size() >= json_wstr.size() || json_wstr[pos + wstr.size()] != '') {           pos += 1;           found = false;           continue;        }        pos += wstr.size() + 1;        while(pos < json_wstr.size() && std::is_space(json_wstr[pos])) {            ++pos;        }        if (json_wstr[pos] != ':') {            found = false;        }    }    while(!found);    if (pos == std::wstring::npos) {        throw stuff;    }Throwing ObjectsThough it is not required. You should generally throw an object derived from std::exception. Preferably something derived from std::runtime_error.throw std::runtime_error(Key not found in JSON);DRY CodeDRY: Don't repeat yourself.There is a huges section of code that is repeated in both access functions. This should be refactored into its own method.String Literals in C++1Literal string in C++ have been improved.You don't need to escape all those  characters in your input string.wchar_t data[] = LRJSON({PLOP:PLOP})JSON;