From 069a8540b0a8bdf89c06f291c0420261f5562d18 Mon Sep 17 00:00:00 2001 From: Bart Van Der Meerssche Date: Sat, 29 Jan 2011 19:53:41 +0100 Subject: [PATCH] [luci] backport json decode/encode string functions from 0.10 --- .../flukso/luasrc/{fluksod.lua => spid.lua} | 0 .../package/luci/libs/json/luasrc/json.lua | 30 ++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) rename mote/v2/openwrt/package/flukso/luasrc/{fluksod.lua => spid.lua} (100%) diff --git a/mote/v2/openwrt/package/flukso/luasrc/fluksod.lua b/mote/v2/openwrt/package/flukso/luasrc/spid.lua similarity index 100% rename from mote/v2/openwrt/package/flukso/luasrc/fluksod.lua rename to mote/v2/openwrt/package/flukso/luasrc/spid.lua diff --git a/mote/v2/openwrt/package/luci/libs/json/luasrc/json.lua b/mote/v2/openwrt/package/luci/libs/json/luasrc/json.lua index a39d19a..5b09285 100644 --- a/mote/v2/openwrt/package/luci/libs/json/luasrc/json.lua +++ b/mote/v2/openwrt/package/luci/libs/json/luasrc/json.lua @@ -10,7 +10,7 @@ You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 -$Id: json.lua 3996 2009-01-04 20:08:45Z Cyrus $ +$Id$ Decoder: Info: @@ -52,6 +52,7 @@ local type = type local pairs = pairs local ipairs = ipairs local next = next +local pcall = pcall local getmetatable = getmetatable @@ -59,6 +60,33 @@ local getmetatable = getmetatable -- @cstyle instance module "luci.json" + +--- Directly decode a JSON string +-- @param json JSON-String +-- @return Lua object +function decode(json, ...) + local a = ActiveDecoder(function() return nil end, ...) + a.chunk = json + local s, obj = pcall(a.get, a) + return s and obj or nil +end + + +--- Direcly encode a Lua object into a JSON string. +-- @param obj Lua Object +-- @return JSON string +function encode(obj, ...) + local out = {} + local e = Encoder(obj, 1, ...):source() + local chnk, err + repeat + chnk, err = e() + out[#out+1] = chnk + until chnk + return not err and table.concat(out) or nil +end + + --- Null replacement function -- @return null function null()