{"id":13492100,"url":"https://github.com/openresty/lua-cjson","last_synced_at":"2025-03-28T09:33:45.622Z","repository":{"id":14257018,"uuid":"16964589","full_name":"openresty/lua-cjson","owner":"openresty","description":"Lua CJSON is a fast JSON encoding/parsing module for Lua","archived":false,"fork":true,"pushed_at":"2024-03-01T01:18:48.000Z","size":396,"stargazers_count":407,"open_issues_count":15,"forks_count":112,"subscribers_count":24,"default_branch":"master","last_synced_at":"2024-03-01T02:33:01.255Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://www.kyne.com.au/~mark/software/lua-cjson.php","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"mpx/lua-cjson","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/openresty.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-02-18T21:37:03.000Z","updated_at":"2024-02-27T08:22:15.000Z","dependencies_parsed_at":"2023-02-10T21:16:24.845Z","dependency_job_id":null,"html_url":"https://github.com/openresty/lua-cjson","commit_stats":null,"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openresty%2Flua-cjson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openresty%2Flua-cjson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openresty%2Flua-cjson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openresty%2Flua-cjson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/openresty","download_url":"https://codeload.github.com/openresty/lua-cjson/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246004115,"owners_count":20708134,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T19:01:03.109Z","updated_at":"2025-03-28T09:33:45.272Z","avatar_url":"https://github.com/openresty.png","language":"C","funding_links":[],"categories":["C","Libraries"],"sub_categories":[],"readme":"Name\n====\n\nlua-cjson - Fast JSON encoding/parsing\n\nTable of Contents\n=================\n\n* [Name](#name)\n* [Description](#description)\n* [Additions to mpx/lua](#additions)\n    * [encode_empty_table_as_object](#encode_empty_table_as_object)\n    * [empty_array](#empty_array)\n    * [array_mt](#array_mt)\n    * [empty_array_mt](#empty_array_mt)\n    * [encode_number_precision](#encode_number_precision)\n    * [encode_escape_forward_slash](#encode_escape_forward_slash)\n    * [encode_skip_unsupported_value_types](#encode_skip_unsupported_value_types)\n    * [decode_array_with_array_mt](#decode_array_with_array_mt)\n\nDescription\n===========\n\nThis fork of [mpx/lua-cjson](https://github.com/mpx/lua-cjson) is included in\nthe [OpenResty](https://openresty.org/) bundle and includes a few bugfixes and\nimprovements, especially to facilitate the encoding of empty tables as JSON Arrays.\n\nPlease refer to the [lua-cjson documentation](https://kyne.au/~mark/software/lua-cjson.php)\nfor standard usage, this README only provides informations regarding this fork's additions.\n\nSee [`mpx/master..openresty/master`](https://github.com/mpx/lua-cjson/compare/master...openresty:master)\nfor the complete history of changes.\n\n[Back to TOC](#table-of-contents)\n\nAdditions\n=========\n\nencode_empty_table_as_object\n----------------------------\n**syntax:** `cjson.encode_empty_table_as_object(true|false|\"on\"|\"off\")`\n\nChange the default behavior when encoding an empty Lua table.\n\nBy default, empty Lua tables are encoded as empty JSON Objects (`{}`). If this is set to false,\nempty Lua tables will be encoded as empty JSON Arrays instead (`[]`).\n\nThis method either accepts a boolean or a string (`\"on\"`, `\"off\"`).\n\n[Back to TOC](#table-of-contents)\n\nempty_array\n-----------\n**syntax:** `cjson.empty_array`\n\nA lightuserdata, similar to `cjson.null`, which will be encoded as an empty JSON Array by\n`cjson.encode()`.\n\nFor example, since `encode_empty_table_as_object` is `true` by default:\n\n```lua\nlocal cjson = require \"cjson\"\n\nlocal json = cjson.encode({\n    foo = \"bar\",\n    some_object = {},\n    some_array = cjson.empty_array\n})\n```\n\nThis will generate:\n\n```json\n{\n    \"foo\": \"bar\",\n    \"some_object\": {},\n    \"some_array\": []\n}\n```\n\n[Back to TOC](#table-of-contents)\n\narray_mt\n--------\n**syntax:** `setmetatable({}, cjson.array_mt)`\n\nWhen lua-cjson encodes a table with this metatable, it will systematically\nencode it as a JSON Array. The resulting, encoded Array will contain the array\npart of the table, and will be of the same length as the `#` operator on that\ntable. Holes in the table will be encoded with the `null` JSON value.\n\nExample:\n\n```lua\nlocal t = { \"hello\", \"world\" }\nsetmetatable(t, cjson.array_mt)\ncjson.encode(t) -- [\"hello\",\"world\"]\n```\n\nOr:\n\n```lua\nlocal t = {}\nt[1] = \"one\"\nt[2] = \"two\"\nt[4] = \"three\"\nt.foo = \"bar\"\nsetmetatable(t, cjson.array_mt)\ncjson.encode(t) -- [\"one\",\"two\",null,\"three\"]\n```\n\nThis value was introduced in the `2.1.0.5` release of this module.\n\n[Back to TOC](#table-of-contents)\n\nempty_array_mt\n--------------\n**syntax:** `setmetatable({}, cjson.empty_array_mt)`\n\nA metatable which can \"tag\" a table as a JSON Array in case it is empty (that is, if the\ntable has no elements, `cjson.encode()` will encode it as an empty JSON Array).\n\nInstead of:\n\n```lua\nlocal function serialize(arr)\n    if #arr \u003c 1 then\n        arr = cjson.empty_array\n    end\n\n    return cjson.encode({some_array = arr})\nend\n```\n\nThis is more concise:\n\n```lua\nlocal function serialize(arr)\n    setmetatable(arr, cjson.empty_array_mt)\n\n    return cjson.encode({some_array = arr})\nend\n```\n\nBoth will generate:\n\n```json\n{\n    \"some_array\": []\n}\n```\n\n[Back to TOC](#table-of-contents)\n\nencode_number_precision\n-----------------------\n**syntax:** `cjson.encode_number_precision(precision)`\n\nThis fork allows encoding of numbers with a `precision` up to 16 decimals (vs. 14 in mpx/lua-cjson).\n\n[Back to TOC](#table-of-contents)\n\nencode_escape_forward_slash\n---------------------------\n**syntax:** `cjson.encode_escape_forward_slash(enabled)`\n\n**default:** true\n\nIf enabled, forward slash '/' will be encoded as '\\\\/'.\n\nIf disabled, forward slash '/' will be encoded as '/' (no escape is applied).\n\n[Back to TOC](#table-of-contents)\n\nencode_skip_unsupported_value_types\n---------------------------\n**syntax:** `cjson.encode_skip_unsupported_value_types(enabled)`\n\n**default:** false\n\nIf enabled, cjson will not throw exception when there are unsupported types\nin the Lua table.\n\nFor example:\n\n```lua\nlocal ffi = require \"ffi\"\nlocal cjson = require \"cjson\"\ncjson.encode_skip_unsupported_value_types(true)\nlocal t = {key = \"val\"}\n\nt.cdata = ffi.new(\"char[?]\", 100)\nprint(cjson.encode(t))\n```\n\nThis will generate:\n\n```json\n{\"key\":\"val\"}\n```\n\n[Back to TOC](#table-of-contents)\n\ndecode_array_with_array_mt\n--------------------------\n**syntax:** `cjson.decode_array_with_array_mt(enabled)`\n\n**default:** false\n\nIf enabled, JSON Arrays decoded by `cjson.decode` will result in Lua\ntables with the [`array_mt`](#array_mt) metatable. This can ensure a 1-to-1\nrelationship between arrays upon multiple encoding/decoding of your\nJSON data with this module.\n\nIf disabled, JSON Arrays will be decoded to plain Lua tables, without\nthe `array_mt` metatable.\n\nThe `enabled` argument is a boolean.\n\nExample:\n\n```lua\nlocal cjson = require \"cjson\"\n\n-- default behavior\nlocal my_json = [[{\"my_array\":[]}]]\nlocal t = cjson.decode(my_json)\ncjson.encode(t) -- {\"my_array\":{}} back to an object\n\n-- now, if this behavior is enabled\ncjson.decode_array_with_array_mt(true)\n\nlocal my_json = [[{\"my_array\":[]}]]\nlocal t = cjson.decode(my_json)\ncjson.encode(t) -- {\"my_array\":[]} properly re-encoded as an array\n```\n\n[Back to TOC](#table-of-contents)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenresty%2Flua-cjson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopenresty%2Flua-cjson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenresty%2Flua-cjson/lists"}