From ee011355f205bf503a1545cbd144efa23b300881 Mon Sep 17 00:00:00 2001 From: DarkWiiPlayer Date: Fri, 12 Jul 2019 10:23:11 +0200 Subject: [PATCH] Add LuaJIT converter scripts between JSON and YAML --- bin/json2yaml | 21 +++++++++++++++++++++ bin/yaml2json | 21 +++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100755 bin/json2yaml create mode 100755 bin/yaml2json diff --git a/bin/json2yaml b/bin/json2yaml new file mode 100755 index 0000000..3966b72 --- /dev/null +++ b/bin/json2yaml @@ -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] = '' + end + end + return document +end + +print(yaml.dump({replace(document)})) diff --git a/bin/yaml2json b/bin/yaml2json new file mode 100755 index 0000000..112cbe2 --- /dev/null +++ b/bin/yaml2json @@ -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] = '' + end + end + return document +end + +print(json.encode(replace(document)))