Table of Contents

cJson.ahk [v1][v2]

The first and only AutoHotkey JSON library to use embedded compiled C for high performance.

Compatibility

This library is compatible with AutoHotkey v2.0, and older versions are available for v1.1 U64 and U32.

Using cJson

Converting an AHK Object to JSON:

AutoHotkey v2.0

AutoHotkey v1.1

Converting JSON to an AHK Object:

AutoHotkey v2.0

AutoHotkey v1.1

Notes

Data Types

AutoHotkey does not provide types that uniquely identify all the possible values that may be encoded or decoded. To work around this problem, cJson provides magic objects that give you greater control over how things are encoded. By default, cJson will behave according to the following table:

AutoHotkey v2.0

Value Encodes as Decodes as
true 1 1 *
false 0 0 *
null N/A "" *
0.10.10000000000000001 0.10000000000000001
JSON.True true N/A
JSON.False false N/A
JSON.Null null N/A

* To avoid type data loss when decoding true and false, the class property JSON.BoolsAsInts can be set := false. Once set, boolean true and false will decode to JSON.True and JSON.False respectively. Similarly, for Nulls JSON.NullsAsStrings can be set := false. Once set, null will decode to JSON.Null.

† In AutoHotkey, numbers with a fractional component are represented internally as double-precision floating point values. Floating point values are effectively a base-2 fraction, and just like how not all base-10 fractions can convert cleanly into base-10 decimals (see: 2/3 to 0.333...) not all base-2 fractions can convert cleanly into base-10 decimals. This results in situations where you write a simple base-10 decimal of 0.1, but it shows as a really ugly 0.10000000000000001 when your code displays it. Although AutoHotkey v1 used a bunch of tricks to hide this imprecision, such as by rounding aggressively to six places and by storing decimal values written in your code as strings until used for calculation, AutoHotkey v2 does not try to hide this. Similarly, cJson does not either.

AutoHotkey v1.1

Value Encodes as Decodes as
true 1 1 *
false 0 0 *
null N/A "" *
0.5"0.5" 0.500000
0.5+00.500000 N/A
JSON.True true N/A
JSON.False false N/A
JSON.Null null N/A

* To avoid type data loss when decoding true and false, the class property JSON.BoolsAsInts can be set := false. Once set, boolean true and false will decode to JSON.True and JSON.False respectively. Similarly, for Nulls JSON.NullsAsStrings can be set := false. Once set, null will decode to JSON.Null.

† Pure floats, as generated by an expression, will encode as floats. Hybrid floats that contain a string buffer will encode as strings. Floats hard-coded into a script are saved by AHK as hybrid floats. To force encoding as a float, perform some redundant operation like adding zero.

Array Detection

AutoHotkey v2.0

AutoHotkey v2.0 has separate Array and Map objects, and those will convert to JSON Arrays and Maps correctly.

AutoHotkey v1.1

AutoHotkey v1.1 makes no internal distinction between indexed-sequential arrays and keyed objects. As a result, this distinction must be chosen heuristically by the cJson library. If an object contains only sequential integer keys starting at 1, it will be rendered as an array. Otherwise, it will be rendered as an object.

Roadmap


Download cJson.ahk