lua2json
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的更多相关文章
随机推荐
- jQuery中dom对象与jQuery对象之间互相转换
首先介绍一下什么是dom对象什么时候jQuery对象 1.dom对象就是使用原生js的api获取到的对象就是dom对象 eg: var box1 = document.getElementById(& ...
- 配置php扩展redis
环境说明: 系统版本 CentOS 6.9 x86_64 软件版本 nginx-1.12.2 php-5.5.38 yum安装redis3.2.11 php扩展 ...
- 安装openstack 时 遇见的一些问题及解决方法!
感谢朋友支持本博客,欢迎共同探讨交流.因为能力和时间有限,错误之处在所难免.欢迎指正! 假设转载.请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...
- node - 关于package.json
2018-8-3(首次更新) 一.关于版本号: 文章来自:https://blog.csdn.net/yancloudfrontend/article/details/72867314 指定版本:比如 ...
- scrapy 安装详解
一. Scrapy简介 Scrapy is a fast high-level screen scraping and web crawling framework, used to crawl we ...
- sone1动态树
这尼吗桑心病狂的动态树:http://www.lydsy.com/JudgeOnline/problem.php?id=3153 终于让哥以一种碉堡的姿势过了: 牛B轰轰的最后两个都是我的...无法超 ...
- JavaScript对象浅复制
1.概述 Object.assign方法用于对象的合并,将源对象(source)的所有可枚举属性,复制到目标对象(target). 注意,如果目标对象与源对象有同名属性,或多个源对象有同名属性,则后面 ...
- poj 1390 Blocks (经典区间dp 方块消除)
Blocks Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4250 Accepted: 1704 Descriptio ...
- hdu4888 多校B 最大流以及最大流唯一推断+输出方案
题意.给一个矩阵,告诉你每行和.每列和.而且限制所填数不大于k,问矩阵是否唯一. 经典建图不说了.第一次遇到推断最大流唯一性的.学习了:用dfs来推断残网中是否还存在环,若存在,则表明绕这个环走一圈, ...
- [经验总结]material design效果与开发总结
首先贴一个參考过的文章,写的不错: 在低版本号android系统上实现Material design应用 以下是工作中总结出来的,列出了在<5.0的设备是怎样实现material design的 ...