tidwall / gjson

Get JSON values quickly - JSON parser for Go
MIT License
14.21k stars 847 forks source link

Boolean type #264

Open ShadowJonathan opened 2 years ago

ShadowJonathan commented 2 years ago

I was incredibly surprised to see that there was no gjson.Boolean type to check gjson.Type against.

Instead, there's a True "type" and a False "type", which to me seem to be no "types" at all.

Could those two be collated into a "boolean" type?

tidwall commented 2 years ago

Instead, there's a True "type" and a False "type", which to me seem to be no "types" at all.

The layout and ordering of each Type, including True and False, is important and was chosen to be:

Null < False < Number < String < True < JSON

This is required in order to provide structured ordering when comparing gjson.Result values of different types.

You can see how it's used in the Less function here:

https://github.com/tidwall/gjson/blob/master/gjson.go#L2059-L2082

The original design of gjson, and this feature specifically, was for the purpose of efficient indexing of json values in a buntdb database.

https://github.com/tidwall/buntdb/issues/9#issuecomment-239063958

Could those two be collated into a "boolean" type?

It may be possible, but it would likely break compatibility.

Perhaps an alternative option would be to create a helper function:

func (t Result) IsBool() bool {
    return t.Type == gjson.True || t.Type == gjson.False
}
ShadowJonathan commented 2 years ago

Hmm, alright, I think that the IsBool() function could cover this purpose pretty well then.