=encoding utf-8


=head1 Name

lua-cjson - Fast JSON encoding/parsing



This fork of L<mpxE<sol>lua-cjson|https://github.com/mpx/lua-cjson> is included in
the L<OpenResty|https://openresty.org/> bundle and includes a few bugfixes and
improvements, especially to facilitate the encoding of empty tables as JSON Arrays.

Please refer to the L<lua-cjson documentation|http://www.kyne.com.au/~mark/software/lua-cjson.php>
for standard usage, this README only provides informations regarding this fork's additions.

See L<`mpxE<sol>master..openrestyE<sol>master`|https://github.com/mpx/lua-cjson/compare/master...openresty:master>
for the complete history of changes.




=head1 Additions


=head2 encode_empty_table_as_object

B<syntax:> C<cjson.encode_empty_table_as_object(true|false|"on"|"off")>

Change the default behavior when encoding an empty Lua table.

By default, empty Lua tables are encoded as empty JSON Objects (C<{}>). If this is set to false,
empty Lua tables will be encoded as empty JSON Arrays instead (C<[]>).

This method either accepts a boolean or a string (C<"on">, C<"off">).




=head2 empty_array

B<syntax:> C<cjson.empty_array>

A lightuserdata, similar to C<cjson.null>, which will be encoded as an empty JSON Array by
C<cjson.encode()>.

For example, since C<encode_empty_table_as_object> is C<true> by default:


    local cjson = require "cjson"
    
    local json = cjson.encode({
        foo = "bar",
        some_object = {},
        some_array = cjson.empty_array
    })

This will generate:


    {
        "foo": "bar",
        "some_object": {},
        "some_array": []
    }




=head2 empty_array_mt

B<syntax:> C<setmetatable({}, cjson.empty_array_mt)>

A metatable which can "tag" a table as a JSON Array in case it is empty (that is, if the
table has no elements, C<cjson.encode()> will encode it as an empty JSON Array).

Instead of:


    local function serialize(arr)
        if #arr < 1 then
            arr = cjson.empty_array
        end
    
        return cjson.encode({some_array = arr})
    end

This is more concise:


    local function serialize(arr)
        setmetatable(arr, cjson.empty_array_mt)
    
        return cjson.encode({some_array = arr})
    end

Both will generate:


    {
        "some_array": []
    }




=head2 encode_number_precision

B<syntax:> C<cjson.encode_number_precision(precision)>

This fork allows encoding of numbers with a C<precision> up to 16 decimals (vs. 14 in mpx/lua-cjson).


