Skip to content

Latest commit

 

History

History
66 lines (47 loc) · 1.92 KB

File metadata and controls

66 lines (47 loc) · 1.92 KB

json

Summary

geode.json converts between JSON text and Luau values, using the same JSON model as geode.Mod.getSavedValue and setSavedValue.

JSON types map to Lua as boolean, number, string, nil (null), array table, and object table. parse returns nil plus an error string past the size or depth cap. dump raises a Lua error past the depth cap. See globals Error shapes and Limits and errors. Unsupported Luau types serialize as JSON null. A sparse array (missing numeric keys) dumps its holes as JSON null.

parse

geode.json.parse(text: string) -> (any?, string?)

Parses a JSON string into a Luau value. On success returns the value. On failure uses globals Error shapes.

local value, err = geode.json.parse('{"a":[1,2,3]}')
if not value then
    print("bad json: " .. err)
else
    print(value.a[2]) -- 2
end

dump

geode.json.dump(value: any, indent: number?) -> string

Serializes a Luau value to a JSON string.

  • If #table > 0, the table becomes a JSON array. Only numeric keys 1..#table are kept. String keys are dropped.
  • Otherwise the table becomes a JSON object. Only string keys are kept.
  • indent controls formatting. The default is compact. A positive number indents with that many spaces, and -1 indents with tabs.
geode.json.dump({ x = true })   -- '{"x":true}'
geode.json.dump({ 1, 2, 3 }, 2) -- Pretty-printed array, 2-space indent.

Limits

Nesting depth and parse size are capped.

See Limits and errors.

Related

Source

  • src/bindings/geode/GeodeSmallBindings.cpp
  • src/bindings/geode/JsonConvert.hpp
  • src/bindings/geode/JsonConvert.cpp
  • tools/luau_codegen/extra_bindings/json.dluau