-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.lua
More file actions
85 lines (74 loc) · 2.65 KB
/
example.lua
File metadata and controls
85 lines (74 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
local json = dofile("./json.lua/json.lua")
local toon = require("toon")
print("Version of json.lua: " .. json._version)
print("Version of toon.lua: " .. toon.version)
-- ============================================================================
-- UTILS
-- ============================================================================
--- Reads the entire content of a file.
-- @param filePath (string) The path to the file to read.
-- @return (string) The content of the file.
local function readFile(filePath)
local file, err = io.open(filePath, "r")
if not file then
io.stderr:write("Error: Could not open file '" .. tostring(filePath) .. "': " .. tostring(err) .. "\n")
return ""
end
local content = file:read("*a")
file:close()
return content
end
--- Trim whitespace from both ends of a string.
-- @param s (string) The string to trim.
-- @return (string) The trimmed string.
local function trim(s)
return s and s:match("^%s*(.-)%s*$") or ""
end
--- Convert json string to toon string
-- @param jsonStr (string) json string
-- @return (string) toon string
local function json2Toon(jsonStr)
return toon.encode(json.decode(jsonStr))
end
--- Convert toon string to json string
-- @param toonStr (string) toon string
-- @return (string) json string
local function toon2Json(toonStr)
return json.encode(toon.decode(toonStr))
end
-- Some paired json and toon files provided by `spec/examples/conversions`
local exampleFilsDir = "spec/examples/conversions/";
local files = {
jsonFiles = {
exampleFilsDir .. "api-response.json",
exampleFilsDir .. "config.json",
exampleFilsDir .. "users.json"
},
toonFiles = {
exampleFilsDir .. "api-response.toon",
exampleFilsDir .. "config.toon",
exampleFilsDir .. "users.toon"
}
}
-- ============================================================================
-- JSON TO TOON
-- ============================================================================
for _, path in ipairs(files.jsonFiles) do
local jsonIStr = trim(readFile(path))
local toonOStr = trim(json2Toon(jsonIStr))
print(string.rep("=", 60))
print("Input json: \n" .. jsonIStr)
print()
print("Output toon: \n" .. toonOStr)
end
-- ============================================================================
-- TOON TO JSON
-- ============================================================================
for _, path in ipairs(files.toonFiles) do
local toonIStr = trim(readFile(path))
local jsonOStr = trim(toon2Json(toonIStr))
print(string.rep("=", 60))
print("Input toon: \n" .. toonIStr)
print()
print("Output json: \n" .. jsonOStr)
end