Fork me on GitHub
yajl-tcl
by flightaware
Tcl bindings for Yet Another JSON Library

Overview

This is yajl-tcl, a Tcl C extension written to bring the capabilities of the Yet Another JSON Library (YAJL) , a C library for parsing and generating JSON, available to Tcl coders. JSON stands for Javascript Object Notation, a popular data exchange format for Javascript. (For more info on JSON, visit json.org.)

Although there is already a package in the tcllib library, huddle, that can generate JSON, the big advantage in using yajl-tcl is performance. Also, yajl-tcl can parse JSON as well as generating it.

In a benchmark generating JSON representing 5,000 airports with code, name, latitude and longitude, yajl-tcl was 157 times faster than huddle.

Code

Get it on github: http://github.com/flightaware/yajl-tcl

Support

You can sometimes find yajl-tcl on IRC in the #tcl channel on the Freenode network or you can file issues with github here.

Download

You can also clone the project with Git by running:

$ git clone git://github.com/flightaware/yajl-tcl

Using yajl-tcl

package require yajltcl
    
Create a yajl-tcl object...
yajl create x -beautify 1
    
or
set x [yajl create #auto]
    
...then generate some json...
x map_open string type string FeatureCollection string features array_open

proc json_major_airport {obj icao lat lon} {
    $obj map_open string type string Feature string geometry \
	         map_open string type string Point string coordinates \
		         array_open number $lon number $lat \
				 array_close \
			 map_close string properties \
			 map_open string label string $icao map_close map_close
}

json_major_airport x KABQ 35.0401944 -106.6091944
json_major_airport x KBUR 34.206667 -118.3586667 

x array_close map_close

puts [x get]
    
...yielding...
{
	"type": "FeatureCollection",
	"features": [
		{
			"type": "Feature",
			"geometry": {
				"type": "Point",
				"coordinates": [
					-106.6091944,
					35.0401944
				]
			},
			"properties": {
				"label": "KABQ"
			}
		},
		{
			"type": "Feature",
			"geometry": {
				"type": "Point",
				"coordinates": [
					-118.3586667,
					34.206667
				]
			},
			"properties": {
				"label": "KBUR"
			}
		}
	]
}
	

Quick Reference

Generating

Create your yajl-tcl object as above then invoke the object with one or more methods. They are generated left to right. Some have no arguments and some have one.

The methods are: array_open, array_close, bool, clear, double, integer, map_close, map_open, null, number, string, free, get, and reset

Parsing

As of version 1.2, yajl-tcl can also parse...
set list [$object parse $json]
List will be as above.

yajl-tcl library routines

License

Copyright (c) 2010-2011, FlightAware LLC
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.

    * Neither the name of the FlightAware LLC nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.