"I have a mind like a steel... uh... thingy." Patrick Logan's weblog.

Search This Blog

Tuesday, May 03, 2005

Announcing json-py

Updates from the comments:

Ian Bicking suggests doctest would be more appropriate than unittest. I would have to agree, if I were enough of a Python programmer to know to use doctest. Each unit test is a couple of lines long, so I should move them to doctests with little trouble.

Eric Thompson wonders about support for tuples. The JSON array syntax looks like Python's list syntax, so I chose read to produce lists. JSON only has "objects" (dictionaries) and "arrays" (could be lists or tuples) for data structures. The write function could accept either lists or tuples and produce JSON arrays. Currently only lists are implemented. I'm open to suggestions.

End Update

I wrote a JSON reader and writer in Python over the last few days. Today sourceforge approved and created the project, and the files are ready for download.

It's "pure" Python 2.4 and has no other dependencies. Most of JSON is implemented, which is not much, coming in at about 200 LOC. The missing piece is the \uXXXX notation for hex Unicode characters. There's probably some missing functionality in the writer, e.g. writing escaped characters correctly, because I've mostly needed the reader so far.

There's one other Python implementation I know of and I think this new one is a good bit more correct and complete. There are 40 some unit tests, just enough to get my needs met. They do not exhaustively stress the specification yet.

There are several implementations in other languages. JSON can be used in AJAX-based systems in lieu of XML. In my case I've been using JSON for simple, yet expressive, configuration files. (I had set out to use YAML. Although simpler than XML it is still a good bit more complex than JSON.)

[Update: Jeremy Kemper points out in a comment that JSON is a subset of YAML. I think it is a sweet spot.]
Not as feature-rich as XML, I think the intent is to keep JSON per se as simple as it is, and layer conventions using JSON itself to represent more complex information (datetime, location, customer, etc.) and "metadata" (version, author, etc.). Don't let the "O" in JSON throw you. An "Object" in JSON is simply a dictionary in Python (and Map in Java), i.e. a set of name/value pairs.

https://sourceforge.net/projects/json-py

5 comments:

Anonymous said...

All JSON is valid YAML -- cool!

Ian Bicking said...

unittest? This sounds like classic doctest material.

Anonymous said...

I like it and may make an excuse soon to use JSON. Kudos on the work!

One thing - I may be missing something obvious, but why doesn't it accept tuples?

Anonymous said...

I make cherryshell python interpreter with jsonrpc and cherrypy. I use jsolait javascript library. And json implementation from this author http://jsolait.net
CherryShell and also jsonrpc demo can be found here http://john.thetrotter.net

I make it 2 weeks ago

Patrick Logan said...

I'll have to check out cherryshell.

I looked at pyjson from the jsonrpc stuff and found it not as clean, simple, or complete as what I had hoped. That was my motivation for writing json-py. Perhaps it could be substituted w/out much effort.

Blog Archive

About Me

Portland, Oregon, United States
I'm usually writing from my favorite location on the planet, the pacific northwest of the u.s. I write for myself only and unless otherwise specified my posts here should not be taken as representing an official position of my employer. Contact me at my gee mail account, username patrickdlogan.