项目作者: diegogub

项目描述 :
json patch
高级语言: Lua
项目地址: git://github.com/diegogub/lua-jsonpatch.git
创建时间: 2019-06-04T08:49:30Z
项目社区:https://github.com/diegogub/lua-jsonpatch

开源协议:

下载


lua-jsonpatch

Supported Operations / TODO

  • replace / rp
  • add / a
  • remove / rm
  • move / mv
  • copy / cp
  • test / t

Install

  1. luarocks install lua-jsonpatch

Usage

  1. local jpatch = require "lua-jsonpatch"
  2. local json = require "json"
  3. local obj = { test = { mypath = "", m = "0"} , arr = { sub = { 0, 1 ,2 ,3 }}}
  4. local patch = {
  5. { op = "replace", path = "/test/mypath", value="change it"},
  6. { op = "replace", path = "/arr/sub/0", value=10},
  7. { op = "rp", path = "/arr/sub/1", value=20},
  8. { op = "add", path = "/arr/sub/-", value=100},
  9. { op = "rm", path = "/arr/sub/0", value=100},
  10. { op = "move", from = "/test/m", path="/arr"},
  11. { op = "copy", from = "/test/mypath", path="/arr"},
  12. }
  13. local err = jpatch.apply(obj,patch)
  14. if err then
  15. print(err)
  16. end
  17. -- possible to compress size of patches
  18. local c_patch , err = jpatch.compress(patch)
  19. if not err then
  20. print(json.encode(c_patch))
  21. end
  22. local d_patch , err = jpatch.decompress(c_patch)
  23. if not err then
  24. print(json.encode(d_patch))
  25. end

Testing

  1. luarocks install busted
  2. busted