Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make go module, marshal userdata as json(null), and encode sparse arrays #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/layeh/gopher-json

go 1.19

require github.com/yuin/gopher-lua v0.0.0-20221210110428-332342483e3f
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/yuin/gopher-lua v0.0.0-20221210110428-332342483e3f h1:wihIB0V/mGpVYrL8I7n/WxVqWnP07CBXZ5uCgxUP1tI=
github.com/yuin/gopher-lua v0.0.0-20221210110428-332342483e3f/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
14 changes: 9 additions & 5 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package json
import (
"encoding/json"
"errors"
"fmt"

"github.com/yuin/gopher-lua"
lua "github.com/yuin/gopher-lua"
)

// Preload adds json to the given Lua state's package.preload table. After it
Expand Down Expand Up @@ -56,7 +57,6 @@ func apiEncode(L *lua.LState) int {

var (
errNested = errors.New("cannot encode recursively nested tables to JSON")
errSparseArray = errors.New("cannot encode sparse array")
errInvalidKeys = errors.New("cannot encode mixed or invalid key types")
)

Expand Down Expand Up @@ -109,10 +109,11 @@ func (j jsonValue) MarshalJSON() (data []byte, err error) {
return
}
if expectedKey != key {
err = errSparseArray
return
errValue := lua.LString(fmt.Sprintf("[%s] = %s", key.String(), value.String()))
arr = append(arr, jsonValue{errValue, j.visited})
} else {
arr = append(arr, jsonValue{value, j.visited})
}
arr = append(arr, jsonValue{value, j.visited})
expectedKey++
key, value = converted.Next(key)
}
Expand All @@ -131,6 +132,9 @@ func (j jsonValue) MarshalJSON() (data []byte, err error) {
default:
err = errInvalidKeys
}
case *lua.LUserData:
data, err = json.Marshal(nil)
return
default:
err = invalidTypeError(j.LValue.Type())
}
Expand Down
64 changes: 60 additions & 4 deletions json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package json

import (
"encoding/json"
"fmt"
"testing"

"github.com/yuin/gopher-lua"
lua "github.com/yuin/gopher-lua"
)

func TestSimple(t *testing.T) {
Expand All @@ -21,9 +22,6 @@ func TestSimple(t *testing.T) {
assert(json.encode({}) == "[]")
assert(json.encode({1, 2, 3}) == "[1,2,3]")

local _, err = json.encode({1, 2, [10] = 3})
assert(string.find(err, "sparse array"))

local _, err = json.encode({1, 2, 3, name = "Tim"})
assert(string.find(err, "mixed or invalid key types"))

Expand Down Expand Up @@ -62,6 +60,8 @@ func TestSimple(t *testing.T) {
a[i] = i
end
assert(json.encode(a) == "[1,2,3,4,5]")

assert(json.encode(json) == nil)
`
s := lua.NewState()
defer s.Close()
Expand Down Expand Up @@ -97,3 +97,59 @@ func TestDecodeValue_jsonNumber(t *testing.T) {
t.Fatalf("expecting LString, got %T", v)
}
}

func TestEncode_SparseArray(t *testing.T) {
tests := []struct {
table string
expected string
}{
{
table: `{
1,
2,
[10] = 3
}`,
expected: `[1,2,"[10] = 3"]`,
},
{
table: `{
nested = {
[37] = "index 37"
}
}`,
expected: `{"nested":["[37] = index 37"]}`,
},
{
table: `{
nested = {
"index 1",
[37] = "index 37"
}
}`,
expected: `{"nested":["index 1","[37] = index 37"]}`,
},
{
table: `{
nested = {
"index 1",
[37] = "index 37"
}
}`,
expected: `{"nested":["index 1","[37] = index 37"]}`,
},
}

for _, test := range tests {
s := lua.NewState()
defer s.Close()
Preload(s)

luaScript := fmt.Sprintf(`
local json = require("json")
local t = %s
assert(json.encode(t) == '%s')`, test.table, test.expected)
if err := s.DoString(luaScript); err != nil {
t.Error(err)
}
}
}