Live data from Hacker News

Sj.h: A tiny little JSON parsing library in ~150 lines of C99

github.com

11–20 of 248 posts

Re: Sj.h: A tiny little JSON parsing library in ~150 lines of C99

#11
post #10

Earlier quoted context omitted.

Real question, does it manage nested objects ?

It seems so: https://github.com/rxi/sj.h/blob/master/demo%2Fobject.c

yep but how deep can you parse nested into nested etc

Re: Sj.h: A tiny little JSON parsing library in ~150 lines of C99

#12
JSON parser libraries in general is a black hole of suffering imo.

They're either written with a different use case in mind, or a complex mess of abstractions; often both.

It's not a very difficult problem to solve if you only write exactly what you need for your specific use case.

Re: Sj.h: A tiny little JSON parsing library in ~150 lines of C99

#13
post #3

What’s the usecase for something like this? There are lots of excellent libraries for json available. Is this a teaching tool?

Trivial to integrate into an existing code base, minimal size overhead, no heap allocations, no stdlib usage (only stdbool.h and stddef.h included for type definitions), no C++ template shenanigans and very simple and straightforward API. C libraries which tick all those boxes are actually quite rare, and C++ libraries are much rarer.

Re: Sj.h: A tiny little JSON parsing library in ~150 lines of C99

#15
post #3

What’s the usecase for something like this? There are lots of excellent libraries for json available. Is this a teaching tool?

A small single file, pure C dependency that doesn't allocate memory can be a universal solution to a common problem if it works well.

Re: Sj.h: A tiny little JSON parsing library in ~150 lines of C99

#16
post #10

Earlier quoted context omitted.

It seems so: https://github.com/rxi/sj.h/blob/master/demo%2Fobject.c

yep but how deep can you parse nested into nested etc

Why don’t you look at the source code, it’s only 150 lines?

The nesting is limited by using an int as the depth counter. The C standard guarantees that MAX_INT is at least 32767, so that’s a limit on portable nesting depth. Nowadays int is typically 32 or 64 bits, so a much higher limit in typical C implementations.

If I see correctly, the library doesn’t check for overflow, however. This might conceivably be an exploitable vulnerability (and such an overflow would constitute UB).

Re: Sj.h: A tiny little JSON parsing library in ~150 lines of C99

#17
post #12

JSON parser libraries in general is a black hole of suffering imo. They're either written with a different use case in mind, or a complex mess of abstractions; often both. It's not a very difficult problem to solve if you only write exactly what you need for your specific use case.

You can't get much more 'opinion-less' than this library though. Iterate over keys and array items, identify the value type and return string-slices.

Re: Sj.h: A tiny little JSON parsing library in ~150 lines of C99

#18
post #3

What’s the usecase for something like this? There are lots of excellent libraries for json available. Is this a teaching tool?

Being able to parse without a lot of overhead and without allocations is quite interesting. E.g. when you process some massive json dump to just extract some properties (the Wikidata dumps come to mind).

Re: Sj.h: A tiny little JSON parsing library in ~150 lines of C99

#19
The library doesn’t check for signed integer overflow here:

https://github.com/rxi/sj.h/blob/eb725e0858877e86932128836c1...

https://github.com/rxi/sj.h/blob/eb725e0858877e86932128836c1...

https://github.com/rxi/sj.h/blob/eb725e0858877e86932128836c1...

Certain inputs can therefore trigger UB.

Post reply on HN