JSON库

在进行数据传输时JSON格式目前应用广泛,因此从Lua对象与JSON字符串之间相互转换是一个非常常见的功能;目前Lua也有几个JSON库,如:cjson、dkjson。其中cjson的语法严格(比如unicode \u0020\u7eaf),要求符合规范否则会解析失败(如\u002),而dkjson相对宽松,当然也可以通过修改cjson的源码来完成一些特殊要求。而在使用dkjson时也没有遇到性能问题,目前使用的就是dkjson。使用时要特别注意的是大部分JSON库都仅支持UTF-8编码;因此如果你的字符编码是如GBK则需要先转换为UTF-8然后进行处理。

1.1 test_cjson.lua

local cjson = require("cjson")

--lua对象到字符串
local obj = {
id = 1,
name = "zhangsan",
age = nil,
is_male = false,
hobby = {"film", "music", "read"}
} local str = cjson.encode(obj)
ngx.say(str, "<br/>") --字符串到lua对象
str = '{"hobby":["film","music","read"],"is_male":false,"name":"zhangsan","id":1,"age":null}'
local obj = cjson.decode(str) ngx.say(obj.age, "<br/>")
ngx.say(obj.age == nil, "<br/>")
ngx.say(obj.age == cjson.null, "<br/>")
ngx.say(obj.hobby[1], "<br/>") --循环引用
obj = {
id = 1
}
obj.obj = obj
-- Cannot serialise, excessive nesting
--ngx.say(cjson.encode(obj), "<br/>")
local cjson_safe = require("cjson.safe")
--nil
ngx.say(cjson_safe.encode(obj), "<br/>")

null将会转换为cjson.null;循环引用会抛出异常Cannot serialise, excessive nesting,默认解析嵌套深度是1000,可以通过cjson.encode_max_depth()设置深度提高性能;使用cjson.safe不会抛出异常而是返回nil。

1.2 openResty.conf配置文件

    location ~ /lua_cjson {
default_type 'text/html';
lua_code_cache on;
content_by_lua_file /usr/openResty/json/lua/test_cjson.lua;
}

1.3 查看结果

{"is_male":false,"name":"zhangsan","hobby":["film","music","read"],"id":1}
null
false
true
film
nil

lua-cjson文档http://www.kyne.com.au/~mark/software/lua-cjson-manual.html

接下来学习下dkjson。

2.1 下载dkjson库

cd /usr/openResty/lualib
wget http://dkolf.de/src/dkjson-lua.fsl/raw/dkjson.lua?name=16cbc26080996d9da827df42cb0844a25518eeb3 -O dkjson.lua

2.2 test_dkjson.lua

local dkjson = require("dkjson")

--lua对象到字符串
local obj = {
id = 1,
name = "zhangsan",
age = nil,
is_male = false,
hobby = {"film", "music", "read"}
} local str = dkjson.encode(obj, {indent = true})
ngx.say(str, "<br/>") --字符串到lua对象
str = '{"hobby":["film","music","read"],"is_male":false,"name":"zhangsan","id":1,"age":null}'
local obj, pos, err = dkjson.decode(str, 1, nil) ngx.say(obj.age, "<br/>")
ngx.say(obj.age == nil, "<br/>")
ngx.say(obj.hobby[1], "<br/>") --循环引用
obj = {
id = 1
}
obj.obj = obj
--reference cycle
--ngx.say(dkjson.encode(obj), "<br/>")

默认情况下解析的json的字符会有缩排和换行,使用{indent = true}配置将把所有内容放在一行。和cjson不同的是解析json字符串中的null时会得到nil。

2.3 openResty.conf配置文件

    location ~ /lua_dkjson {
default_type 'text/html';
lua_code_cache on;
content_by_lua_file /usr/openResty/lua/json/test_dkjson.lua;
}

2.4 查看结果

{ "is_male":false, "name":"zhangsan", "hobby":["film","music","read"], "id":1 }
nil
true
film

dkjson文档http://dkolf.de/src/dkjson-lua.fsl/homehttp://dkolf.de/src/dkjson-lua.fsl/wiki?name=Documentation

最新文章

  1. paip.输出内容替换在Apache 过滤器filter的设置
  2. JavaScript核心编程(代码片段)
  3. 网易新闻iOS版使用的18个开源组件
  4. ASP.net 上传
  5. javascript活动对象的理解——伪单例模式
  6. js设置radio选中
  7. PHP动态图像处理
  8. 洛谷 [P1963] [NOI2009] 变换序列
  9. ubuntu15.04下sublime text不能输入中文的解决
  10. CLOUD流程设置
  11. 三、K8S成功
  12. BZOJ3676 APIO2014回文串(manacher+后缀自动机)
  13. 画时序图工具TimingDesigner 9.2 安装指导
  14. centos 7 安装 mysql 5.7
  15. Ubuntu 突然上不去网了怎么办
  16. 有关 Windows 10 中&ldquo;适用于 Linux 的 Windows 子系统(Beta)&rdquo;
  17. centos7-默认启动方式改变
  18. [UOJ424]count
  19. IIS7的HTTP到HTTPS的重定向
  20. HikariPool-1 - Exception during pool initialization.

热门文章

  1. java项目采用exe4j打包成exe档
  2. cocos2d-x 源代码分析 总文件夹
  3. 构建自己的PHP框架(composer)
  4. if-then和if-then-else声明
  5. WPF利用VisualTreeHelper遍历寻找对象的子级对象或者父级对象
  6. WPF Button控件模板
  7. WPF属性(二)附加属性
  8. 数据绑定(七)使用ObjectDataProvider对象作为Binding的Source
  9. 【图文教程】de4dot实战字符串解密(演示:hishop微分销系统)
  10. Window Features 总览