Skip to content

Use int64 for object or array length #27

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

Merged
merged 1 commit into from
Apr 12, 2024
Merged
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
10 changes: 8 additions & 2 deletions arshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3736,7 +3736,10 @@ func TestMarshal(t *testing.T) {
want: `[null,{},null,{},null,null,{},{},null,{},null,null,{},"LAST"]`,
opts: []Options{
WithMarshalers(func() *Marshalers {
type P [2]int
type P struct {
D int
N int64
}
type PV struct {
P P
V any
Expand Down Expand Up @@ -7784,7 +7787,10 @@ func TestUnmarshal(t *testing.T) {
}),
opts: []Options{
WithUnmarshalers(func() *Unmarshalers {
type P [2]int
type P struct {
D int
N int64
}
type PV struct {
P P
V any
Expand Down
2 changes: 1 addition & 1 deletion jsontext/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,7 @@ func (d *Decoder) StackDepth() int {
// Each name and value in a JSON object is counted separately,
// so the effective number of members would be half the length.
// A complete JSON object must have an even length.
func (d *Decoder) StackIndex(i int) (Kind, int) {
func (d *Decoder) StackIndex(i int) (Kind, int64) {
// NOTE: Keep in sync with Encoder.StackIndex.
switch s := d.s.Tokens.index(i); {
case i > 0 && s.isObject():
Expand Down
2 changes: 1 addition & 1 deletion jsontext/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ func (e *Encoder) StackDepth() int {
// Each name and value in a JSON object is counted separately,
// so the effective number of members would be half the length.
// A complete JSON object must have an even length.
func (e *Encoder) StackIndex(i int) (Kind, int) {
func (e *Encoder) StackIndex(i int) (Kind, int64) {
// NOTE: Keep in sync with Decoder.StackIndex.
switch s := e.s.Tokens.index(i); {
case i > 0 && s.isObject():
Expand Down
6 changes: 3 additions & 3 deletions jsontext/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (m *stateMachine) index(i int) *stateEntry {

// DepthLength reports the current nested depth and
// the length of the last JSON object or array.
func (m stateMachine) DepthLength() (int, int) {
func (m stateMachine) DepthLength() (int, int64) {
return m.Depth(), m.Last.Length()
}

Expand Down Expand Up @@ -342,8 +342,8 @@ const (

// Length reports the number of elements in the JSON object or array.
// Each name and value in an object entry is treated as a separate element.
func (e stateEntry) Length() int {
return int(e & stateCountMask)
func (e stateEntry) Length() int64 {
return int64(e & stateCountMask)
}

// isObject reports whether this is a JSON object.
Expand Down
6 changes: 3 additions & 3 deletions jsontext/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestStateMachine(t *testing.T) {
type operation any
type (
// stackLengths checks the results of stateEntry.length accessors.
stackLengths []int
stackLengths []int64

// appendTokens is sequence of token kinds to append where
// none of them are expected to fail.
Expand Down Expand Up @@ -156,12 +156,12 @@ func TestStateMachine(t *testing.T) {
for _, op := range ops {
switch op := op.(type) {
case stackLengths:
var got []int
var got []int64
for i := 0; i < state.Depth(); i++ {
e := state.index(i)
got = append(got, e.Length())
}
want := []int(op)
want := []int64(op)
if !reflect.DeepEqual(got, want) {
t.Fatalf("%s: stack lengths mismatch:\ngot %v\nwant %v", sequence, got, want)
}
Expand Down