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

Boolean type #264

Open
ShadowJonathan opened this issue Jan 19, 2022 · 2 comments
Open

Boolean type #264

ShadowJonathan opened this issue Jan 19, 2022 · 2 comments

Comments

@ShadowJonathan
Copy link

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
Copy link
Owner

tidwall commented Jan 19, 2022

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.

tidwall/buntdb#9 (comment)

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
Copy link
Author

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

tidwall added a commit that referenced this issue Feb 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants