proposal: encoding/json/jsontext: add Kind.IsValid
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
}
Related Issues
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
For reference, this is closely related to #71756, which in some variants proposes making the Kind completely opaque.
We discussed this in the json/v2 proposal meeting and we're leaning towards:
- Adding a
KindInvalidconstant (see #71756 ). - Making sure the API actually returns
KindInvalidfor 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).
Change https://go.dev/cl/728380 mentions this issue: json/jsontext: normalize all invalid Kinds to 0