This project is a simple JSON parser written in C++. It can parse a subset of JSON data formats, converting them into C++ data structures. The parser supports basic error handling to manage invalid JSON inputs, ensuring that malformed data is detected and reported.
-
Clone the repository or download the source code.
-
Ensure that you have a cmake installed.
-
Navigate to the project directory and compile the source code using the following commands:
cmake -S . -B build # change directroy to build folder cd build # build make
#include <fstream>
#include <iostream>
#include <sstream>
#include "json-parser/JSONParser.h"
int main() {
// Open the file
std::ifstream file("input.json");
// Check if the file is open
if (!file.is_open()) {
std::cerr << "Unable to open the file." << std::endl;
return 1;
}
// Read the file into a stringstream
std::stringstream buffer;
buffer << file.rdbuf();
// Close the file
file.close();
// Get the content of the stringstream as a string
std::string input = buffer.str();
// Output the content
// std::cout << input << std::endl;
JSONParser parser(input);
auto value = parser.parse();
if (!value.isError()) {
// This will print the json by visting all the tree nodes
value.value()->print(0);
} else {
std::cout << value.errorMessage() << std::endl;
}
return 0;
}
The test cases are sourced from nst/JSONTestSuite. All tests pass except for two, which fail due to a stack overflow caused by deeply nested objects. I'm currently working on a fix for this issue. You can check the GitHub Workflow logs for detailed results.