Skip to main content
fixed grammar
Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134

I am implementing a json parser in c++, iC++. I have written some code and i wanted to share it on stack overflow,Stack Overflow.

While writing this program i haveI assumed asthat the json will have only have objects and arrayarrays are ofonly string arrays.

Also i haveI used a stack to find corresponding closing ']' or '}'.

Also i haveI used vectorvectors to store arrayarrays so that iI could get specific elementelements using their index.

Below is my code:

Please Suggestsuggest improvements for my code. FewA few improvements iI am looking for are

  1. Validate the Json,i I am thinking of using regx in c++
  2. Get a specific value like Float, Bool, String etc.
  3. Improved Performance
  4. Should be able to handle huge json

I am implementing json parser in c++, i have written some code and i wanted to share it on stack overflow,

While writing this program i have assumed as the json will have only objects and array are of string.

Also i have used stack to find corresponding closing ']' or '}'

Also i have used vector to store array so that i could get specific element using index.

Below is my code

Please Suggest improvements for code. Few improvements i am looking for are

  1. Validate Json,i am thinking of using regx in c++
  2. Get specific value like Float, Bool, String etc.
  3. Improved Performance
  4. Should able to handle huge json

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:

Please suggest improvements for my code. A few improvements I am looking for are

  1. Validate the Json, I am thinking of using regx in c++
  2. Get a specific value like Float, Bool, String etc.
  3. Improved Performance
  4. Should be able to handle huge json
Source Link
Dipak
  • 143
  • 1
  • 4

Generic Json parser c++

I am implementing json parser in c++, i have written some code and i wanted to share it on stack overflow,

While writing this program i have assumed as the json will have only objects and array are of string.

Also i have used stack to find corresponding closing ']' or '}'

Also i have used vector to store array so that i could get specific element using 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 L"Json Is Empty";
    }
    long pos = json_wstr.find(wstr);
    if(pos == std::wstring::npos) {
        throw L"Object 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 L"Invalid Json";
            wstack.pop();
        }
        if(json_wstr[pos] == L'[') {
            wstack.push(L"[");
        }
        if(json_wstr[pos] == L']'){
            if(wstack.top().find(L"[") == wstring::npos)
                throw L"Invalid 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 L"Json Is Empty";
    }
    long pos = json_wstr.find_first_of(L"[");
    if(pos == wstring::npos)
        throw L"Invalid 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 L"Invalid 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 L"Invalid 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[L"GlossDef"][L"GlossSeeAlso"][2];
    return 0;
}  

Please Suggest improvements for code. Few improvements i am looking for are

  1. Validate Json,i am thinking of using regx in c++
  2. Get specific value like Float, Bool, String etc.
  3. Improved Performance
  4. Should able to handle huge json