Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
json2lua/json2lua
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
executable file
207 lines (173 sloc)
4.2 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /bin/bash | |
| VERSION="v0.3.3" | |
| # Detect Lua interpreter (prefer LuaJIT 2) | |
| if [ ! -z "${LUA}" ]; then | |
| if [ -z "$(which ${LUA})" ]; then | |
| LUA="" | |
| fi | |
| fi | |
| if [ -z "${LUA}" ]; then | |
| LUA="luajit2" | |
| if [ -z "$(which ${LUA})" ]; then | |
| LUA="luajit" | |
| if [ -z "$(which ${LUA})" ]; then | |
| LUA="lua" | |
| if [ -z "$(which ${LUA})" ]; then | |
| echo "Error: luajit2, luajit and lua executables not found" >&2 | |
| exit 1 | |
| fi | |
| fi | |
| fi | |
| fi | |
| function version() | |
| { | |
| cat << EOF | |
| JSON to Lua translator ${VERSION} | |
| EOF | |
| } | |
| function usage() | |
| { | |
| cat << EOF | |
| Usage: | |
| $0 [options] < in.json > out.lua | |
| Options: | |
| -h Print this text | |
| -v Print script version | |
| -n Do not pretty-print Lua code (default: do pretty-print) | |
| -wN Set maximum pretty-print width to N chars (default: 80) | |
| -iStr Set pretty-print indent to string (default: two spaces, ' ') | |
| -N Force object keys to be transformed to numbers | |
| whenever possible (default: off) | |
| -e Do not print terminating EOL (default: do print) | |
| -r Prefix data with 'return' (default: off) | |
| EOF | |
| } | |
| NO_PRETTY_PRINT=false | |
| MAX_WIDTH=80 | |
| INDENT_STR=" " | |
| FORCE_NUM_KEYS=false | |
| SKIP_TERMINATING_EOL=false | |
| PREFIX_WITH_RETURN=false | |
| while getopts ":hvnw:i:Ner" opt; do | |
| case ${opt} in | |
| h) | |
| version | |
| usage | |
| exit 0 | |
| ;; | |
| v) | |
| version | |
| exit 0 | |
| ;; | |
| n) | |
| NO_PRETTY_PRINT=true | |
| ;; | |
| w) | |
| MAX_WIDTH="${OPTARG}" | |
| ;; | |
| i) | |
| INDENT_STR="${OPTARG}" | |
| ;; | |
| N) | |
| FORCE_NUM_KEYS=true | |
| ;; | |
| e) | |
| SKIP_TERMINATING_EOL=true | |
| ;; | |
| r) | |
| PREFIX_WITH_RETURN=true | |
| ;; | |
| \?) | |
| echo "unknown option: -${OPTARG}" >&2 | |
| exit 1 | |
| ;; | |
| :) | |
| echo "option -${OPTARG} requires an argument" >&2 | |
| exit 1 | |
| ;; | |
| esac | |
| done | |
| ${LUA} -e " | |
| local NO_PRETTY_PRINT = ${NO_PRETTY_PRINT} | |
| local MAX_WIDTH = ${MAX_WIDTH} | |
| local INDENT_STR = '${INDENT_STR}' | |
| local FORCE_NUM_KEYS = ${FORCE_NUM_KEYS} | |
| local SKIP_TERMINATING_EOL = ${SKIP_TERMINATING_EOL} | |
| local PREFIX_WITH_RETURN = ${PREFIX_WITH_RETURN} | |
| -------------------------------------------------------------------------------- | |
| pcall(require, 'luarocks.require') -- Ignoring errors | |
| local json_decode, json_util, json_decode_util | |
| do | |
| local json, err = pcall(require, 'json') | |
| if not json then | |
| io.stdout:write( | |
| err, '\n\n', | |
| 'try running \'luarocks install luajson\'', '\n' | |
| ) | |
| io.stdout:flush() | |
| os.exit(1) | |
| end | |
| json_util = require('json.util') | |
| json_decode = require('json.decode') | |
| json_decode_util = require('json.decode.util') | |
| if | |
| FORCE_NUM_KEYS and | |
| not json_decode_util.setObjectKeyForceNumber | |
| then | |
| -- TODO: Traverse table manually in this case. | |
| error( | |
| 'can\'t force numeric keys:\ninstalled luajson version' | |
| .. ' does not support setObjectKey option' | |
| ) | |
| end | |
| end | |
| -------------------------------------------------------------------------------- | |
| do -- Bootstrap lua-nucleo | |
| local res, err = pcall(require, 'lua-nucleo.module') | |
| if not res then | |
| io.stdout:write( | |
| err, | |
| '\n\n', | |
| 'try running \'luarocks install lua-nucleo\'', | |
| '\n' | |
| ) | |
| io.stdout:flush() | |
| os.exit(1) | |
| end | |
| require('lua-nucleo.strict') | |
| end | |
| -------------------------------------------------------------------------------- | |
| local input = assert(io.stdin:read('*a')) | |
| -- Using simple decoding since we need nulls to be translated to nils. | |
| local decode_options = json_decode.simple | |
| if FORCE_NUM_KEYS then | |
| local twithdefaults = import 'lua-nucleo/table-utils.lua' { 'twithdefaults' } | |
| decode_options = twithdefaults( | |
| { | |
| object = | |
| { | |
| setObjectKey = json_decode_util.setObjectKeyForceNumber; | |
| }; | |
| }, | |
| decode_options | |
| ) | |
| end | |
| local data, err = json_decode(input, decode_options) | |
| if err then | |
| error('luajson error: ' .. err) | |
| end | |
| if PREFIX_WITH_RETURN then | |
| io.stdout:write('return ') | |
| end | |
| if NO_PRETTY_PRINT or type(data) ~= 'table' then | |
| local tstr = import 'lua-nucleo/tstr.lua' { 'tstr' } | |
| io.stdout:write(tstr(data)) | |
| else | |
| local tpretty = import 'lua-nucleo/tpretty.lua' { 'tpretty' } | |
| io.stdout:write(tpretty(data, INDENT_STR, MAX_WIDTH)) | |
| end | |
| if not SKIP_TERMINATING_EOL then | |
| io.stdout:write('\n') | |
| end | |
| io.stdout:flush() | |
| " |