json

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2021 License: BSD-3-Clause Imports: 3 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

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 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 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