Add JsonArray#collector for Stream<@Nullable JsonElement>#2973
Add JsonArray#collector for Stream<@Nullable JsonElement>#2973MukjepScarlet wants to merge 5 commits intogoogle:mainfrom
JsonArray#collector for Stream<@Nullable JsonElement>#2973Conversation
263a54f to
5c33f9b
Compare
Marcono1234
left a comment
There was a problem hiding this comment.
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.)
Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>
Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>
eamonnmcmanus
left a comment
There was a problem hiding this comment.
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.
|
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. 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
} |
For Java 8.
JsonObject#collectorwill be added later if this is accepted.