PitchHut
Log in / Sign up
pl_json
7 views
Streamlined JSON parsing for effortless data handling in C/C++.
Pitch

PL_JSON is a lightweight, single-header library for C/C++ that simplifies JSON parsing into a user-friendly structure. With its efficient tokenization process and straightforward API, developers can achieve a balance of speed and simplicity, making data handling a breeze without sacrificing performance.

Description

PL_JSON is a lightweight, single-header JSON parser designed for C and C++. This library is not only simple to implement but also provides reasonable performance, achieving throughput speeds of approximately 1.5 GB/sec on modern hardware. With around 250 lines of code (excluding the API), it stands out for its minimalistic design without sacrificing usability.

Key Features

  • Fast Performance: While not the absolute fastest option available, PL_JSON strikes a balance between speed and complexity, making it suitable for many applications.
  • Straightforward API: The API for accessing parsed JSON structures, such as json_t, is designed to be intuitive, allowing developers to easily navigate the resulting data.
  • Two-Step Parsing: The library employs a two-step process for memory efficiency. First, it tokenizes the input JSON into a temporary buffer, followed by parsing the tokens into the final structure.

Basic Usage

To use PL_JSON, define PL_JSON_IMPLEMENTATION before including the header file. Here's a quick example of how to read and parse a JSON file:

#define PL_JSON_IMPLEMENTATION
#include "pl_json.h"

// Read JSON file
FILE *fh = fopen(path, "rb");
fseek(fh, 0, SEEK_END);
int size = ftell(fh);
fseek(fh, 0, SEEK_SET);

char *json_text = malloc(size);
fread(json_text, 1, size, fh);
fclose(fh);

// Tokenization and parsing logic as demonstrated in the README

Check the full documentation in the header file for more details.

Limitations

Be aware that PL_JSON has some limitations:

  • It does not support unicode escape sequences (\uxxxx).
  • The library has a default nesting limit of 256.
  • It may accept invalid number formats that can lead to issues, such as 10.20.30.

In summary, PL_JSON is an excellent choice for developers looking for a simple yet effective way to parse JSON data in C/C++. Its efficiency, ease of use, and compact design make it a powerful tool for any project involving JSON.