Module:Jf-JSON: Difference between revisions

Content deleted Content added
fix isNumber initialization bug
upgrade to latest version
Line 14:
-- maintained. Enjoy.
--
local VERSION = 2016072820160916.1719 -- version history at end of file
local AUTHOR_NOTE = "-[ JSON.lua package by Jeffrey Friedl (http://regex.info/blog/lua/json) version 2016072820160916.1719 ]-"
 
--
Line 333:
-- produces
-- ["one","two",null,null]
--
--
--
--
-- HANDLING LARGE AND/OR PRECISE NUMBERS
--
--
-- Without special handling, numbers in JSON can lose precision in Lua.
-- For example:
Line 365 ⟶ 368:
-- (This is done by encoding the numeric data with a Lua table/metatable that returns
-- the possibly-imprecise numeric form when accessed numerically, but the original precise
-- representation when accessed as a string.) You can also explicitly access
-- via JSON:forceString() and JSON:forceNumber())
--
-- Consider the example above, with this option turned on:
Line 514 ⟶ 518:
end
 
local isNumber = {
isNumber = {
__index = isNumber,
 
__tostring = function(T) return T.S end,
__unm = function(op) return getnum(op) end,
Line 532 ⟶ 533:
__le = function(op1, op2) return getnum(op1) <= getnum(op2) end,
}
isNumber.__index = {isNumber
 
function OBJDEF:asNumber(item)
Line 550 ⟶ 552:
end
end
 
--
-- Given an item that might be a normal string or number, or might be an 'isNumber' object defined above,
-- return the string version. This shouldn't be needed often because the 'isNumber' object should autoconvert
-- to a string in most cases, but it's here to allow it to be forced when needed.
--
function OBJDEF:forceString(item)
if type(item) == 'table' and type(item.S) == 'string' then
return item.S
else
return tostring(item)
end
end
 
--
-- Given an item that might be a normal string or number, or might be an 'isNumber' object defined above,
-- return the numeric version.
--
function OBJDEF:forceNumber(item)
if type(item) == 'table' and type(item.N) == 'number' then
return item.N
else
return tonumber(item)
end
end
 
local function unicode_codepoint_as_utf8(codepoint)
Line 1,322 ⟶ 1,350:
--
-- Version history:
--
-- 20160916.19 Fixed the isNumber.__index assignment (thanks to Jack Taylor)
--
-- 2016072820160730.1718 Added concatenationJSON:forceString() to the metatable forand JSON:asNumberforceNumber().
--
-- 20160728.17 Added concatenation to the metatable for JSON:asNumber()
--
-- 20160709.16 Could crash if not passed an options table (thanks jarno heikkinen <jarnoh@capturemonkey.com>).