json

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: May 6, 2022 License: BSD-3-Clause Imports: 4 Imported by: 0

README

json GoDoc

Usage

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

-- json.encode()
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.encode") end

-- json.decode()
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.decode") 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 = yaml.new_decoder(reader)
result, err = decoder:decode()
f:close()
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
    print(inspect(result, {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 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