skeeto / pdjson

C JSON parser library that doesn't suck
The Unlicense
281 stars 36 forks source link

Improve portability to embedded platforms #29

Open AlbanBedel opened 2 years ago

AlbanBedel commented 2 years ago

I needed some json library to integrate in a custom u-boot and was happy to find a library with a nice API and properly constrained memory usage. But to integrate pdjson in such an environment a few changes were needed, mostly revolving around the fact that no full libc is available. In particular FILE and float are often not available on embedded platforms and malloc is often avoided.

This series is a first attempt at refactoring the code to make the core part easier to integrate in such limited environment. For this I moved the support for FILE stream to a dedicated C file and split the header file in two. The expectation is that platforms with special needs can provide their own pdjson.h that take care of providing all the required types and libc compatibility before including pdjson_base.h which provide the declarations for the core functionality.

There could be other ways to achieve the same goal but I tried to keep with the simple layout and lack of configure script.

boris-kolpackov commented 2 years ago

I would personally prefer this to be done in a less invasive manner where only the "lacking" platforms have to jump through the hoops and for everyone else things stay more or less the same. Specifically:

  1. Instead of splitting things into two headers, I would add a conditional inclusion of a base header in pdjson.h, something along these lines:
#ifdef PDJSON_CUSTOM_BASE
#include "pdjson_base.h"
#else
#include <stdio.h>
...
#endif

This way those who need to jump through hoops can provide the header and define PDJSON_CUSTOM_BASE while the rest of us can live in ignorant bliss.

  1. I would use the more traditional conditional compilation for the stdio stuff rather than breeding separate files. The enable-by-default fits nicely into the above approach (and, as a bonus, would allow one to disable stdio even without resorting to the custom base header):
#ifdef PDJSON_CUSTOM_BASE
#include "pdjson_base.h"
#else
#ifndef PDJSON_STDIO
#  define PDJSON_STDIO 1
#endif

#if PDJSON_STDIO
#  include <stdio.h>
#endif
...
#endif

The same can be done for float and malloc (I haven't looked closely at those patches).

Let me also add that I am not the maintainer of this project and even if you decide to make these changes, I have no say on whether to merged.

@skeeto BTW, if you find maintaining pdjson a burden, I could probably help/take over.