Skip to content

Add JsonArray#collector for Stream<@Nullable JsonElement>#2973

Open
MukjepScarlet wants to merge 5 commits intogoogle:mainfrom
MukjepScarlet:feat/jsonArray.collector
Open

Add JsonArray#collector for Stream<@Nullable JsonElement>#2973
MukjepScarlet wants to merge 5 commits intogoogle:mainfrom
MukjepScarlet:feat/jsonArray.collector

Conversation

@MukjepScarlet
Copy link
Contributor

For Java 8.

JsonObject#collector will be added later if this is accepted.

@MukjepScarlet MukjepScarlet force-pushed the feat/jsonArray.collector branch from 263a54f to 5c33f9b Compare January 22, 2026 17:04
Copy link
Contributor

@Marcono1234 Marcono1234 left a comment

Choose a reason for hiding this comment

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

Thanks! Just a few suggestions, but feel free to ignore them in case you don't think they are useful.

(This is no indiciation though whether these changes will be integrated, the Gson maintainers will decide that.)

MukjepScarlet and others added 3 commits January 23, 2026 11:21
Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>
Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>
Copy link
Member

@eamonnmcmanus eamonnmcmanus left a comment

Choose a reason for hiding this comment

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

I think we're not quite ready for this. We've reverted Gson to requiring Android API Level 23, because that's the minimum that Google currently uses internally. I believe that will be updated to 24 fairly soon, at which point we can change the Gson build to require 24. Then I believe this change will be possible.

I did see a couple of places in Google's internal codebase where people are defining a Collector like this. I'm slightly concerned that referencing JsonArray would pull in a bunch of stream-related classes because of the static final here, even if the calling code doesn't otherwise use streams. Maybe that's not a big deal, though.

@MukjepScarlet
Copy link
Contributor Author

I think if the user is using Java 8+ (Android API 24+), they will definitely import Stream classes somewhere (unless pure Kotlin/Scala), so this could be not a big deal.
By the way in Kotlin I have not found a good way to do similar thing because JsonArray doesn't implements Collection, so I can't use like mapTo(JsonArray()).
I have this:

fun <T> Iterable<T>.mapToJsonArray(transform: (T) -> JsonElement?): JsonArray {
  val arr = if (this is Collection<*>) JsonArray(this.size) else JsonArray()
  for (it in this) arr.add(transform(it))
  return arr
}

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.

3 participants