<commit_before>[![Build Status](https://travis-ci.org/flightaware/yajl-tcl.svg?branch=master)](https://travis-ci.org/flightaware/yajl-tcl)

### This is yajl-tcl, a direct Tcl interface to the yajl JSON generator library.

*Version 1.7.0*

This package is a freely available open source package under the "Berkeley" license, same as Tcl.  You can do virtually anything you like with it, such as modifying it, redistributing it, and selling it either in whole or in part. See the file "LICENSE" for complete information.

yajl-tcl was written by <NAME> of FlightAware.  This company-funded work was generously open-sourced.

Using yajl-tcl
-----------

```tcl
package require yajltcl
```

Example
===========

Create a yajl-tcl object...

```tcl
yajl create x -beautify 1
```

or

```tcl
set x [yajl create #auto]
```

then generate some json

```tcl
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]
```

produces...

```javascript
{
	"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"
			}
		}
	]
}
```

```tcl
x reset - prepare the object for reuse
```

or

```tcl
x delete - delete the object and all of its internal data
```

Yajl-tcl Quick Reference
==================

Create the object as above.

Invoke the object with one or more methods. They are invoked left to right. Some have no arguments and some have one.
 
Methods
-----------

```array_open```, ```array_close```, bool, clear, double, integer, map_close, map_open, null, number, string, free, get, reset, or delete

```array_open``` - start an array

```array_close``` - end an array

```clear``` - clears the buffer but doesn't reset the parser's state (not sure how useful this is)

```bool``` - add a boolean, value follows

```double``` - add a double precision floating point value, value follows

```integer``` - add an integer value, value follows

```number``` - add a numeric value, value follows

Note that with respect to "double" yajl internally formats that only with "%g"
providing six digits of precision and this is not currently configurable 
via YAJL.  If you need higher precision, use "format" or equivalent, coupled 
with yajl-tcl's "number" method.

```map_open``` - open a map

```map_close``` - close a map

```null``` - insert a null value

```free``` - nothing yet

```get``` - get the JSON generated so far.  clears the generator buffer.  maintains
state so you can keep going, i.e. stream it.

```reset``` - free, reallocate and configure the yajl generator object for reuse

```delete``` - delete the object and all of its internal data

Parsing
=======

As of version 1.2, yajl-tcl can also parse.

```tcl
set list [$object parse $json]
```

List will be as above.

Alternatively, you can use ```::yajl::json2dict``` to turn JSON into a key-value
list that be loaded into an array.  As an example, the JSON from the prior
section can be parsed using code similar to the following:

```tcl
set jsonkv [::yajl::json2dict $json]
puts "raw = $jsonkv"
array set myArray $jsonkv
puts "outerType = $myArray(type)"
foreach featurekv $myArray(features) {
    unset -nocomplain featureAry
    array set featureAry $featurekv
    puts "innerType = $featureAry(type), geom = $featureAry(geometry)"
}
```

That will output the following:

```javascript
raw = 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}}}
outerType = FeatureCollection
innerType = Feature, geom = type Point coordinates {-106.6091944 35.0401944}
innerType = Feature, geom = type Point coordinates {-118.3586667 34.206667}
```

A slight variation of json2dict is ```yajl::json2dict_ex```.  This version turns JSON into a fully traversable Tcl dictionary by virtue of that it expands JSON arrays into key-value pairs, as might be done with a map, where the keys are synthetized sequential integers starting from zero.

After having parsed a list of channels returned from the Slack API, for instance, to fetch the ID of the third member of the fourth channel you might say ```dict get $dict channels 3 members 2```.  See the tests file json2dict_ex.test included in the distro for a nice big JSON structure that hopefully will make clear what's going on.

Parsing to huddle
=================
<commit_msg>fix typo<commit_after>[![Build Status](https://travis-ci.org/flightaware/yajl-tcl.svg?branch=master)](https://travis-ci.org/flightaware/yajl-tcl)

### This is yajl-tcl, a direct Tcl interface to the yajl JSON generator library.

*Version 1.7.0*

This package is a freely available open source package under the "Berkeley" license, same as Tcl.  You can do virtually anything you like with it, such as modifying it, redistributing it, and selling it either in whole or in part. See the file "LICENSE" for complete information.

yajl-tcl was written by <NAME> of FlightAware.  This company-funded work was generously open-sourced.

Using yajl-tcl
-----------

```tcl
package require yajltcl
```

Example
===========

Create a yajl-tcl object...

```tcl
yajl create x -beautify 1
```

or

```tcl
set x [yajl create #auto]
```

then generate some json

```tcl
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]
```

produces...

```javascript
{
	"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"
			}
		}
	]
}
```

```tcl
x reset - prepare the object for reuse
```

or

```tcl
x delete - delete the object and all of its internal data
```

Yajl-tcl Quick Reference
==================

Create the object as above.

Invoke the object with one or more methods. They are invoked left to right. Some have no arguments and some have one.
 
Methods
-----------

```array_open```, ```array_close```, bool, clear, double, integer, map_close, map_open, null, number, string, free, get, reset, or delete

```array_open``` - start an array

```array_close``` - end an array

```clear``` - clears the buffer but doesn't reset the parser's state (not sure how useful this is)

```bool``` - add a boolean, value follows

```double``` - add a double precision floating point value, value follows

```integer``` - add an integer value, value follows

```number``` - add a numeric value, value follows

Note that with respect to "double" yajl internally formats that only with "%g"
providing six digits of precision and this is not currently configurable 
via YAJL.  If you need higher precision, use "format" or equivalent, coupled 
with yajl-tcl's "number" method.

```map_open``` - open a map

```map_close``` - close a map

```null``` - insert a null value

```free``` - nothing yet

```get``` - get the JSON generated so far.  clears the generator buffer.  maintains
state so you can keep going, i.e. stream it.

```reset``` - free, reallocate and configure the yajl generator object for reuse

```delete``` - delete the object and all of its internal data

Parsing
=======

As of version 1.2, yajl-tcl can also parse.

```tcl
set list [$object parse $json]
```

List will be as above.

Alternatively, you can use ```::yajl::json2dict``` to turn JSON into a key-value
list that be loaded into an array.  As an example, the JSON from the prior
section can be parsed using code similar to the following:

```tcl
set jsonkv [::yajl::json2dict $json]
puts "raw = $jsonkv"
array set myArray $jsonkv
puts "outerType = $myArray(type)"
foreach featurekv $myArray(features) {
    unset -nocomplain featureAry
    array set featureAry $featurekv
    puts "innerType = $featureAry(type), geom = $featureAry(geometry)"
}
```

That will output the following:

```javascript
raw = 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}}}
outerType = FeatureCollection
innerType = Feature, geom = type Point coordinates {-106.6091944 35.0401944}
innerType = Feature, geom = type Point coordinates {-118.3586667 34.206667}
```

A slight variation of json2dict is ```yajl::json2dict_ex```.  This version turns JSON into a fully traversable Tcl dictionary by virtue of that it expands JSON arrays into key-value pairs, as might be done with a map, where the keys are synthesized sequential integers starting from zero.

After having parsed a list of channels returned from the Slack API, for instance, to fetch the ID of the third member of the fourth channel you might say ```dict get $dict channels 3 members 2```.  See the tests file json2dict_ex.test included in the distro for a nice big JSON structure that hopefully will make clear what's going on.

Parsing to huddle
=================

{'fraction_non_alphanumeric': 0.11742976572372898, 'fraction_numerical': 0.031301642850199284, 'mean_word_length': 3.9844961240310077, 'pattern_counts': {'":': 28, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2905772', 'n_tokens_mistral': 3595, 'n_tokens_neox': 3098, 'n_words': 1317}