Skip to content

kaifkh20/json-parser-c

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSON Parser in C

Build Status Tests

A lightweight, modular JSON parser written in C. It provides a clean API to tokenize, parse, and manage JSON objects, arrays, and values with safe memory handling.

Table of Contents

  1. Project Structure
  2. Features
  3. Build Instructions
  4. Running Tests
  5. Usage Example
  6. Future Improvements
  7. License

Project Structure

.
├── include/
│   ├── lexer.h         # Lexical analyzer (tokenizer)
│   ├── parser.h        # Core parser and JSON data structures
│   ├── utility_func.h  # Memory management + debugging helpers
│   ├── json_parser.h # High-level parsing interface
|   └── stack.h
├── src/
│   ├── lexer.c
│   ├── parser.c
│   ├── utility_func.c
│   ├── json_parser.c
│   └── stack.c
├── main.c              # Example entry point / test runner
├── makefile            # Build script (Linux & Windows)
└── tests/              # JS tests (bun test)

Features

  • Tokenizes JSON strings into typed tokens.
  • Parses into nested C data structures:
    • Object
    • Array
    • Value (STRING, INTEGER, FLOAT, BOOLEAN, NULL, etc.)
  • Safe memory deallocation helpers.
  • Simple API for parsing JSON from strings.

Build Instructions

Controlled by the project makefile:

Linux

make lin
# output: ./jp

Windows (cross-compile)

make win
# output: ./jp-w.exe  (requires MinGW)

Clean

make clean        # remove all builds
make clean-lin    # remove Linux build only
make clean-win    # remove Windows build only

Running Tests

Tests are written with bun test.

bun test

Note: Ensure Bun is installed: curl -fsSL https://bun.sh/install | bash

Usage Example

#include "json_parser.h"

int main() {
    char* json_input = "{ \"name\": \"Alice\", \"age\": 25, \"student\": false }";

    // Parse JSON string
    ResponseKV result = json_parser(json_input);

    // Work with result...
    // e.g. traverse values, print types, etc.

    // Free allocated memory
    free_mem(result);
    return 0;
}

Compile & Run

make lin
./jp

Future Improvements

  • Better error reporting for malformed JSON.
  • Support for streaming large JSON files.
  • Helper functions for traversal/querying (like JSONPath).

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published