46 lines
803 B
Go
46 lines
803 B
Go
package json
|
|
|
|
import (
|
|
luajit "git.sharkk.net/Sky/LuaJIT-to-Go"
|
|
"github.com/goccy/go-json"
|
|
)
|
|
|
|
func GetFunctionList() map[string]luajit.GoFunction {
|
|
return map[string]luajit.GoFunction{
|
|
"json_encode": json_encode,
|
|
"json_decode": json_decode,
|
|
}
|
|
}
|
|
|
|
func json_encode(s *luajit.State) int {
|
|
value, err := s.ToValue(1)
|
|
if err != nil {
|
|
s.PushNil()
|
|
s.PushString("failed to read value")
|
|
return 2
|
|
}
|
|
|
|
data, err := json.Marshal(value)
|
|
if err != nil {
|
|
s.PushNil()
|
|
s.PushString("encoding failed")
|
|
return 2
|
|
}
|
|
|
|
s.PushString(string(data))
|
|
return 1
|
|
}
|
|
|
|
func json_decode(s *luajit.State) int {
|
|
jsonStr := s.ToString(1)
|
|
var result any
|
|
if err := json.Unmarshal([]byte(jsonStr), &result); err != nil {
|
|
s.PushNil()
|
|
s.PushString("invalid JSON")
|
|
return 2
|
|
}
|
|
|
|
s.PushValue(result)
|
|
return 1
|
|
}
|