forked from vadv/gopher-lua-libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
51 lines (46 loc) · 1.08 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package json
import (
"log"
inspect "github.com/alexjx/gopher-lua-libs/inspect"
lua "github.com/yuin/gopher-lua"
)
// json.decode(string)
func ExampleDecode() {
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}}
}
// json.encode(obj)
func ExampleEncode() {
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}}'
// "[]"
}