Add LuaJIT converter scripts between JSON and YAML

This commit is contained in:
Talia 2019-07-12 10:23:11 +02:00
parent 352f7756d6
commit ee011355f2
2 changed files with 42 additions and 0 deletions

21
bin/json2yaml Executable file
View File

@ -0,0 +1,21 @@
#!/usr/bin/env luajit
local json = require 'cjson'
local yaml = require 'lyaml'
local document = json.decode(io.read('*a'))
local function replace(document)
for key, value in pairs(document) do
if value == json.null then
document[key] = yaml.null
elseif type(value) == 'table' then
replace(value)
elseif type(value) == 'userdata' then
document[key] = '<USERDATA>'
end
end
return document
end
print(yaml.dump({replace(document)}))

21
bin/yaml2json Executable file
View File

@ -0,0 +1,21 @@
#!/usr/bin/env luajit
local json = require 'cjson'
local yaml = require 'lyaml'
local document = yaml.load(io.read('*a'))
local function replace(document)
for key, value in pairs(document) do
if value == yaml.null then
document[key] = json.null
elseif type(value) == 'table' then
replace(value)
elseif type(value) == 'userdata' then
document[key] = '<USERDATA>'
end
end
return document
end
print(json.encode(replace(document)))