From fe2fbd04eca6779dddfce6dbbc565e050a05ff1f Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Sun, 9 Sep 2018 00:38:24 +0200 Subject: db: add trivial json db reader --- db.lua | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'db.lua') diff --git a/db.lua b/db.lua index 4e7434a..0a9d0f4 100644 --- a/db.lua +++ b/db.lua @@ -12,6 +12,7 @@ local pcall = pcall local io = require "io" local iostdout = io.stdout +local iostdin = io.stdin local common = require "common" local common_status = common.status @@ -22,10 +23,25 @@ local errorln = common.errorln local noiseln = common.noiseln local debugln = common.debugln +local json_internalize = function (jsdata) + local jsdec = cjson.new () + local parsed = jsdec.decode (jsdata) + if parsed == nil then return status_error end + return status_ok, parsed +end + ------------------------------------------------------------------------------- --- API ------------------------------------------------------------------------------- +local read_json_db = function (cal, fh) + fh = fh or iostdin + local jsdata = fh:read "*all" + fh:close () + if jsdata == nil then return status_error end + return json_internalize (jsdata) +end + local output_calendar = function (cal, fh) fh = fh or iostdout local jsenc = cjson.new () @@ -39,5 +55,6 @@ local output_calendar = function (cal, fh) end return { output_calendar = output_calendar + , read_json_db = read_json_db } -- cgit v1.2.3