package json import ( luajit "git.sharkk.net/Sky/LuaJIT-to-Go" "github.com/goccy/go-json" ) // GetJSONFunctions returns all JSON-related Go functions func GetJSONFunctions() map[string]luajit.GoFunction { return map[string]luajit.GoFunction{ "json_encode": func(s *luajit.State) int { if err := s.CheckMinArgs(1); err != nil { return s.PushError("json_encode: %v", err) } value, err := s.ToValue(1) if err != nil { return s.PushError("json_encode: failed to read value: %v", err) } data, err := json.Marshal(value) if err != nil { return s.PushError("json_encode: %v", err) } s.PushString(string(data)) return 1 }, "json_decode": func(s *luajit.State) int { if err := s.CheckMinArgs(1); err != nil { return s.PushError("json_decode: %v", err) } jsonStr, err := s.SafeToString(1) if err != nil { return s.PushError("json_decode: input must be a string") } var result any if err := json.Unmarshal([]byte(jsonStr), &result); err != nil { // Return nil and error string instead of PushError for JSON parsing errors s.PushNil() s.PushString(err.Error()) return 2 } if err := s.PushValue(result); err != nil { return s.PushError("json_decode: failed to push result: %v", err) } return 1 }, } }