json

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 14, 2023 License: BSD-3-Clause Imports: 4 Imported by: 22

README

json GoDoc

Usage

local json = require("json")
local inspect = require("inspect")

-- json.decode()
local jsonString = [[
    {
        "a": {"b":1}
    }
]]
local result, err = json.decode(jsonString)
if err then
    error(err)
end
local result = inspect(result, { newline = "", indent = "" })
if not (result == "{a = {b = 1}}") then
    error("json.decode")
end

-- json.encode()
local table = { a = { b = 1 } }
local result, err = json.encode(table)
if err then
    error(err)
end
local result = inspect(result, { newline = "", indent = "" })
if not (result == [['{"a":{"b":1}}']]) then
    error("json.encode")
end
decoder

Using a decoder allows reading from file or strings.Reader with input that has multiple values

  • With file
local json = require("json")
local io = require("io")
local inspect = require("inspect")

f, err = io.open("myfile.json", "r")
assert(not err, err)
decoder = json.new_decoder(f)
result, err = decoder:decode()
f:close()
assert(not err, err)
print(inspect(result))
  • With strings.Reader
local json = require("json")
local strings = require("strings")
local inspect = require("inspect")

reader = strings.new_reader([[
{
  "foo": "bar",
  "num": 123,
  "arr": ["abc", "def", "ghi"]
}
]])
decoder = json.new_decoder(reader)
result, err = decoder:decode()
assert(not err, err)
print(inspect(result))
encoder

Using an allows writing to file or strings.Builder and to write multiple values if desired

  • with file
local json = require("json")
local io = require("io")

f, err = io.open('myfile.json', 'w')
assert(not err, err)
encoder = json.new_encoder(f)
err = encoder:encode({ abc = "def", num = 123, arr = { 1, 2, 3 } })
assert(not err, err)
  • with strings.Builder
local json = require("json")
local strings = require("strings")

writer = strings.new_builder()
encoder = json.new_encoder(writer)
err = encoder:encode({ abc = "def", num = 123, arr = { 1, 2, 3 } })
assert(not err, err)
s = writer:string()
print(s)
  • with strings.Builder pretty printed
local json = require("json")
local strings = require("strings")

writer = strings.new_builder()
encoder = json.new_encoder(writer)
encoder:set_indent('', "  ")
err = encoder:encode({ abc = "def", num = 123, arr = { 1, 2, 3 } })
assert(not err, err)
s = writer:string()
print(s)

Documentation

Overview

Package json implements json decode/encode functionality for lua. original code: https://github.com/layeh/gopher-json

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckJSONDecoder added in v0.1.6

func CheckJSONDecoder(L *lua.LState, n int) *json.Decoder

func CheckJSONEncoder added in v0.1.6

func CheckJSONEncoder(L *lua.LState, n int) *json.Encoder

func Decode

func Decode(L *lua.LState) int

Decode lua json.decode(string) returns (table, err)

Example

json.decode(string)

state := lua.NewState()
Preload(state)
inspect.Preload(state)
source := `
    local json = require("json")
    local inspect = require("inspect")
    local jsonString = [[{"a":{"b":1}}]]
    local result, err = json.decode(jsonString)
    if err then error(err) end
	local remove_all_metatables = function(item, path)
	  if path[#path] ~= inspect.METATABLE then return item end
	end
    print(inspect(result, {process = remove_all_metatables, newline="", indent=""}))
`
if err := state.DoString(source); err != nil {
	log.Fatal(err.Error())
}
Output:

{a = {b = 1}}

func Encode

func Encode(L *lua.LState) int

Encode lua json.encode(obj) returns (string, err)

Example

json.encode(obj)

state := lua.NewState()
Preload(state)
inspect.Preload(state)
source := `
    local json = require("json")
    local inspect = require("inspect")
    local table = {a={b=1}}
    local result, err = json.encode(table)
    if err then error(err) end
    print(inspect(result, {newline="", indent=""}))

	print(inspect( json.encode( {} ) ))
`
if err := state.DoString(source); err != nil {
	log.Fatal(err.Error())
}
Output:

'{"a":{"b":1}}'
"[]"

func LVJSONDecoder added in v0.1.6

func LVJSONDecoder(L *lua.LState, decoder *json.Decoder) lua.LValue

func LVJSONEncoder added in v0.1.6

func LVJSONEncoder(L *lua.LState, encoder *json.Encoder) lua.LValue

func Loader

func Loader(L *lua.LState) int

Loader is the module loader function.

func Preload

func Preload(L *lua.LState)

Preload adds json to the given Lua state's package.preload table. After it has been preloaded, it can be loaded using require:

local json = require("json")

func TableIsObject added in v0.3.1

func TableIsObject(L *lua.LState) int

TableIsObject lua json.tableIsObject marks a table as an object (to distinguish between [] and {})

func ValueDecode

func ValueDecode(L *lua.LState, data []byte) (lua.LValue, error)

ValueDecode converts the JSON encoded data to Lua values.

func ValueEncode

func ValueEncode(value lua.LValue) ([]byte, error)

ValueEncode returns the JSON encoding of value.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL