lua-resty-prettycjson
Lua cJSON 的 JSON 格式化工具
$ opm get bungle/lua-resty-prettycjson
lua-resty-prettycjson
lua-resty-prettycjson
是一个用于 Lua cJSON 的 JSON 格式化工具。
安装
只需将 `prettycjson.lua` 放置在你的 package.path
中的某个位置,最好放在 resty
目录下。如果你使用的是 OpenResty,默认位置为 /usr/local/openresty/lualib/resty
。
使用 OpenResty 包管理器 (opm)
$ opm get bungle/lua-resty-prettycjson
使用 LuaRocks 或 MoonRocks
如果你使用的是 LuaRocks >= 2.2
$ luarocks install lua-resty-prettycjson
如果你使用的是 LuaRocks < 2.2
$ luarocks install --server=http://rocks.moonscript.org moonrocks
$ moonrocks install lua-resty-prettycjson
lua-resty-prettycjson
的 MoonRocks 仓库位于:https://rocks.moonscript.org/modules/bungle/lua-resty-prettycjson。
Lua API
string function(dt, [lf = "\n", [id = "\t", [ac = " ", [ec = function]]]])
格式化 JSON 输出。如果你想使用与默认值 \n
不同的换行符,可以传递 lf
(换行符)。如果你想使用除 \t
(制表符) 之外的其他字符进行缩进 (id
参数),也可以将其作为参数传递。如果你想在冒号 :
后使用除
(空格) 之外的其他字符 (ac
参数),也可以更改它,例如尝试 \n
。如果你想使用除 cJSON 之外的其他编码器,请将编码函数作为第五个参数 (ec
) 传递。它应该接受任何内容作为输入参数,如果编码过程中出现问题,此函数应该返回 nil
和错误消息,例如
nil, "Cannot serialise function: type not supported"
对于输入参数 dt
,它接受 cjson.encode
接受的任何内容(或自定义编码函数接受的任何内容)。
示例
local pretty = require "resty.prettycjson"
print(pretty({
key1 = "data",
key2 = 27,
key3 = {
key3_1 = "something",
key3_2 = "something else"
},
key4 = {
"item1",
"item2"
},
key5 = {},
key5 = {{''}, {'',''}, {{},{}}},
key6 = { '' },
key7 = {{{{ test = "value", {{{{{{}}},{{},{},{}},{},{}}}}}}}}
}))
这将输出
{
"key6": [
""
],
"key3": {
"key3_1": "something",
"key3_2": "something else"
},
"key7": [
[
[
{
"1": [
[
[
[
[
{}
]
],
[
{},
{},
{}
],
{},
{}
]
]
],
"test": "value"
}
]
]
],
"key1": "data",
"key5": [
[
""
],
[
"",
""
],
[
{},
{}
]
],
"key2": 27,
"key4": [
"item1",
"item2"
]
}
变更
此模块每个版本的变更记录在 Changes.md 文件中。
许可证
lua-resty-prettycjson
使用三条款 BSD 许可证。
Copyright (c) 2015 — 2016, Aapo Talvensaari
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of lua-resty-prettycjson nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
作者
Aapo Talvensaari (@bungle)
许可证
3bsd
版本
-
Lua cJSON 的 JSON 格式化工具 2016-09-29 19:16:43