Jsmn, a minimalistic JSON parser in C
zserge.bitbucket.org
zserge.bitbucket.org
Since this skimps out on half of the work, it won't even be able to tell you with certainty what's valid JSON and what isn't.
(disclaimer: I also wrote a popular ANSI C JSON parser)
Have you done any performance tests against other C json parsers?
Thanks,
It doesn't say that it's not useful. It's just that for anything non-trivial, you'd need to supplement this library with quite a bit of your own code, e.g. a stack for tracking nesting levels.
And then I wrote about writing the examples: http://alisdair.mcdiarmid.org/2012/08/14/jsmn-example.html
https://github.com/quartzjer/js0n/blob/master/js0n.c
Appears to work the same way, though it doesn't bubble back type information.
(Also, the `goto * go[ * cur];` trick was pretty crazy the first time I saw it)
http://docs.oracle.com/cd/E19205-01/820-7598/bjabt/index.html
And LLVM: http://blog.llvm.org/2010/01/address-of-label-and-indirect-branches.htmlAnswer: C99, borrowed from a GCC extension. http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Designated-Inits...
As far as js0n, it's a great example of cool things that can be done in gcc, but isn't really useful at all since it only handles the first level of depth and has no typing. The jsmn, with typing and nesting, is actually usable, also compiles a lot smaller than the big tables in js0n.
JSON just doesn't fit that profile. Neat implementation, but don't give the impression that any of this would help with embedded.
Sometimes you don't get to choose what the source format is because you're consuming someone else's data feed, and if all they offer is JSON, you parse JSON. Also, on a 400-ish mhz ARM with 64 MB of RAM parsing reasonably sized JSON data is no big deal in native code.
You need to further qualify your comments beyond just "embedded" because what you're now talking about (very low mhz, KB of RAM) is a niche within the larger embedded world.
Sure, it sounds like a bad idea, but it wouldn't be the first time someone has had to do something crazy for compatibility with someone else's stuff.
For me, that means code space and RAM measured in kilobytes, cycles in MHz. Most people don't realize that serializing data (most importantly, floating point numbers) to strings is a complicated and time-intensive matter. Even a limited printf implementation can easily cost you many kilobytes. Not to mention it introduces you to C's most special hell: variable length memory blocks containing strings.
The one thing that embedded gives you is a lot of control about your computing environment. Just sending around binary data is a very viable thing to do under these conditions. Not so for JSON; its single biggest selling point is that you can use it in every platform out there. Its the oldest tradeoff: giving up flexibility allows you to use more constrained processors (and save money).
Does it have to do with efficiency?
Because if so, now we find ourselves discussing the resource requirements just to scan/tokenise and parse it to get it back into a human readable form. Why did we translate it to a non-readable form in the first place? What were we trying to achieve?
Maybe we should let JSON be something the receiver translates text to (if they want that sort of format), not the sender. The receiver knows what resources she has to work with, the sender has to guess. The same principle applies to XML. By all means, play around with these machin-readable formats to your heart's content. But do it on the receiver side. No need to impose some particular format on everyone.
The "universal format" is plain text. The UNIX people realised this long ago. People read data as plain text, not JSON and not XML, not even HTML. No matter how many times you translate it into something else, using a machine to help you, it will, if humans are to read it, be translated back to plain text.
As for the "plain text haters", let us be reminded that UNIX can do typesetting. Professional quality typesetting. But that's the receiver's job.[1] There's a learning curve, sure, but what the receiver can produce using typesetting utilities on her own machine is world's better than what a silly web browser can produce from markup.
1. I am so tired of dumping PDF's to text and images. PDF makes it seemingly impossble to scan through a large number of documents quickly. Ever been tasked with reading through 100 documents all in PDF format (i.e., scanned images from a photocopier)? What could be accmplished in minutes with BRE takes hours or even days to accomplish. This is a problem that persists year after year. OCR is a hack. In most cases, the text should never have been scanned to an image in the first place. The documents are being created on computers, not typewriters!
So, as I see it, if you were a plain text hater, and you were really sincere about making things look nice, then you would be a proponent of educating people how to do typesetting and sending them plain text, the universal format, that they can easily work with.
My solution to JSON and XML is sed. It works in all resource conditions and most times is just as fast as any RAM hungry parser. If I need to do complex things, that's what lex and yacc are there for. Pipes and filters; small buffers. 'Nuf said.
Is it still YAJL?
This looks good to me. It isn't going to be the fastest or the shortest (no we aren't golfing) but it's simple and easy to understand.
jsmn only parses the JSON into tokens, you handle all the rest.
jannson provides its own hashmap etc., jsmn doesn't.
I've just used jsmn for a current project of mine where I've already had implemented e.g. a custom hashmap, and I didn't want to link two into my code. So the choice for the lean (if not to say "minimally") jsmn came naturally. And I don't regret it :)
https://github.com/popee/libjason/blob/master/jason.rl
It is implemented in ragel state machine compiler, with no other dependencies. It is modified version of libejson but simplified to use only standard JSON. Also small. Btw ragel is great utility ;-)