TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Jsmn, a minimalistic JSON parser in C

89 pointsby jasonmooalmost 13 years ago

11 comments

udpalmost 13 years ago
The code is certainly very short, but what about \u escape sequences in strings, parsing different representations of numbers, etc.? Since those things are part of the JSON standard, you're not a JSON parser if you just leave them to the application to handle.<p>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.<p>(disclaimer: I also wrote a popular ANSI C JSON parser)
评论 #4387668 未加载
Xionalmost 13 years ago
This is not a JSON parser, it's a tokenizer. From a parser I'd expect at least acknowledgement of the basic key-value association.<p>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.
alisdairalmost 13 years ago
I wrote some simple jsmn examples, since it doesn't ship with any: <a href="https://github.com/alisdair/jsmn-example/" rel="nofollow">https://github.com/alisdair/jsmn-example/</a><p>And then I wrote about writing the examples: <a href="http://alisdair.mcdiarmid.org/2012/08/14/jsmn-example.html" rel="nofollow">http://alisdair.mcdiarmid.org/2012/08/14/jsmn-example.html</a>
评论 #4387503 未加载
duanebalmost 13 years ago
It fails to parse basic unicode escapes - not a JSON parser.
alexgoldstonealmost 13 years ago
For those interested in other lightweight implementations, I have not yet had a chance to compare this but have been very happy with cJSON by @dave_gamble for extremely resource-constrained embedded microcontrollers.<p><a href="http://sourceforge.net/projects/cjson/" rel="nofollow">http://sourceforge.net/projects/cjson/</a>
评论 #4388284 未加载
评论 #4387940 未加载
gilgad13almost 13 years ago
If we're golfing:<p><a href="https://github.com/quartzjer/js0n/blob/master/js0n.c" rel="nofollow">https://github.com/quartzjer/js0n/blob/master/js0n.c</a><p>Appears to work the same way, though it doesn't bubble back type information.<p>(Also, the `goto * go[ * cur];` trick was pretty crazy the first time I saw it)
评论 #4387234 未加载
评论 #4387441 未加载
nwjsmithalmost 13 years ago
Looks good. For those of us shopping around, what advantages does Jsmn have over yajl?
评论 #4387178 未加载
ijobs-lyalmost 13 years ago
What is the purpose of JSON?<p>Does it have to do with efficiency?<p>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?<p>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.<p>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.<p>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.<p>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!<p>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.<p>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.
lpgauthalmost 13 years ago
Out of curiosity, what's the fastest JSON parser written in C out there?<p>Is it still YAJL?
评论 #4389143 未加载
benatkinalmost 13 years ago
Direct link to the source: <a href="https://bitbucket.org/zserge/jsmn/src/1caee52d37e3/jsmn.c" rel="nofollow">https://bitbucket.org/zserge/jsmn/src/1caee52d37e3/jsmn.c</a><p>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.
rmkalmost 13 years ago
How is this different from jansson? I have used jansson in the past, and it has served me very well.
评论 #4387521 未加载