forked from vadv/gopher-lua-libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
35 lines (30 loc) · 730 Bytes
/
api.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
// Package json implements json decode/encode functionality for lua.
// original code: https://github.com/layeh/gopher-json
package json
import (
lua "github.com/yuin/gopher-lua"
)
// Decode lua json.decode(string) returns (table, err)
func Decode(L *lua.LState) int {
str := L.CheckString(1)
value, err := ValueDecode(L, []byte(str))
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
L.Push(value)
return 1
}
// Encode lua json.encode(obj) returns (string, err)
func Encode(L *lua.LState) int {
value := L.CheckAny(1)
data, err := ValueEncode(value)
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
L.Push(lua.LString(string(data)))
return 1
}