Skip to content

Add JsonPrimitive.TRUE, FALSE, and of(boolean) - #3177

Merged
eamonnmcmanus merged 1 commit into
google:mainfrom
eamonnmcmanus:primitive-boolean
Nov 18, 2026
Merged

eamonnmcmanus merged 1 commit into
google:mainfrom
eamonnmcmanus:primitive-boolean

Conversation

@eamonnmcmanus

@eamonnmcmanus eamonnmcmanus commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Introduce public singleton constants JsonPrimitive.TRUE and JsonPrimitive.FALSE along with JsonPrimitive.of(boolean), and use JsonPrimitive.of(...) across Gson (JsonArray, JsonObject, JsonElementTypeAdapter, JsonTreeWriter, and StructTypeAdapter) instead of allocating a new JsonPrimitive instance per boolean value.

We might later want to add other JsonPrimitive.of methods, for example to enable reusing objects for small integer values, but that's a less clear win than for boolean.

We could also conceivably deprecate the JsonPrimitive(boolean) constructor in favour of JsonPrimitive.of(boolean).

Introduce public singleton constants `JsonPrimitive.TRUE` and
`JsonPrimitive.FALSE` along with `JsonPrimitive.of(boolean)`, and use
`JsonPrimitive.of(...)` across Gson (`JsonArray`, `JsonObject`,
`JsonElementTypeAdapter`, `JsonTreeWriter`, and `StructTypeAdapter`)
instead of allocating a new `JsonPrimitive` instance per boolean value.

@cpovirk cpovirk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:)

@eamonnmcmanus
eamonnmcmanus merged commit 7fc4d18 into google:main Nov 18, 2026
22 checks passed
@eamonnmcmanus
eamonnmcmanus deleted the primitive-boolean branch November 18, 2026 23:25
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

Successfully merging this pull request may close these issues.

2 participants