4 ms·
I remember searching for a JSON library with minimal dependencies a while ago, and came across this: https://rawgit.com/miloyip/nativejson-benchmark/master/sam
by conradev 2y ago
I remember searching for a JSON library with minimal dependencies a while ago, and came across this:
https://rawgit.com/miloyip/nativejson-benchmark/master/sample/conformance.html 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 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.
- nwpierce 2y agoSomehow I didn't run across that one in my searching - I'll check it out. I've been working on a json C library myself: https://github.com/nwpierce/jsb 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.
- alex_suzuki 2y agoDid you try cJSON? Works well for me. https://github.com/DaveGamble/cJSON https://github.com/DaveGamble/cJSON
- msarnoff 2y agoIf you want to go extremely lightweight, there’s jsmn: https://github.com/zserge/jsmn https://github.com/zserge/jsmn 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.
- jdougan 2y agoIs there a reason you can't do printf("%.*s", strlen, strptr); ?
- jeffreygoesto 2y agoThe terminating character is still the closing double quote and not a null, since the library does neither copy out nor alter the input. For example tiny_json replaces the closing quotes to create C strings, but that needs the full file to be in a mutable buffer which can be prohibitive for small controllers reading some config from flash only.
- jdougan 2y agoWith the "%.*s" format you need no null at the end. It just counts out the characters: #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<=
- jeffreygoesto 2y agoAh. Ok. Scanning the length before printing is mandatory then.
- msarnoff 2y agoYes, that’s exactly what I’ve done.