19

What is the best way to generate UTF-8 JSON in C? I've looked at Jansson, but it seems extremely bulky. Is there any other good low-dependency library for creating and reading JSON objects/strings in C?

hippietrail
  • 15,848
  • 18
  • 99
  • 158
Peter Downs
  • 482
  • 1
  • 4
  • 14
  • 4
    What is wrong with Jansson? Seems like perfectly usable C library to me – Constantinius Jul 05 '11 at 14:44
  • It has a bunch of dependencies and my boss would rather not have to worry about them. *I* would be ok with using it, I'm just looking for alternatives first. – Peter Downs Jul 05 '11 at 15:08
  • 1
    Look at the list for C at http://json.org/. – Fox32 Jul 05 '11 at 15:09
  • 5
    @Peter Downs: It's time for your boss to retire. – Matt Joiner Jul 05 '11 at 15:19
  • @Peter: I'm not familiar with Jansson, but the docs claim that it has no dependencies - it even documents how to avoid using autotools if you can't use that. – Michael Burr Jul 05 '11 at 15:34
  • 4
    Jansson has zero dependencies. I'm using it on a project right now, you run automake, `#include ` in your source, read the great [documentation](http://www.digip.org/jansson/doc/2.2/apiref.html), and you're set. – ACK_stoverflow Mar 28 '12 at 20:57
  • 1
    Jansson depends on autoconf and supplies a minimal libtool that works but is not entirely portable, Does not provide flexible testing for -shared forms in configure.ac (as in depends on gcc), It does fail its first API test on big-endian ia64 and x86, so I am skeptical of this library. Raise issue #606 on GitHub. – Randall Becker Mar 19 '22 at 23:15

1 Answers1

24

Perhaps the JSON module from CCAN? http://ccodearchive.net/ It doesn't even depend on anything else from CCAN, and consists of exactly two files json.c and json.h

(The JSON module is here http://git.ozlabs.org/?p=ccan;a=tree;f=ccan/json )

Spudd86
  • 2,986
  • 22
  • 20
  • This is exactly what I've been looking for. A million thanks! – Peter Downs Jul 06 '11 at 13:15
  • 2
    Just following up in case anyone else sees this. The JSON CCAN module is awesome: fully functional but simple enough that I was able to add a few extensions. Very clean, and the source code is well documented and formatted. – Peter Downs Dec 19 '11 at 13:04
  • this code uses snprintf. the WIN32 and the linux versions of this are NOT identical. – Kinjal Dixit Dec 01 '15 at 07:23
  • It also uses GOTOS?! "Very clean" for sufficiently heretical values of clean. – Mark Storer Oct 21 '19 at 14:43
  • 1
    @MarkStorer It uses them for failure handling, which is a common pattern, and none of the destinations are more than a slight scroll away from the jump. goto is not as evil as everyone says see https://stackoverflow.com/questions/46586/goto-still-considered-harmful If goto results in simpler control flow than the alternative that's good, it's easier to reason about. The harm is when the goto makes the code harder to understand. – Spudd86 Oct 21 '19 at 21:40
  • 1
    I wasn't speaking of some failure of logic. There are perfectly valid reasons for using gotos in C code. I was speaking of HERESEY, inspiring dogmatic hatred. Quite different. Perhaps a ";)" would have cleared things up? – Mark Storer Oct 22 '19 at 11:49