├── Changes.md ├── LICENSE ├── README.md ├── dist.ini ├── lib └── resty │ └── prettycjson.lua └── lua-resty-prettycjson-dev-1.rockspec /Changes.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `lua-resty-prettycjson` will be documented in this file. 4 | 5 | ## [1.6] - 2016-09-29 6 | ### Added 7 | - Support for the official OpenResty package manager (opm). 8 | 9 | ### Changed 10 | - Changed the change log format to keep-a-changelog. 11 | 12 | ## [1.5] - 2016-03-23 13 | ### Fixed 14 | - Now works even if Lua cJSON is not available. 15 | 16 | ## [1.4] - 2016-04-23 17 | ### Added 18 | - Feature: Allow custom encoding function. See also: 19 | https://github.com/bungle/lua-resty-prettycjson/issues/3 20 | https://github.com/bungle/lua-resty-prettycjson/pull/4 21 | https://github.com/bungle/lua-resty-prettycjson/pull/5 22 | Thanks @vadi2 23 | 24 | ## [1.3] - 2015-09-30 25 | ### Changed 26 | - Only double quoted strings (JSON doesn't use single quotes) are 27 | checked now. 28 | 29 | ## [1.2] - 2015-09-29 30 | ### Fixed 31 | - Fixes string handling See also: 32 | https://github.com/bungle/lua-resty-prettycjson/issues/2 33 | Thanks @irmiab 34 | 35 | ### Changed 36 | - Uses now string.len / string.sub instead of string.gmatch See also: 37 | https://github.com/bungle/lua-resty-prettycjson/pull/1 38 | Thanks @antonheryanto 39 | 40 | ## [1.1] - 2015-09-29 41 | ### Changed 42 | - Removed unneccessary concat call. 43 | - Made code more tight 44 | 45 | ## [1.0] - 2015-09-28 46 | ### Added 47 | - LuaRocks support via MoonRocks. -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 — 2016, Aapo Talvensaari 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | * Neither the name of lua-resty-prettycjson nor the names of its 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 22 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 25 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # lua-resty-prettycjson 2 | 3 | `lua-resty-prettycjson` is a JSON Pretty Formatter for [Lua cJSON](http://www.kyne.com.au/~mark/software/lua-cjson.php). 4 | 5 | ## Installation 6 | 7 | Just place [`prettycjson.lua`](https://github.com/bungle/lua-resty-prettycjson/blob/master/lib/resty/prettycjson.lua) 8 | somewhere in your `package.path`, under `resty` directory. If you are using OpenResty, the default location 9 | would be `/usr/local/openresty/lualib/resty`. 10 | 11 | ### Using OpenResty Package Manager (opm) 12 | 13 | ```Shell 14 | $ opm get bungle/lua-resty-prettycjson 15 | ``` 16 | 17 | ### Using LuaRocks 18 | 19 | ```Shell 20 | $ luarocks install lua-resty-prettycjson 21 | ``` 22 | 23 | LuaRocks repository for `lua-resty-prettycjson` is located at https://luarocks.org/modules/bungle/lua-resty-prettycjson. 24 | 25 | ## Lua API 26 | #### string function(dt, [lf = "\n", [id = "\t", [ac = " ", [ec = function]]]]) 27 | 28 | Pretty formats the JSON output. You may pass `lf` (line feed) if you want to use different linefeed 29 | than the default `\n`. If you want to indent (` id` argument) with something else than `\t` (a tab) 30 | you can pass that as arguments as well. And if you want to have something else than ` ` (single space) after 31 | colons `:` (`ac` argument) in json, you can change that as well, try for example `\n`. If you'd like to use 32 | an encoder other than cJSON, pass the encoding function as the 5th argument (`ec`). It should accept anything as 33 | input parameter, and if there is a problem with encoding this function should return `nil` and an error 34 | message, such as: 35 | 36 | ```lua 37 | nil, "Cannot serialise function: type not supported" 38 | ``` 39 | 40 | For input argument `dt` it accepts anything that `cjson.encode` accepts (or whatever the custom encoding 41 | function accepts). 42 | 43 | ##### Example 44 | 45 | ```lua 46 | local pretty = require "resty.prettycjson" 47 | 48 | print(pretty({ 49 | key1 = "data", 50 | key2 = 27, 51 | key3 = { 52 | key3_1 = "something", 53 | key3_2 = "something else" 54 | }, 55 | key4 = { 56 | "item1", 57 | "item2" 58 | }, 59 | key5 = {}, 60 | key5 = {{''}, {'',''}, {{},{}}}, 61 | key6 = { '' }, 62 | key7 = {{{{ test = "value", {{{{{{}}},{{},{},{}},{},{}}}}}}}} 63 | })) 64 | ``` 65 | 66 | That will output: 67 | 68 | ```json 69 | { 70 | "key6": [ 71 | "" 72 | ], 73 | "key3": { 74 | "key3_1": "something", 75 | "key3_2": "something else" 76 | }, 77 | "key7": [ 78 | [ 79 | [ 80 | { 81 | "1": [ 82 | [ 83 | [ 84 | [ 85 | [ 86 | {} 87 | ] 88 | ], 89 | [ 90 | {}, 91 | {}, 92 | {} 93 | ], 94 | {}, 95 | {} 96 | ] 97 | ] 98 | ], 99 | "test": "value" 100 | } 101 | ] 102 | ] 103 | ], 104 | "key1": "data", 105 | "key5": [ 106 | [ 107 | "" 108 | ], 109 | [ 110 | "", 111 | "" 112 | ], 113 | [ 114 | {}, 115 | {} 116 | ] 117 | ], 118 | "key2": 27, 119 | "key4": [ 120 | "item1", 121 | "item2" 122 | ] 123 | } 124 | ``` 125 | 126 | ## Changes 127 | 128 | The changes of every release of this module is recorded in [Changes.md](https://github.com/bungle/lua-resty-prettycjson/blob/master/Changes.md) file. 129 | 130 | ## License 131 | 132 | `lua-resty-prettycjson` uses three clause BSD license. 133 | 134 | ``` 135 | Copyright (c) 2015 — 2016, Aapo Talvensaari 136 | All rights reserved. 137 | 138 | Redistribution and use in source and binary forms, with or without 139 | modification, are permitted provided that the following conditions are met: 140 | 141 | * Redistributions of source code must retain the above copyright notice, this 142 | list of conditions and the following disclaimer. 143 | 144 | * Redistributions in binary form must reproduce the above copyright notice, 145 | this list of conditions and the following disclaimer in the documentation 146 | and/or other materials provided with the distribution. 147 | 148 | * Neither the name of lua-resty-prettycjson nor the names of its 149 | contributors may be used to endorse or promote products derived from 150 | this software without specific prior written permission. 151 | 152 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 153 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 154 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 155 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 156 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 157 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 158 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 159 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 160 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 161 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 162 | ``` 163 | -------------------------------------------------------------------------------- /dist.ini: -------------------------------------------------------------------------------- 1 | name = lua-resty-prettycjson 2 | abstract = JSON Pretty Formatter for Lua cJSON 3 | author = Aapo Talvensaari (@bungle) 4 | is_original = yes 5 | license = 3bsd 6 | repo_link = https://github.com/bungle/lua-resty-prettycjson 7 | version = 1.6 8 | -------------------------------------------------------------------------------- /lib/resty/prettycjson.lua: -------------------------------------------------------------------------------- 1 | local ok, cjson = pcall(require, "cjson.safe") 2 | local enc = ok and cjson.encode or function() return nil, "Lua cJSON encoder not found" end 3 | local cat = table.concat 4 | local sub = string.sub 5 | local rep = string.rep 6 | return function(dt, lf, id, ac, ec) 7 | local s, e = (ec or enc)(dt) 8 | if not s then return s, e end 9 | lf, id, ac = lf or "\n", id or "\t", ac or " " 10 | local i, j, k, n, r, p, q = 1, 0, 0, #s, {}, nil, nil 11 | local al = sub(ac, -1) == "\n" 12 | for x = 1, n do 13 | local c = sub(s, x, x) 14 | if not q and (c == "{" or c == "[") then 15 | r[i] = p == ":" and cat{ c, lf } or cat{ rep(id, j), c, lf } 16 | j = j + 1 17 | elseif not q and (c == "}" or c == "]") then 18 | j = j - 1 19 | if p == "{" or p == "[" then 20 | i = i - 1 21 | r[i] = cat{ rep(id, j), p, c } 22 | else 23 | r[i] = cat{ lf, rep(id, j), c } 24 | end 25 | elseif not q and c == "," then 26 | r[i] = cat{ c, lf } 27 | k = -1 28 | elseif not q and c == ":" then 29 | r[i] = cat{ c, ac } 30 | if al then 31 | i = i + 1 32 | r[i] = rep(id, j) 33 | end 34 | else 35 | if c == '"' and p ~= "\\" then 36 | q = not q and true or nil 37 | end 38 | if j ~= k then 39 | r[i] = rep(id, j) 40 | i, k = i + 1, j 41 | end 42 | r[i] = c 43 | end 44 | p, i = c, i + 1 45 | end 46 | return cat(r) 47 | end 48 | -------------------------------------------------------------------------------- /lua-resty-prettycjson-dev-1.rockspec: -------------------------------------------------------------------------------- 1 | package = "lua-resty-prettycjson" 2 | version = "dev-1" 3 | source = { 4 | url = "git://github.com/bungle/lua-resty-prettycjson.git" 5 | } 6 | description = { 7 | summary = "JSON Pretty Formatter for Lua cJSON", 8 | detailed = "lua-resty-prettycjson is a JSON Pretty Formatter for Lua cJSON.", 9 | homepage = "https://github.com/bungle/lua-resty-prettycjson", 10 | maintainer = "Aapo Talvensaari ", 11 | license = "BSD" 12 | } 13 | dependencies = { 14 | "lua >= 5.1" 15 | } 16 | build = { 17 | type = "builtin", 18 | modules = { 19 | ["resty.prettycjson"] = "lib/resty/prettycjson.lua" 20 | } 21 | } 22 | --------------------------------------------------------------------------------