Skip to content

The Json-CPP project is a C++ library designed to parse, generate, and manipulate JSON data. It provides a simple and efficient way for C++ developers to work with JSON

Notifications You must be signed in to change notification settings

abdelfetah18/json-parser-cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSON Parser in C++

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.

How to Build

  1. Clone the repository or download the source code.

  2. Ensure that you have a cmake installed.

  3. 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

Examples

#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;
}

Tests

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.

About

The Json-CPP project is a C++ library designed to parse, generate, and manipulate JSON data. It provides a simple and efficient way for C++ developers to work with JSON

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published