本网站正在建设中(~ ̄▽ ̄)~

  • 你好~!欢迎来到中文歌声合成个人收集站-VCPedia.cn!
  • 若发现页面信息有误投稿至本站,请联系管理员。

Module:MicroJSON

VCPedia.cn ——关于中文歌声合成的一切。
跳到导航 跳到搜索

  1. local export = {}
  2. local function encode_value(value, schema)
  3. if type(value) == "string" then
  4. return export.encode_str(value)
  5. elseif type(value) == "table" then
  6. local first = next(value)
  7. if first == nil then
  8. return (schema and (schema[0] or schema[1])) and "[]" or "{}"
  9. elseif first == 1 then
  10. return export.encode_array(value, schema)
  11. else
  12. return export.encode_object(value, schema)
  13. end
  14. elseif type(value) == "boolean" then
  15. return value and "true" or "false"
  16. end
  17. end
  18. function export.encode_str(str)
  19. return '"' .. tostring(str)
  20. :gsub('["\\]', '\\%0')
  21. :gsub('\b', '\\b')
  22. :gsub('\f', '\\f')
  23. :gsub('\n', '\\n')
  24. :gsub('\r', '\\r')
  25. :gsub('\t', '\\t')
  26. .. '"'
  27. end
  28. function export.encode_array(array, schema)
  29. local output = {}
  30. for i, value in ipairs(array) do
  31. output[#output + 1] = encode_value(value, (type(schema) == "table") and (schema[i] or schema[0]))
  32. end
  33. return "[" .. table.concat(output, ",") .. "]"
  34. end
  35. function export.encode_object(object, schema)
  36. local output = {}
  37. for key, value in pairs(object) do
  38. output[#output + 1] = export.encode_str(key) .. ":" .. encode_value(value, (type(schema) == "table") and (schema[key] or schema[true]))
  39. end
  40. return "{" .. table.concat(output, ",") .. "}"
  41. end
  42. return export