= Lua CJSON 2.1devel Manual =
Mark Pulford <mark@kyne.com.au>
:revdate: 1st March 2012
Overview
--------
The Lua CJSON module provides JSON support for Lua.
*Features*::
- Fast, standards compliant encoding/parsing routines
- Full support for JSON with UTF-8, including decoding surrogate pairs
- Optional run-time support for common exceptions to the JSON
specification (infinity, NaN,..)
- No dependencies on other libraries
*Caveats*::
- UTF-16 and UTF-32 are not supported
Lua CJSON is covered by the MIT license. Review the file +LICENSE+ for
details.
The latest version of this software is available from the
http://www.kyne.com.au/%7Emark/software/lua-cjson.php[Lua CJSON website].
Feel free to email me if you have any patches, suggestions, or comments.
Installation
------------
Lua CJSON requires either http://www.lua.org[Lua] 5.1, Lua 5.2, or
http://www.luajit.org[LuaJIT] to build.
The build method can be selected from 4 options:
Make:: Unix (including Linux, BSD, Mac OSX & Solaris), Windows
CMake:: Unix, Windows
RPM:: Linux
LuaRocks:: Unix, Windows
Make
~~~~
The included +Makefile+ has generic settings.
First, review and update the included makefile to suit your platform (if
required).
Next, build and install the module:
[source,sh]
make install
Or install manually into your Lua module directory:
[source,sh]
make
cp cjson.so $LUA_MODULE_DIRECTORY
CMake
~~~~~
http://www.cmake.org[CMake] can generate build configuration for many
different platforms (including Unix and Windows).
First, generate the makefile for your platform using CMake. If CMake is
unable to find Lua, manually set the +LUA_DIR+ environment variable to
the base prefix of your Lua 5.1 installation.
While +cmake+ is used in the example below, +ccmake+ or +cmake-gui+ may
be used to present an interface for changing the default build options.
[source,sh]
mkdir build
cd build
# Optional: export LUA_DIR=$LUA51_PREFIX
cmake ..
Next, build and install the module:
[source,sh]
make install
# Or:
make
cp cjson.so $LUA_MODULE_DIRECTORY
Review the
http://www.cmake.org/cmake/help/documentation.html[CMake documentation]
for further details.
RPM
~~~
Linux distributions using http://rpm.org[RPM] can create a package via
the included RPM spec file. Ensure the +rpm-build+ package (or similar)
has been installed.
Build and install the module via RPM:
[source,sh]
rpmbuild -tb lua-cjson-2.1devel.tar.gz
rpm -Uvh $LUA_CJSON_RPM
LuaRocks
~~~~~~~~
http://luarocks.org[LuaRocks] can be used to install and manage Lua
modules on a wide range of platforms (including Windows).
First, extract the Lua CJSON source package.
Next, install the module:
[source,sh]
cd lua-cjson-2.1devel
luarocks make
[NOTE]
LuaRocks does not support platform specific configuration for Solaris.
On Solaris, you may need to manually uncomment +USE_INTERNAL_ISINF+ in
the rockspec before building this module.
Review the http://luarocks.org/en/Documentation[LuaRocks documentation]
for further details.
[[build_options]]
Build Options (#define)
~~~~~~~~~~~~~~~~~~~~~~~
Lua CJSON offers several +#define+ build options to address portability
issues, and enable non-default features. Some build methods may
automatically set platform specific options if required. Other features
should be enabled manually.
USE_INTERNAL_ISINF:: Workaround for Solaris platforms missing +isinf+.
DISABLE_INVALID_NUMBERS:: Recommended on platforms where +strtod+ /
+sprintf+ are not POSIX compliant (eg, Windows MinGW). Prevents
+cjson.encode_invalid_numbers+ and +cjson.decode_invalid_numbers+ from
being enabled. However, +cjson.encode_invalid_numbers+ may still be
set to +"null"+. When using the Lua CJSON built-in floating point
conversion this option is unnecessary and is ignored.
Built-in floating point conversion
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Lua CJSON may be built with David Gay's
http://www.netlib.org/fp/[floating point conversion routines]. This can
increase overall performance by up to 50% on some platforms when
converting a large amount of numeric data. However, this option reduces
portability and is disabled by default.
USE_INTERNAL_FPCONV:: Enable internal number conversion routines.
IEEE_BIG_ENDIAN:: Must be set on big endian architectures.
MULTIPLE_THREADS:: Must be set if Lua CJSON may be used in a
multi-threaded application. Requires the _pthreads_ library.
API (Functions)
---------------
Synopsis
~~~~~~~~
[source,lua]
------------
-- Module instantiation
local cjson = require "cjson"
local cjson2 = cjson.new()
local cjson_safe = require "cjson.safe"
-- Translate Lua value to/from JSON
text = cjson.encode(value)
value = cjson.decode(text)
-- Get and/or set Lua CJSON configuration
setting = cjson.decode_invalid_numbers([setting])
setting = cjson.encode_invalid_numbers([setting])
keep = cjson.encode_keep_buffer([keep])
depth = cjson.encode_max_depth([depth])
depth = cjson.decode_max_depth([depth])
convert, ratio, safe = cjson.encode_sparse_array([convert[, ratio[, safe]]])
------------
Module Instantiation
~~~~~~~~~~~~~~~~~~~~
[source,lua]
------------
local cjson = require "cjson"
local cjson2 = cjson.new()
local cjson_safe = require "cjson.safe"
------------
Import Lua CJSON via the Lua +require+ function. Lua CJSON does not
register a global module table.
The +cjson+ module will throw an error during JSON conversion if any
invalid data is encountered. Refer to <<cjson_encode,+cjson.encode+>>
and <<cjson_decode,+cjson.decode+>> for details.
The +cjson.safe+ module behaves identically to the +cjson+ module,
except when errors are encountered during JSON conversion. On error, the
+cjson_safe.encode+ and +cjson_safe.decode+ functions will return
+nil+ followed by the error message.
+cjson.new+ can be used to instantiate an independent copy of the Lua
CJSON module. The new module has a separate persistent encoding buffer,
and default settings.
Lua CJSON can support Lua implementations using multiple preemptive
threads within a single Lua state provided the persistent encoding
buffer is not shared. This can be achieved by one of the following
methods:
- Disabling the persistent encoding buffer with
<<encode_keep_buffer,+cjson.encode_keep_buffer+>>
- Ensuring each thread calls <<encode,+cjson.encode+>> separately (ie,
treat +cjson.encode+ as non-reentrant).
- Using a separate +cjson+ module table per preemptive thread
(+cjson.new+)
[NOTE]
Lua CJSON uses +strtod+ and +snprintf+ to perform numeric conversion as
they are usually well supported, fast and bug free. However, these
functions require a workaround for JSON encoding/parsing under locales
using a comma decimal separator. Lua CJSON detects the current locale
during instantiation to determine and automatically implement the
workaround if required. Lua CJSON should be reinitialised via
+cjson.new+ if the locale of the current process changes. Using a
different locale per thread is not supported.
decode
~~~~~~
[source,lua]
------------
value = cjson.decode(json_text)
------------
+cjson.decode+ will deserialise any UTF-8 JSON string into a Lua value
or table.
UTF-16 and UTF-32 JSON strings are not supported.
+cjson.decode+ requires that any NULL (ASCII 0) and double quote (ASCII
34) characters are escaped within strings. All escape codes will be
decoded and other bytes will be passed transparently. UTF-8 characters
are not validated during decoding and should be checked elsewhere if
required.
JSON +null+ will be converted to a NULL +lightuserdata+ value. This can
be compared with +cjson.null+ for convenience.
By default, numbers incompatible with the JSON specification (infinity,
NaN, hexadecimal) can be decoded. This default can be changed with
<<decode_invalid_numbers,+cjson.decode_invalid_numbers+>>.
.Example: Decoding
[source,lua]
json_text = '[ true, { "foo": "bar" } ]'
value = cjson.decode(json_text)
-- Returns: { true, { foo = "bar" } }
[CAUTION]
Care must be taken after decoding JSON objects with numeric keys.
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
openresty-1.15.8.3.tar.gz (2000个子文件)
action_macros 2KB
action_replacements 77B
action_types 326B
array 535B
auto_config 511B
autogen 320B
build 12KB
ngx_stream_lua_lex.c 179KB
ngx_http_lua_lex.c 179KB
minilua.c 170KB
ngx_http_lua_socket_tcp.c 169KB
ngx_http_upstream.c 162KB
ngx_stream_lua_socket_tcp.c 152KB
ngx_http_core_module.c 138KB
ngx_http_grpc_module.c 132KB
ngx_http_v2.c 130KB
ngx_http_v2_huff_decode.c 126KB
ngx_http_proxy_module.c 121KB
ngx_event_openssl.c 115KB
ngx_http_lua_util.c 114KB
ngx_resolver.c 110KB
ngx_http_fastcgi_module.c 107KB
ngx_http_mp4_module.c 103KB
ngx_stream_lua_util.c 95KB
ngx_http_request.c 92KB
dtoa.c 86KB
lj_record.c 85KB
lj_parse.c 78KB
ngx_http_ssi_filter_module.c 78KB
ngx_stream_lua_shdict.c 75KB
ngx_http_lua_shdict.c 74KB
ngx_http_uwsgi_module.c 71KB
lj_asm.c 71KB
ngx_http_lua_pipe.c 70KB
lj_opt_fold.c 69KB
ngx_stream_lua_regex.c 68KB
ngx_http_file_cache.c 68KB
ngx_http_lua_regex.c 68KB
ngx_http_variables.c 64KB
ngx_stream_proxy_module.c 64KB
lj_crecord.c 60KB
ngx_http_scgi_module.c 59KB
ngx_http_parse.c 58KB
ngx_http_v2_filter_module.c 56KB
lj_cparse.c 56KB
ngx_http.c 51KB
ngx_http_log_module.c 49KB
ngx_http_lua_subrequest.c 49KB
ngx_stream_lua_socket_udp.c 48KB
ngx_http_lua_directive.c 47KB
ngx_event_openssl_stapling.c 45KB
lua_cjson.c 45KB
ngx_mail_auth_http_module.c 44KB
ngx_string.c 44KB
ngx_http_geo_module.c 43KB
ngx_http_lua_socket_udp.c 43KB
ngx_http_memc_response.c 42KB
ngx_http_script.c 42KB
ngx_http_lua_module.c 42KB
ngx_http_image_filter_module.c 42KB
ngx_stream_geo_module.c 42KB
lj_alloc.c 41KB
ngx_stream_log_module.c 41KB
ngx_postgres_module.c 40KB
ngx_http_charset_filter_module.c 40KB
ngx_connection.c 40KB
lj_ffrecord.c 40KB
nginx.c 38KB
ngx_http_lua_headers.c 37KB
ngx_cycle.c 37KB
ngx_process_cycle.c 37KB
ngx_http_srcache_util.c 36KB
lj_ccall.c 35KB
ngx_event.c 34KB
ngx_conf_file.c 34KB
ngx_stream_lua_directive.c 33KB
ngx_http_gzip_filter_module.c 32KB
ngx_http_dav_module.c 31KB
lj_opt_mem.c 31KB
ngx_http_ssl_module.c 30KB
ngx_open_file_cache.c 30KB
lj_api.c 30KB
ngx_http_autoindex_module.c 30KB
ngx_inet.c 30KB
ngx_mail_proxy_module.c 30KB
ngx_stream_variables.c 30KB
ngx_stream_lua_ssl_certby.c 29KB
ngx_http_lua_req_body.c 29KB
ngx_http_xslt_filter_module.c 29KB
ngx_http_lua_ssl_certby.c 29KB
ngx_http_request_body.c 29KB
ngx_http_redis_module.c 29KB
lj_snap.c 28KB
ngx_http_rds_json_output.c 28KB
ngx_http_drizzle_upstream.c 28KB
ngx_stream_lua_module.c 28KB
ngx_http_rewrite_module.c 28KB
ngx_stream_lua_clfactory.c 27KB
ngx_http_lua_clfactory.c 27KB
ngx_http_drizzle_util.c 27KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
saikey0379
- 粉丝: 290
- 资源: 16
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功