From 2b7f0ca9c31587502f1ee797001362825845f6bc Mon Sep 17 00:00:00 2001 From: Thomas Harning Jr Date: Mon, 10 Oct 2011 01:07:47 -0400 Subject: [PATCH] all: 5.2 compatibility closes #11 --- lua/json.lua | 22 ++++++-- lua/json/decode.lua | 47 +++++++++++------ lua/json/decode/array.lua | 43 +++++++++++++--- lua/json/decode/calls.lua | 27 ++++++++-- lua/json/decode/number.lua | 33 +++++++++--- lua/json/decode/object.lua | 30 +++++++++-- lua/json/decode/others.lua | 30 +++++++++-- lua/json/decode/strings.lua | 32 ++++++++++-- lua/json/decode/util.lua | 101 ++++++++++++++++++++++++++++++------- lua/json/encode.lua | 32 +++++++++--- lua/json/encode/array.lua | 29 +++++++++-- lua/json/encode/calls.lua | 25 +++++++-- lua/json/encode/number.lua | 27 ++++++++-- lua/json/encode/object.lua | 27 ++++++++-- lua/json/encode/others.lua | 30 +++++++++-- lua/json/encode/output.lua | 24 +++++++-- lua/json/encode/output_utility.lua | 21 +++++++- lua/json/encode/strings.lua | 28 ++++++++-- lua/json/util.lua | 47 +++++++++++++---- 19 files changed, 538 insertions(+), 117 deletions(-) diff --git a/lua/json.lua b/lua/json.lua index 296fb0d..ae70148 100644 --- a/lua/json.lua +++ b/lua/json.lua @@ -6,7 +6,21 @@ local decode = require("json.decode") local encode = require("json.encode") local util = require("json.util") -module("json") -_M.decode = decode -_M.encode = encode -_M.util = util +local is_52 = _VERSION == "Lua 5.2" + +local _G = _G + +if is_52 then + _ENV = nil +end + +local json = { + decode = decode, + encode = encode, + util = util +} + +if not is_52 then + _G.json = json +end +return json diff --git a/lua/json/decode.lua b/lua/json/decode.lua index 403cf60..9d1035b 100644 --- a/lua/json/decode.lua +++ b/lua/json/decode.lua @@ -19,7 +19,13 @@ local ipairs, pairs = ipairs, pairs local string_char = require("string").char local require = require -module("json.decode") + +local is_52 = _VERSION == "Lua 5.2" +local _G = _G + +if is_52 then + _ENV = nil +end local modulesToLoad = { "array", @@ -32,7 +38,9 @@ local modulesToLoad = { local loadedModules = { } -default = { +local json_decode = {} + +json_decode.default = { unicodeWhitespace = true, initialObject = false, nothrow = false @@ -40,9 +48,9 @@ default = { local modes_defined = { "default", "strict", "simple" } -simple = {} +json_decode.simple = {} -strict = { +json_decode.strict = { unicodeWhitespace = true, initialObject = true, nothrow = false @@ -54,7 +62,7 @@ for _,name in ipairs(modulesToLoad) do local mod = require("json.decode." .. name) for _, mode in pairs(modes_defined) do if mod[mode] then - _M[mode][name] = mod[mode] + json_decode[mode][name] = mod[mode] end end loadedModules[name] = mod @@ -65,8 +73,8 @@ for _,name in ipairs(modulesToLoad) do end -- Shift over default into defaultOptions to permit build optimization -local defaultOptions = default -default = nil +local defaultOptions = json_decode.default +json_decode.default = nil local function buildDecoder(mode) @@ -123,11 +131,11 @@ local function buildDecoder(mode) end -- Since 'default' is nil, we cannot take map it -local defaultDecoder = buildDecoder(default) +local defaultDecoder = buildDecoder(json_decode.default) local prebuilt_decoders = {} for _, mode in pairs(modes_defined) do - if _M[mode] ~= nil then - prebuilt_decoders[_M[mode]] = buildDecoder(_M[mode]) + if json_decode[mode] ~= nil then + prebuilt_decoders[json_decode[mode]] = buildDecoder(json_decode[mode]) end end @@ -140,8 +148,8 @@ Options: initialObject => whether or not to require the initial object to be a table/array allowUndefined => whether or not to allow undefined values ]] -function getDecoder(mode) - mode = mode == true and strict or mode or default +local function getDecoder(mode) + mode = mode == true and json_decode.strict or mode or json_decode.default local decoder = mode == nil and defaultDecoder or prebuilt_decoders[mode] if decoder then return decoder @@ -149,13 +157,22 @@ function getDecoder(mode) return buildDecoder(mode) end -function decode(data, mode) +local function decode(data, mode) local decoder = getDecoder(mode) return decoder(data) end -local mt = getmetatable(_M) or {} +local mt = {} mt.__call = function(self, ...) return decode(...) end -setmetatable(_M, mt) + +json_decode.getDecoder = getDecoder +json_decode.decode = decode +setmetatable(json_decode, mt) +if not is_52 then + _G.json= _G.json or {} + _G.json.decode = merge(json_decode, _G.json.decode) +end + +return json_decode diff --git a/lua/json/decode/array.lua b/lua/json/decode/array.lua index c36dc35..002f0a4 100644 --- a/lua/json/decode/array.lua +++ b/lua/json/decode/array.lua @@ -9,9 +9,25 @@ local jsonutil = require("json.util") local table_maxn = require("table").maxn -local unpack = unpack +local is_52 = _VERSION == "Lua 5.2" +local unpack = unpack or require("table").unpack -module("json.decode.array") +local _G = _G + +if is_52 then + local pairs, type = pairs, type + -- Replacement for maxn since it is removed + table_maxn = function(t) + local max = 0 + for k in pairs(t) do + if type(k) == 'number' and k > max then + max = k + end + end + return max + end + _ENV = nil +end -- Utility function to help manage slighly sparse arrays local function processArray(array) @@ -30,8 +46,8 @@ local defaultOptions = { trailingComma = true } -default = nil -- Let the buildCapture optimization take place -strict = { +local default = nil -- Let the buildCapture optimization take place +local strict = { trailingComma = false } @@ -72,13 +88,28 @@ local function buildCapture(options, global_options, state) return capture end -function register_types() +local function register_types() util.register_type("ARRAY") end -function load_types(options, global_options, grammar, state) +local function load_types(options, global_options, grammar, state) local capture = buildCapture(options, global_options, state) local array_id = util.types.ARRAY grammar[array_id] = capture util.append_grammar_item(grammar, "VALUE", lpeg.V(array_id)) end + +local array = { + default = default, + strict = strict, + register_types = register_types, + load_types = load_types +} + +if not is_52 then + _G.json = _G.json or {} + _G.json.decode = _G.json.decode or {} + _G.json.decode.array = array +end + +return array diff --git a/lua/json/decode/calls.lua b/lua/json/decode/calls.lua index d653df6..9aa99d7 100644 --- a/lua/json/decode/calls.lua +++ b/lua/json/decode/calls.lua @@ -14,7 +14,12 @@ local buildCall = require("json.util").buildCall local getmetatable = getmetatable -module("json.decode.calls") +local is_52 = _VERSION == "Lua 5.2" +local _G = _G + +if is_52 then + _ENV = nil +end local defaultOptions = { defs = nil, @@ -23,8 +28,8 @@ local defaultOptions = { } -- No real default-option handling needed... -default = nil -strict = nil +local default = nil +local strict = nil local isPattern if lpeg.type then @@ -127,9 +132,23 @@ local function buildCapture(options, global_options, state) return callCapture end -function load_types(options, global_options, grammar, state) +local function load_types(options, global_options, grammar, state) local capture = buildCapture(options, global_options, state) if capture then util.append_grammar_item(grammar, "VALUE", capture) end end + +local calls = { + default = default, + strict = strict, + load_types = load_types +} + +if not is_52 then + _G.json = _G.json or {} + _G.json.decode = _G.json.decode + _G.json.decode.calls = calls +end + +return calls diff --git a/lua/json/decode/number.lua b/lua/json/decode/number.lua index efc7fd6..a17ea8f 100644 --- a/lua/json/decode/number.lua +++ b/lua/json/decode/number.lua @@ -7,13 +7,17 @@ local tonumber = tonumber local merge = require("json.util").merge local util = require("json.decode.util") -module("json.decode.number") +local is_52 = _VERSION == "Lua 5.2" +local _G = _G + +if is_52 then + _ENV = nil +end local digit = lpeg.R("09") local digits = digit^1 -int = (lpeg.P('-') + 0) * (lpeg.R("19") * digits + digit) -local int = int +local int = (lpeg.P('-') + 0) * (lpeg.R("19") * digits + digit) local frac = lpeg.P('.') * digits @@ -32,8 +36,8 @@ local defaultOptions = { hex = false } -default = nil -- Let the buildCapture optimization take place -strict = { +local default = nil -- Let the buildCapture optimization take place +local strict = { nan = false, inf = false } @@ -73,13 +77,28 @@ local function buildCapture(options) return ret end -function register_types() +local function register_types() util.register_type("INTEGER") end -function load_types(options, global_options, grammar) +local function load_types(options, global_options, grammar) local integer_id = util.types.INTEGER local capture = buildCapture(options) util.append_grammar_item(grammar, "VALUE", capture) grammar[integer_id] = int / tonumber end + +local number = { + int = int, + default = default, + strict = strict, + register_types = register_types, + load_types = load_types +} + +if not is_52 then + _G.json = _G.json or {} + _G.json.decode = _G.json.decode or {} + _G.json.decode.number = number +end +return number diff --git a/lua/json/decode/object.lua b/lua/json/decode/object.lua index ad1beac..fb087e2 100644 --- a/lua/json/decode/object.lua +++ b/lua/json/decode/object.lua @@ -14,7 +14,12 @@ local tostring = tostring local rawset = rawset -module("json.decode.object") +local is_52 = _VERSION == "Lua 5.2" +local _G = _G + +if is_52 then + _ENV = nil +end -- BEGIN LPEG < 0.9 SUPPORT local initObject, applyObjectKey @@ -35,9 +40,9 @@ local defaultOptions = { trailingComma = true } -default = nil -- Let the buildCapture optimization take place +local default = nil -- Let the buildCapture optimization take place -strict = { +local strict = { number = false, identifier = false, trailingComma = false @@ -113,13 +118,28 @@ local function buildCapture(options, global_options, state) return capture end -function register_types() +local function register_types() util.register_type("OBJECT") end -function load_types(options, global_options, grammar, state) +local function load_types(options, global_options, grammar, state) local capture = buildCapture(options, global_options, state) local object_id = util.types.OBJECT grammar[object_id] = capture util.append_grammar_item(grammar, "VALUE", lpeg.V(object_id)) end + +local object = { + default = default, + strict = strict, + register_types = register_types, + load_types = load_types +} + +if not is_52 then + _G.json = _G.json or {} + _G.json.decode = _G.json.decode or {} + _G.json.decode.object = object +end + +return object diff --git a/lua/json/decode/others.lua b/lua/json/decode/others.lua index 01d60db..0bacec8 100644 --- a/lua/json/decode/others.lua +++ b/lua/json/decode/others.lua @@ -9,7 +9,12 @@ local util = require("json.decode.util") local rawset = rawset -- Container module for other JavaScript types (bool, null, undefined) -module("json.decode.others") +local is_52 = _VERSION == "Lua 5.2" +local _G = _G + +if is_52 then + _ENV = nil +end -- For null and undefined, use the util.null value to preserve null-ness local booleanCapture = @@ -26,12 +31,12 @@ local defaultOptions = { setObjectKey = rawset } -default = nil -- Let the buildCapture optimization take place -simple = { +local default = nil -- Let the buildCapture optimization take place +local simple = { null = false, -- Mapped to nil undefined = false -- Mapped to nil } -strict = { +local strict = { allowUndefined = false } @@ -48,7 +53,22 @@ local function buildCapture(options) return valueCapture end -function load_types(options, global_options, grammar) +local function load_types(options, global_options, grammar) local capture = buildCapture(options) util.append_grammar_item(grammar, "VALUE", capture) end + +local others = { + default = default, + simple = simple, + strict = strict, + load_types = load_types +} + +if not is_52 then + _G.json = _G.json or{} + _G.json.decode = _G.json.decode or {} + _G.json.decode.others = others +end + +return others diff --git a/lua/json/decode/strings.lua b/lua/json/decode/strings.lua index f092558..14c1a42 100644 --- a/lua/json/decode/strings.lua +++ b/lua/json/decode/strings.lua @@ -12,7 +12,14 @@ local floor = require("math").floor local table_concat = require("table").concat local error = error -module("json.decode.strings") + +local is_52 = _VERSION == "Lua 5.2" +local _G = _G + +if is_52 then + _ENV = nil +end + local function get_error(item) local fmt_string = item .. " in string [%q] @ %i:%i" return function(data, index) @@ -75,9 +82,9 @@ local defaultOptions = { strict_quotes = false } -default = nil -- Let the buildCapture optimization take place +local default = nil -- Let the buildCapture optimization take place -strict = { +local strict = { badChars = '\b\f\n\r\t\v', additionalEscapes = false, -- no additional escapes escapeCheck = #lpeg.S('bfnrtv/\\"u'), --only these chars are allowed to be escaped @@ -118,13 +125,28 @@ local function buildCapture(options) return captureString end -function register_types() +local function register_types() util.register_type("STRING") end -function load_types(options, global_options, grammar) +local function load_types(options, global_options, grammar) local capture = buildCapture(options) local string_id = util.types.STRING grammar[string_id] = capture util.append_grammar_item(grammar, "VALUE", lpeg.V(string_id)) end + +local strings = { + default = default, + strict = strict, + register_types = register_types, + load_types = load_types +} + +if not is_52 then + _G.json = _G.json or {} + _G.json.decode = _G.json.decode or {} + _G.json.decode.strings = strings +end + +return strings diff --git a/lua/json/decode/util.lua b/lua/json/decode/util.lua index f34335c..a17b2c6 100644 --- a/lua/json/decode/util.lua +++ b/lua/json/decode/util.lua @@ -12,10 +12,50 @@ local rawset = rawset local error = error local setmetatable = setmetatable -module("json.decode.util") +local table_concat = require("table").concat + +local is_52 = _VERSION == "Lua 5.2" +local _G = _G + +if is_52 then + _ENV = nil +end + +local function build_report(msg) + local fmt = msg:gsub("%%", "%%%%") .. " @ character: %i %i:%i [%s] line:\n%s" + return lpeg.P(function(data, pos) + local line, line_index, bad_char, last_line = get_invalid_character_info(data, pos) + local text = fmt:format(pos, line, line_index, bad_char, last_line) + error(text) + end) +end +local function unexpected() + local msg = "unexpected character" + return build_report(msg) +end +local function expected(...) + local items = {...} + local msg + if #items > 1 then + msg = "expected one of '" .. table_concat(items, "','") .. "'" + else + msg = "expected '" .. items[1] .. "'" + end + return build_report(msg) +end +local function denied(item, option) + local msg + if option then + msg = ("'%s' denied by option set '%s'"):format(item, option) + else + msg = ("'%s' denied"):format(item) + end + return build_report(msg) +end -- 09, 0A, 0B, 0C, 0D, 20 -ascii_space = lpeg.S("\t\n\v\f\r ") +local ascii_space = lpeg.S("\t\n\v\f\r ") +local unicode_space do local chr = string_char local u_space = ascii_space @@ -37,24 +77,24 @@ do u_space = u_space + lpeg.P(chr(0xE3, 0x80, 0x80)) -- BOM \uFEFF u_space = u_space + lpeg.P(chr(0xEF, 0xBB, 0xBF)) - _M.unicode_space = u_space + unicode_space = u_space end -identifier = lpeg.R("AZ","az","__") * lpeg.R("AZ","az", "__", "09") ^0 +local identifier = lpeg.R("AZ","az","__") * lpeg.R("AZ","az", "__", "09") ^0 -hex = lpeg.R("09","AF","af") -hexpair = hex * hex +local hex = lpeg.R("09","AF","af") +local hexpair = hex * hex -comments = { +local comments = { cpp = lpeg.P("//") * (1 - lpeg.P("\n"))^0 * lpeg.P("\n"), c = lpeg.P("/*") * (1 - lpeg.P("*/"))^0 * lpeg.P("*/") } -comment = comments.cpp + comments.c +local comment = comments.cpp + comments.c -ascii_ignored = (ascii_space + comment)^0 +local ascii_ignored = (ascii_space + comment)^0 -unicode_ignored = (unicode_space + comment)^0 +local unicode_ignored = (unicode_space + comment)^0 local types = setmetatable({false}, { __index = function(self, k) @@ -62,15 +102,13 @@ local types = setmetatable({false}, { end }) -function register_type(name) +local function register_type(name) types[#types + 1] = name types[name] = #types return #types end -_M.types = types - -function append_grammar_item(grammar, name, capture) +local function append_grammar_item(grammar, name, capture) local id = types[name] local original = grammar[id] if original then @@ -82,9 +120,9 @@ end -- Parse the lpeg version skipping patch-values -- LPEG <= 0.7 have no version value... so 0.7 is value -DecimalLpegVersion = lpeg.version and tonumber(lpeg.version():match("^(%d+%.%d+)")) or 0.7 +local DecimalLpegVersion = lpeg.version and tonumber(lpeg.version():match("^(%d+%.%d+)")) or 0.7 -function get_invalid_character_info(input, index) +local function get_invalid_character_info(input, index) local parsed = input:sub(1, index) local bad_character = input:sub(index, index) local _, line_number = parsed:gsub('\n',{}) @@ -92,7 +130,36 @@ function get_invalid_character_info(input, index) return line_number, #last_line, bad_character, last_line end -function setObjectKeyForceNumber(t, key, value) +local function setObjectKeyForceNumber(t, key, value) key = tonumber(key) or key return rawset(t, key, value) end + +local util = { + unexpected = unexpected, + expected = expected, + denied = denied, + ascii_space = ascii_space, + unicode_space = unicode_space, + identifier = identifier, + hex = hex, + hexpair = hexpair, + comments = comments, + comment = comment, + ascii_ignored = ascii_ignored, + unicode_ignored = unicode_ignored, + register_type = register_type, + types = types, + append_grammar_item = append_grammar_item, + DecimalLpegVersion = DecimalLpegVersion, + get_invalid_character_info = get_invalid_character_info, + setObjectKeyForceNumber = setObjectKeyForceNumber +} + +if not is_52 then + _G.json = _G.json or {} + _G.json.decode = _G.json.decode or {} + _G.json.decode.util = util +end + +return util diff --git a/lua/json/encode.lua b/lua/json/encode.lua index 8666ea4..ddb7c82 100644 --- a/lua/json/encode.lua +++ b/lua/json/encode.lua @@ -15,7 +15,12 @@ local output = require("json.encode.output") local util = require("json.util") local util_merge, isCall = util.merge, util.isCall -module("json.encode") +local is_52 = _VERSION == "Lua 5.2" +local _G = _G + +if is_52 then + _ENV = nil +end --[[ List of encoding modules to load. @@ -36,8 +41,8 @@ local loadedModules = {} -- Default configuration options to apply local defaultOptions = {} -- Configuration bases for client apps -default = nil -strict = { +local default = nil +local strict = { initialObject = true -- Require an object at the root } @@ -111,7 +116,7 @@ end the initial encoder is responsible for initializing state State has at least these values configured: encode, check_unique, already_encoded ]] -function getEncoder(options) +local function getEncoder(options) options = options and util_merge({}, defaultOptions, options) or defaultOptions local encode = getBaseEncoder(options) @@ -148,12 +153,25 @@ end check_unique -- used by inner encoders to make sure value is unique already_encoded -- used to unmark a value as unique ]] -function encode(data, options) +local function encode(data, options) return getEncoder(options)(data) end -local mt = getmetatable(_M) or {} +local mt = {} mt.__call = function(self, ...) return encode(...) end -setmetatable(_M, mt) + +local json_encode = { + default = default, + strict = strict, + getEncoder = getEncoder, + encode = encode +} +setmetatable(json_encode, mt) + +if not is_52 then + _G.json = _G.json or {} + _G.json.encode = util_merge(json_encode, _G.json.encode) +end +return json_encode diff --git a/lua/json/encode/array.lua b/lua/json/encode/array.lua index 45e300c..e1578b9 100644 --- a/lua/json/encode/array.lua +++ b/lua/json/encode/array.lua @@ -16,14 +16,19 @@ local math_floor, math_modf = math.floor, math.modf local util_merge = require("json.util").merge local util_IsArray = require("json.util").IsArray -module("json.encode.array") +local is_52 = _VERSION == "Lua 5.2" +local _G = _G + +if is_52 then + _ENV = nil +end local defaultOptions = { isArray = util_IsArray } -default = nil -strict = nil +local default = nil +local strict = nil --[[ Utility function to determine whether a table is an array or not. @@ -34,7 +39,7 @@ strict = nil before it) * It is a contiguous list of values with zero string-based keys ]] -function isArray(val, options) +local function isArray(val, options) local externalIsArray = options and options.isArray if externalIsArray then @@ -72,7 +77,7 @@ local function unmarkAfterEncode(tab, state, ...) state.already_encoded[tab] = nil return ... end -function getEncoder(options) +local function getEncoder(options) options = options and util_merge({}, defaultOptions, options) or defaultOptions local function encodeArray(tab, state) if not isArray(tab, options) then @@ -97,3 +102,17 @@ function getEncoder(options) end return { table = encodeArray } end + +local array = { + default = default, + strict = strict, + isArray = isArray, + getEncoder = getEncoder +} + +if not is_52 then + _G.json = _G.json or {} + _G.json.encode = _G.json.encode or {} + _G.json.encode.array = array +end +return array diff --git a/lua/json/encode/calls.lua b/lua/json/encode/calls.lua index c1cc646..ff970fe 100644 --- a/lua/json/encode/calls.lua +++ b/lua/json/encode/calls.lua @@ -15,15 +15,19 @@ local util = require("json.util") local util_merge, isCall, decodeCall = util.merge, util.isCall, util.decodeCall -module("json.encode.calls") +local is_52 = _VERSION == "Lua 5.2" +local _G = _G +if is_52 then + _ENV = nil +end local defaultOptions = { } -- No real default-option handling needed... -default = nil -strict = nil +local default = nil +local strict = nil --[[ @@ -32,7 +36,7 @@ strict = nil name == name of the function call parameters == array of parameters to encode ]] -function getEncoder(options) +local function getEncoder(options) options = options and util_merge({}, defaultOptions, options) or defaultOptions local function encodeCall(value, state) if not isCall(value) then @@ -59,3 +63,16 @@ function getEncoder(options) ['function'] = encodeCall } end + +local calls = { + default = default, + strict = strict, + getEncoder = getEncoder +} + +if not is_52 then + _G.json = _G.json or {} + _G.json.encode = _G.json.encode or {} + _G.json.encode.calls = calls +end +return calls diff --git a/lua/json/encode/number.lua b/lua/json/encode/number.lua index 62d5765..63a5786 100644 --- a/lua/json/encode/number.lua +++ b/lua/json/encode/number.lua @@ -7,15 +7,20 @@ local assert = assert local util = require("json.util") local huge = require("math").huge -module("json.encode.number") +local is_52 = _VERSION == "Lua 5.2" +local _G = _G + +if is_52 then + _ENV = nil +end local defaultOptions = { nan = true, inf = true } -default = nil -- Let the buildCapture optimization take place -strict = { +local default = nil -- Let the buildCapture optimization take place +local strict = { nan = false, inf = false } @@ -36,7 +41,7 @@ local function encodeNumber(number, options) return tostring(number) end -function getEncoder(options) +local function getEncoder(options) options = options and util.merge({}, defaultOptions, options) or defaultOptions return { number = function(number, state) @@ -44,3 +49,17 @@ function getEncoder(options) end } end + +local number = { + default = default, + strict = strict, + getEncoder = getEncoder +} + +if not is_52 then + _G.json = _G.json or {} + _G.json.encode = _G.json.encode or {} + _G.json.encode.number = number +end + +return number diff --git a/lua/json/encode/object.lua b/lua/json/encode/object.lua index 796347e..e6b4bb2 100644 --- a/lua/json/encode/object.lua +++ b/lua/json/encode/object.lua @@ -11,13 +11,18 @@ local tostring = tostring local table_concat = require("table").concat local util_merge = require("json.util").merge -module("json.encode.object") +local is_52 = _VERSION == "Lua 5.2" +local _G = _G + +if is_52 then + _ENV = nil +end local defaultOptions = { } -default = nil -strict = nil +local default = nil +local strict = nil --[[ Cleanup function to unmark a value as in the encoding process and return @@ -57,7 +62,7 @@ local function encodeTable(tab, options, state) return unmarkAfterEncode(tab, state, compositeEncoder(valueEncoder, '{', '}', nil, tab, encode, state)) end -function getEncoder(options) +local function getEncoder(options) options = options and util_merge({}, defaultOptions, options) or defaultOptions return { table = function(tab, state) @@ -65,3 +70,17 @@ function getEncoder(options) end } end + +local object = { + default = default, + strict = strict, + getEncoder = getEncoder +} + +if not is_52 then + _G.json = _G.json or {} + _G.json.encode = _G.json.encode or {} + _G.json.encode.object = object +end + +return object diff --git a/lua/json/encode/others.lua b/lua/json/encode/others.lua index ea2c88f..bd199cf 100644 --- a/lua/json/encode/others.lua +++ b/lua/json/encode/others.lua @@ -9,10 +9,15 @@ local jsonutil = require("json.util") local util_merge = require("json.util").merge local type = type -module("json.encode.others") +local is_52 = _VERSION == "Lua 5.2" +local _G = _G + +if is_52 then + _ENV = nil +end -- Shortcut that works -encodeBoolean = tostring +local encodeBoolean = tostring local defaultOptions = { allowUndefined = true, @@ -20,12 +25,12 @@ local defaultOptions = { undefined = jsonutil.undefined } -default = nil -- Let the buildCapture optimization take place -strict = { +local default = nil -- Let the buildCapture optimization take place +local strict = { allowUndefined = false } -function getEncoder(options) +local function getEncoder(options) options = options and util_merge({}, defaultOptions, options) or defaultOptions local function encodeOthers(value, state) if value == options.null then @@ -53,3 +58,18 @@ function getEncoder(options) end return ret end + +local others = { + encodeBoolean = encodeBoolean, + default = default, + strict = strict, + getEncoder = getEncoder +} + +if not is_52 then + _G.json = _G.json or {} + _G.json.encode = _G.json.encode or {} + _G.json.encode.others = others +end + +return others diff --git a/lua/json/encode/output.lua b/lua/json/encode/output.lua index a5dbd79..b754a5c 100644 --- a/lua/json/encode/output.lua +++ b/lua/json/encode/output.lua @@ -13,7 +13,12 @@ local setmetatable = setmetatable local output_utility = require("json.encode.output_utility") -module("json.encode.output") +local is_52 = _VERSION == "Lua 5.2" +local _G = _G + +if is_52 then + _ENV = nil +end local tableCompositeCache = setmetatable({}, {__mode = 'v'}) @@ -38,7 +43,7 @@ local function defaultTableCompositeWriter(nextValues, beginValue, closeValue, i end -- no 'simple' as default action is just to return the value -function getDefault() +local function getDefault() return { composite = defaultTableCompositeWriter } end @@ -77,8 +82,21 @@ local function buildIoWriter(output) end return { composite = ioWriter, simple = ioSimpleWriter } end -function getIoWriter(output) +local function getIoWriter(output) return function() return buildIoWriter(output) end end + +local output = { + getDefault = getDefault, + getIoWriter = getIoWriter +} + +if not is_52 then + _G.json = _G.json or {} + _G.json.encode = _G.json.encode or {} + _G.json.encode.output = output +end + +return output diff --git a/lua/json/encode/output_utility.lua b/lua/json/encode/output_utility.lua index b8d3573..0d89b38 100644 --- a/lua/json/encode/output_utility.lua +++ b/lua/json/encode/output_utility.lua @@ -5,7 +5,12 @@ local setmetatable = setmetatable local assert, loadstring = assert, loadstring -module("json.encode.output_utility") +local is_52 = _VERSION == "Lua 5.2" +local _G = _G + +if is_52 then + _ENV = nil +end -- Key == weak, if main key goes away, then cache cleared local outputCache = setmetatable({}, {__mode = 'k'}) @@ -33,7 +38,7 @@ local function buildFunction(nextValues, innerValue, valueWriter, innerWriter) return assert(loadstring(functionCode))() end -function prepareEncoder(cacheKey, nextValues, innerValue, valueWriter, innerWriter) +local function prepareEncoder(cacheKey, nextValues, innerValue, valueWriter, innerWriter) local cache = outputCache[cacheKey] if not cache then cache = {} @@ -46,3 +51,15 @@ function prepareEncoder(cacheKey, nextValues, innerValue, valueWriter, innerWrit end return fun end + +local output_utility = { + prepareEncoder = prepareEncoder +} + +if not is_52 then + _G.json = _G.json or {} + _G.json.encode = _G.json.encode or {} + _G.json.encode.output_utility = output_utility +end + +return output_utility diff --git a/lua/json/encode/strings.lua b/lua/json/encode/strings.lua index 933ae4c..3330fe1 100644 --- a/lua/json/encode/strings.lua +++ b/lua/json/encode/strings.lua @@ -6,7 +6,13 @@ local string_char = require("string").char local pairs = pairs local util_merge = require("json.util").merge -module("json.encode.strings") + +local is_52 = _VERSION == "Lua 5.2" +local _G = _G + +if is_52 then + _ENV = nil +end local normalEncodingMap = { ['"'] = '\\"', @@ -49,10 +55,10 @@ local defaultOptions = { encodeSetAppend = nil -- Chars to append to the default set } -default = nil -strict = nil +local default = nil +local strict = nil -function getEncoder(options) +local function getEncoder(options) options = options and util_merge({}, defaultOptions, options) or defaultOptions local encodeSet = options.encodeSet if options.encodeSetAppend then @@ -74,3 +80,17 @@ function getEncoder(options) string = encodeString } end + +local strings = { + default = default, + strict = strict, + getEncoder = getEncoder +} + +if not is_52 then + _G.json = _G.json or {} + _G.json.encode = _G.json.encode or {} + _G.json.encode.strings = strings +end + +return strings diff --git a/lua/json/util.lua b/lua/json/util.lua index 28e47ea..8664e9d 100644 --- a/lua/json/util.lua +++ b/lua/json/util.lua @@ -9,13 +9,19 @@ local pairs = pairs local getmetatable, setmetatable = getmetatable, setmetatable local select = select -module("json.util") +local is_52 = _VERSION == "Lua 5.2" +local _G = _G + +if is_52 then + _ENV = nil +end + local function foreach(tab, func) for k, v in pairs(tab) do func(k,v) end end -function printValue(tab, name) +local function printValue(tab, name) local parsed = {} local function doPrint(key, value, space) space = space or '' @@ -38,7 +44,7 @@ function printValue(tab, name) doPrint(name, tab) end -function clone(t) +local function clone(t) local ret = {} for k,v in pairs(t) do ret[k] = v @@ -55,15 +61,14 @@ local function merge(t, from, ...) end return merge(t, ...) end -_M.merge = merge -- Function to insert nulls into the JSON stream -function null() +local function null() return null end -- Marker for 'undefined' values -function undefined() +local function undefined() return undefined end @@ -74,7 +79,7 @@ local ArrayMT = {} Or false if it has no array component at all Otherwise nil to get the normal detection component working ]] -function IsArray(value) +local function IsArray(value) if type(value) ~= 'table' then return false end local ret = getmetatable(value) == ArrayMT if not ret then @@ -83,18 +88,18 @@ function IsArray(value) return ret end end -function InitArray(array) +local function InitArray(array) setmetatable(array, ArrayMT) return array end local CallMT = {} -function isCall(value) +local function isCall(value) return CallMT == getmetatable(value) end -function buildCall(name, ...) +local function buildCall(name, ...) local callData = { name = name, parameters = {n = select('#', ...), ...} @@ -102,7 +107,27 @@ function buildCall(name, ...) return setmetatable(callData, CallMT) end -function decodeCall(callData) +local function decodeCall(callData) if not isCall(callData) then return nil end return callData.name, callData.parameters end + +local json_util = { + printValue = printValue, + clone = clone, + merge = merge, + null = null, + undefined = undefined, + IsArray = IsArray, + InitArray = InitArray, + isCall = isCall, + buildCall = buildCall, + decodeCall = decodeCall +} + +if not is_52 then + _G.json = _G.json or {} + _G.json.util = json_util +end + +return json_util -- 2.11.4.GIT