by conradev · 2y ago · 0 comments
https://rawgit.com/miloyip/nativejson-benchmark/master/sampl...
The variance in feature set, design and performance is huge across all of them. I ultimately landed on libjson, written in C: https://github.com/vincenthz/libjson
It does a lot for you, but it notably does not build a tree for you and does not try to interpret numbers, which I found perfect for adding to languages with C FFI that have their own collection and number types. It’s also great for partial parsing if you need to do any sort of streaming.
It looks like this one can’t currently do partial parsing, but it looks great if C++ maps/vectors are your target.
It does no dynamic memory allocation, which is a plus in constrained IoT/embedded applications. But it’s really only a tokenizer. For example, if you want to parse fields out of a map, you have to write your own wrappers to iterate over key/value pairs. Since no data is copied out of the original buffer, all the “tokens” are given as byte offsets and lengths, not null-terminated strings, so you can’t just do printf(“%s”).
If you can’t (or don’t want to) malloc, it gets the job done. Not sure I’d recommend it for other applications though.
E.g., take the JSON:
"\"\uD83D\uDCA9\""
There's no (pointer, length) into that that you can then printf(…, ptr, len); you'd get the escapes, raw.Ofc., there might be situations like debugging where that's fine.
#include <stdio.h>
void main()
{
char buff[10] = {'R', 'o', 'b', 'o', 't', 't', 'y', 'p', 'e', 's'};
printf("=>%.*s<=", 4, ((char *)&buff + 3));
}
prints =>otty<=Far better than nicklohmann's monster build times.
https://github.com/nwpierce/jsb
My goal was to convert a stream of JSON to/from a binary stream that is easier to traverse and manipulate.