My favorites | Sign in
Project Home Downloads Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
ImplementationNotes  
Notes on the cross-language code structure.
Updated Sep 27, 2009 by gtempacc...@yahoo.com

When writing JSON Template, and especially when doing the JavaScript port, I realized that it makes a good Code Kata. (OK that name is a little silly, but the concept is valid).

Structure

This piece of code uses many programming fundamentals. If you're learning a new language (as I was with JavaScript), then implementing JSON Template will be enlightening.

  • The template is tokenized using a regular expression. (This makes it fast and saves a lot code size. Note that this is correct due to the minimalistic design of the language.)
  • It uses nice object-oriented encapsulation.
  • For example, the ProgramBuilder class receives method calls from the parser, and uses an internal stack to build a tree. (Python only; I didn't port this refactoring to the JavaScript version)
  • The ScopedContext class wraps a data dictionary and receives method calls from the compiled program, in order to change the block context (PushSection, Pop and __iter__). It also has an internal stack.
  • The evaluator or "interpreter" uses mutually recursive functions to expand the template (Execute and family)
  • A user-defined dictionary of functions is used to specify the formatters (this will have different translations in different languages; in Python and JavaScript it's the same)
  • The JavaScript version uses closures instead of objects. This was very delightful to discover. Props to Douglas Crockford for showing us the light. My mouth is agape that the Rhino Book, touted as the definitive guide to JavaScript for the decade, has less than a page on closures, and it's presented as a misfeature of JavaScript! For shame.

All this in 500-ish lines, in a single file, with no dependencies! I would like to see implementations in other languages; please send me patches (via http://codereview.appspot.com/)

Comment by timgilbert, Apr 6, 2009

(Hope this is an OK place to post this...)

As a point of interest, I have been able to run all the tests successfully with Jython 2.5b3, although I did need to take a few extra steps:

1. simplejson is not installed by default in jython 2.5b3. I downloaded it from pypi, ran jython setup.py install, and the import worked fine.

2. jython ships with a java module. ;) Once I commented out the relevant import statements and associated stuff from jsontemplate_test.py the script ran successfully:

Running tests
Test <__main__.TokenizeTest object at 0x1>
..Test <__main__.FromStringTest object at 0x2>
....Test <__main__.InternalTemplateTest object at 0x3>
........Test <__main__.Template2Test object at 0x9>
.................................
Summary
-------
Unexpected errors: 0
Failing: 0
Passing: 47

If I wind up messing around with jython any more I might submit a patch (jython should be able to implement java.verifier a little more cleanly, for one thing). In the meantime I'm considering porting json-template to scala.

Powered by Google Project Hosting