hse icon indicating copy to clipboard operation
hse copied to clipboard

Replace cJSON with another JSON library

Open tristan957 opened this issue 2 years ago • 14 comments

Description

Problems with cJSON:

  1. Undermaintained
  2. "Lots" of allocations (how big of an issue is this? not sure)
  3. Will leak memory depending on which cJSON APIs you use

Alternatives:

  1. yyjson
  2. jansson
  3. Wrap a C++ JSON library to create a C library

cc @alexttx

tristan957 avatar Jan 09 '23 21:01 tristan957

A C++ library to wrap could be https://github.com/nlohmann/json. It is extremely popular

tristan957 avatar Jan 10 '23 21:01 tristan957

"We used all the operator magic of modern C++ to achieve the same feeling in your code."

That's kinda frightening! Why not yyjson?

beckerg avatar Jan 10 '23 21:01 beckerg

We can use it. I am just giving a wide array of options.

tristan957 avatar Jan 10 '23 21:01 tristan957

library stars watchers forks
cJSON 8.3k 271 2.8k
yyjson 2.3k 54 220
jansson 2.7k 125 772
nlohmann/json 33.2k 756 5.7k

alexttx avatar Jan 11 '23 16:01 alexttx

One thing Greg had suggested was creating like a libhse-json that was just a wrapper around whatever library we choose, so that if we ever had to swap it out, it would be pretty straightforward to do in the future. Technically that work could happen right now wrapping cJSON.

But I am not sure how much gain there is in that approach.

tristan957 avatar Jan 11 '23 18:01 tristan957

What criteria should we use to evaluate this?

tristan957 avatar Jan 12 '23 16:01 tristan957

Last release of jansson: 9/9/2021 Last release of yyjson: 12/12/2022 Last release of nlohmann/json: 8/12/2022

tristan957 avatar Jan 12 '23 16:01 tristan957

jansson looks decent, but i didn't really dig in. the extra maturity could be a good thing...

beckerg avatar Jan 12 '23 17:01 beckerg

I edited Alex's comment with the table to include the cJSON stats. Technically cJSON is "mature", but we have a lot of problems with it that can't be fixed because it is unmaintained.

The one thing nlohmann/json has going for it is that it has at least one corporate sponsor.

tristan957 avatar Jan 12 '23 17:01 tristan957

cJSON may be mature, but the using doubles for integers stupidity is broken by design. So we need one that has actual support for 64-bit integers and is reasonably easy to use. yyjson looks pretty C-ish, jansson looks like you need to learn a bunch of magic printf-like escapes. But I haven't dug in much, would need to see some examples in-situ...

beckerg avatar Jan 12 '23 17:01 beckerg

One advantage of a wrapper is the ability to test the different libraries for performance, memory usage, etc. If the underlying APIs are too different, the wrapper may be more trouble than its worth. It might also be forced into adding its own overhead.

alexttx avatar Jan 13 '23 01:01 alexttx

yyjson is different enough from the others that it would probably be difficult to write a good wrapper that could abstract everything well since yyjson has a notion of immutability.

tristan957 avatar Jan 13 '23 15:01 tristan957

So the only way to instantiate a JSON object in yyjson to parse a string? Does that work for HSE?

alexttx avatar Jan 13 '23 18:01 alexttx

No. When you parse a file you get a different struct type in yyjson than you do if you are building a json object by hand.

tristan957 avatar Jan 13 '23 19:01 tristan957