go icon indicating copy to clipboard operation
go copied to clipboard

proposal: encoding/json/jsontext: add Kind.IsValid

Open antoniszymanski opened this issue 3 months ago • 4 comments

Proposal Details

This is a sub-issue of the "encoding/json/v2" proposal (#71497).

I propose adding the following to the encoding/json/jsontext (Switch statement generates longer assembly).

func (k Kind) IsValid() bool {
	return k == 'n' || k == 'f' || k == 't' || k == '"' || k == '0' || k == '{' || k == '}' || k == '[' || k == ']'
}

Rationale

This provides a reliable way to determine whether Kind is valid, because as I learned, this is incorrect.

func (k Kind) IsValid() bool {
	return k != 0
}

antoniszymanski avatar Sep 11 '25 15:09 antoniszymanski

Related Issues

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

gabyhelp avatar Sep 11 '25 15:09 gabyhelp

For reference, this is closely related to #71756, which in some variants proposes making the Kind completely opaque.

prattmic avatar Dec 04 '25 19:12 prattmic

We discussed this in the json/v2 proposal meeting and we're leaning towards:

  1. Adding a KindInvalid constant (see #71756 ).
  2. Making sure the API actually returns KindInvalid for invalid kinds.

The advantage of KindInvalid over an IsValid method is that it can be used within a switch statement (which are commonly used with the Kind type).

dsnet avatar Dec 05 '25 00:12 dsnet

Change https://go.dev/cl/728380 mentions this issue: json/jsontext: normalize all invalid Kinds to 0

gopherbot avatar Dec 08 '25 22:12 gopherbot