|
| 1 | +package json |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + |
| 7 | + "github.com/yuin/gopher-lua" |
| 8 | +) |
| 9 | + |
| 10 | +// Preload adds json to the given Lua state's package.preload table. After it |
| 11 | +// has been preloaded, it can be loaded using require: |
| 12 | +// |
| 13 | +// local json = require("json") |
| 14 | +func Preload(L *lua.LState) { |
| 15 | + L.PreloadModule("json", Loader) |
| 16 | +} |
| 17 | + |
| 18 | +// Loader is the module loader function. |
| 19 | +func Loader(L *lua.LState) int { |
| 20 | + t := L.NewTable() |
| 21 | + L.SetFuncs(t, api) |
| 22 | + L.Push(t) |
| 23 | + return 1 |
| 24 | +} |
| 25 | + |
| 26 | +var api = map[string]lua.LGFunction{ |
| 27 | + "decode": apiDecode, |
| 28 | + "encode": apiEncode, |
| 29 | +} |
| 30 | + |
| 31 | +func apiDecode(L *lua.LState) int { |
| 32 | + if L.GetTop() != 1 { |
| 33 | + L.Error(lua.LString("bad argument #1 to decode"), 1) |
| 34 | + return 0 |
| 35 | + } |
| 36 | + str := L.CheckString(1) |
| 37 | + |
| 38 | + value, err := Decode(L, []byte(str)) |
| 39 | + if err != nil { |
| 40 | + L.Push(lua.LNil) |
| 41 | + L.Push(lua.LString(err.Error())) |
| 42 | + return 2 |
| 43 | + } |
| 44 | + L.Push(value) |
| 45 | + return 1 |
| 46 | +} |
| 47 | + |
| 48 | +func apiEncode(L *lua.LState) int { |
| 49 | + if L.GetTop() != 1 { |
| 50 | + L.Error(lua.LString("bad argument #1 to encode"), 1) |
| 51 | + return 0 |
| 52 | + } |
| 53 | + value := L.CheckAny(1) |
| 54 | + |
| 55 | + data, err := Encode(value) |
| 56 | + if err != nil { |
| 57 | + L.Push(lua.LNil) |
| 58 | + L.Push(lua.LString(err.Error())) |
| 59 | + return 2 |
| 60 | + } |
| 61 | + L.Push(lua.LString(string(data))) |
| 62 | + return 1 |
| 63 | +} |
| 64 | + |
| 65 | +var ( |
| 66 | + errNested = errors.New("cannot encode recursively nested tables to JSON") |
| 67 | + errSparseArray = errors.New("cannot encode sparse array") |
| 68 | + errInvalidKeys = errors.New("cannot encode mixed or invalid key types") |
| 69 | +) |
| 70 | + |
| 71 | +type invalidTypeError lua.LValueType |
| 72 | + |
| 73 | +func (i invalidTypeError) Error() string { |
| 74 | + return `cannot encode ` + lua.LValueType(i).String() + ` to JSON` |
| 75 | +} |
| 76 | + |
| 77 | +// Encode returns the JSON encoding of value. |
| 78 | +func Encode(value lua.LValue) ([]byte, error) { |
| 79 | + return json.Marshal(jsonValue{ |
| 80 | + LValue: value, |
| 81 | + visited: make(map[*lua.LTable]bool), |
| 82 | + }) |
| 83 | +} |
| 84 | + |
| 85 | +type jsonValue struct { |
| 86 | + lua.LValue |
| 87 | + visited map[*lua.LTable]bool |
| 88 | +} |
| 89 | + |
| 90 | +func (j jsonValue) MarshalJSON() (data []byte, err error) { |
| 91 | + switch converted := j.LValue.(type) { |
| 92 | + case lua.LBool: |
| 93 | + data, err = json.Marshal(bool(converted)) |
| 94 | + case lua.LNumber: |
| 95 | + data, err = json.Marshal(float64(converted)) |
| 96 | + case *lua.LNilType: |
| 97 | + data = []byte(`null`) |
| 98 | + case lua.LString: |
| 99 | + data, err = json.Marshal(string(converted)) |
| 100 | + case *lua.LTable: |
| 101 | + if j.visited[converted] { |
| 102 | + return nil, errNested |
| 103 | + } |
| 104 | + j.visited[converted] = true |
| 105 | + |
| 106 | + key, value := converted.Next(lua.LNil) |
| 107 | + |
| 108 | + switch key.Type() { |
| 109 | + case lua.LTNil: // empty table |
| 110 | + data = []byte(`[]`) |
| 111 | + case lua.LTNumber: |
| 112 | + arr := make([]jsonValue, 0, converted.Len()) |
| 113 | + expectedKey := lua.LNumber(1) |
| 114 | + for key != lua.LNil { |
| 115 | + if key.Type() != lua.LTNumber { |
| 116 | + err = errInvalidKeys |
| 117 | + return |
| 118 | + } |
| 119 | + if expectedKey != key { |
| 120 | + err = errSparseArray |
| 121 | + return |
| 122 | + } |
| 123 | + arr = append(arr, jsonValue{value, j.visited}) |
| 124 | + expectedKey++ |
| 125 | + key, value = converted.Next(key) |
| 126 | + } |
| 127 | + data, err = json.Marshal(arr) |
| 128 | + case lua.LTString: |
| 129 | + obj := make(map[string]jsonValue) |
| 130 | + for key != lua.LNil { |
| 131 | + if key.Type() != lua.LTString { |
| 132 | + err = errInvalidKeys |
| 133 | + return |
| 134 | + } |
| 135 | + obj[key.String()] = jsonValue{value, j.visited} |
| 136 | + key, value = converted.Next(key) |
| 137 | + } |
| 138 | + data, err = json.Marshal(obj) |
| 139 | + default: |
| 140 | + err = errInvalidKeys |
| 141 | + } |
| 142 | + default: |
| 143 | + err = invalidTypeError(j.LValue.Type()) |
| 144 | + } |
| 145 | + return |
| 146 | +} |
| 147 | + |
| 148 | +// Decode converts the JSON encoded data to Lua values. |
| 149 | +func Decode(L *lua.LState, data []byte) (lua.LValue, error) { |
| 150 | + var value interface{} |
| 151 | + err := json.Unmarshal(data, &value) |
| 152 | + if err != nil { |
| 153 | + return nil, err |
| 154 | + } |
| 155 | + return DecodeValue(L, value), nil |
| 156 | +} |
| 157 | + |
| 158 | +// DecodeValue converts the value to a Lua value. |
| 159 | +// |
| 160 | +// This function only converts values that the encoding/json package decodes to. |
| 161 | +// All other values will return lua.LNil. |
| 162 | +func DecodeValue(L *lua.LState, value interface{}) lua.LValue { |
| 163 | + switch converted := value.(type) { |
| 164 | + case bool: |
| 165 | + return lua.LBool(converted) |
| 166 | + case float64: |
| 167 | + return lua.LNumber(converted) |
| 168 | + case string: |
| 169 | + return lua.LString(converted) |
| 170 | + case json.Number: |
| 171 | + return lua.LString(converted) |
| 172 | + case []interface{}: |
| 173 | + arr := L.CreateTable(len(converted), 0) |
| 174 | + for _, item := range converted { |
| 175 | + arr.Append(DecodeValue(L, item)) |
| 176 | + } |
| 177 | + return arr |
| 178 | + case map[string]interface{}: |
| 179 | + tbl := L.CreateTable(0, len(converted)) |
| 180 | + for key, item := range converted { |
| 181 | + tbl.RawSetH(lua.LString(key), DecodeValue(L, item)) |
| 182 | + } |
| 183 | + return tbl |
| 184 | + case nil: |
| 185 | + return lua.LNil |
| 186 | + } |
| 187 | + |
| 188 | + return lua.LNil |
| 189 | +} |
0 commit comments