Modul:MicroJSON
Dokumentacija modula | [uredi] [historija] [ ] |
Ova dokumentacija je prikazana u Modul:MicroJSON/dok. |
Ovo je meta-modul i ne može se koristiti u šablonima, nego samo u drugim modulima.
local export = {}
local function encode_value(value, schema)
if type(value) == "string" then
return export.encode_str(value)
elseif type(value) == "table" then
local first = next(value)
if first == nil then
return (schema and (schema[0] or schema[1])) and "[]" or "{}"
elseif first == 1 then
return export.encode_array(value, schema)
else
return export.encode_object(value, schema)
end
elseif type(value) == "boolean" then
return value and "true" or "false"
end
end
function export.encode_str(str)
return '"' .. tostring(str):gsub('["\\]', '\\%0') .. '"'
end
function export.encode_array(array, schema)
local output = {}
for i, value in ipairs(array) do
table.insert(output, encode_value(value, (type(schema) == "table") and (schema[i] or schema[0])))
end
return "[" .. table.concat(output, ",") .. "]"
end
function export.encode_object(object, schema)
local output = {}
for key, value in pairs(object) do
table.insert(output, export.encode_str(key) .. ":" .. encode_value(value, (type(schema) == "table") and (schema[key] or schema[true])))
end
return "{" .. table.concat(output, ",") .. "}"
end
return export