lua对象json序列化,很简单,没做中文的unicode编码了

local function bool2json(v)
return v and "true" or "false";
end

local function string2json(s)
return table.concat({"\"", s, "\""});
end

local allowedkey={
["boolean"]=true,
["number"]=true,
["table"]=true,
}
local function table2json(tb)
local s={};
for k, v in next, tb do
local kind=type(k);
if not allowedkey[type(k)] then
error(_from("{kind}类型不能做为json格式数据key", kind));
end
table.push(s, lua2json(k), ":", lua2json(v), ",");
end
return table.concat(s);
end

local function number2json(num)
return tostring(num);
end

local lua2jsonfuncs={
["boolean"]=bool2json,
["number"]=number2json,
["table"]=table2json,
["string"]=string2json
}
function _G.lua2json(v)
local kind=type(v);
local func=lua2jsonfuncs[kind];
if not func then
error(_from("{kind}类型不能转化为json格式数据", kind));
end
return func(v);
end

--[[-----------test--------------
dump(lua2json(true));
dump(lua2json(false));
dump(lua2json("absd"));
dump(lua2json({a=true, b=1, c='fwef', d={a=true, b=1, c='ffef'}}));
dump(lua2json{1,2,4,3}) ]]

lua2json的更多相关文章

随机推荐

  1. ssh免密码登录之分发密钥

    ssh免密码登录之分发密钥 1.ssh免密码登录 密码登录和密钥登录有什么不同? 密码登录(口令登录),每次登录都需要发送密码(ssh) 密钥登录,分为公钥和私钥,公钥相当于锁,私钥相当于钥匙 1.1 ...

  2. [React] Override webpack config for create-react-app without ejection

    The default service worker that comes with create-react-app doesn't allow for very much configuratio ...

  3. JAVA Eclipse如何重新设置工作空间workspace

    窗口-首选项-常规-启动和关闭,勾选启动时提示工作空间,然后移除现有的工作空间,最好也勾选启动时刷新工作空间   重启之后就可以设置工作空间了  

  4. iOS 设置导航栏 返回按钮文字隐藏

    //隐藏返回按钮文字 [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) f ...

  5. SQL语句练习手册--第四篇

    一.变量那点事儿 1.1 局部变量 (1)声明局部变量 DECLARE @变量名 数据类型 ) DECLARE @id int (2)为变量赋值 SET @变量名 =值 --set用于普通的赋值 SE ...

  6. DeleteDC、ReleaseDC 、DeleteObject的使用

    DeleteDC 该函数删除指定的设备上下文环境(DC). 原型: BOOL DeleteDC(HDC hdc): 参数: hdc:设备上下文环境的句柄. 返回值: 成功,返回非零值:失败,返回零.调 ...

  7. lodash 展平数组 flatten flattenDeep

    _.flatten(array) 向上一级展平数组嵌套 <!DOCTYPE html> <html lang="zh"> <head> < ...

  8. sql server 类oracle vm_contact() 函数创建

    CREATE FUNCTION dbo.fun_orgname(@id int)RETURNS varchar(8000)AS BEGIN      DECLARE @str varchar(8000 ...

  9. java之UDP(datagramsocket,datagramPacket)实例

    import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import ...

  10. angularjs中的路由介绍详解 ui-route

    这篇文章主要介绍了Angularjs中UI Router全攻略,涉及到angularjs ui router的基本用法,需要的朋友参考下吧   首先给大家介绍angular-ui-router的基本用 ...