Skip to content

feat: add readJsonArrayAs for streaming JSON array files#1548

Open
buildingvibes wants to merge 2 commits intozio:series/2.xfrom
buildingvibes:feat/read-json-array-as
Open

feat: add readJsonArrayAs for streaming JSON array files#1548
buildingvibes wants to merge 2 commits intozio:series/2.xfrom
buildingvibes:feat/read-json-array-as

Conversation

@buildingvibes
Copy link
Copy Markdown

Summary

Add readJsonArrayAs methods to the zio.json package that read JSON files containing a top-level array and stream each element individually.

This addresses the use case described in #1071 where users need to read JSON files formatted as arrays (e.g. [{"id": 1}, {"id": 2}]) in a streaming fashion, getting a ZStream[Any, Throwable, A] of each decoded element.

Changes

  • JsonPackagePlatformSpecific.scala: Added readJsonArrayAs[A: JsonDecoder] overloads for File, Path, String, and URL inputs, following the same pattern as the existing readJsonLinesAs family but using JsonStreamDelimiter.Array instead of JsonStreamDelimiter.Newline.
  • DecoderPlatformSpecificSpec.scala: Added tests for reading JSON arrays from both file paths and URLs.
  • log.json: Added test resource file containing a JSON array of events.

Usage

import zio.json._

case class Event(at: Long, message: String)
object Event {
  implicit val decoder: JsonDecoder[Event] = DeriveJsonDecoder.gen[Event]
}

// Stream elements from a JSON array file
val events: ZStream[Any, Throwable, Event] =
  readJsonArrayAs[Event](Paths.get("events.json"))

/claim #1071

Add readJsonArrayAs methods that read JSON files containing a top-level
array and stream each element individually. This complements the existing
readJsonLinesAs methods (which handle newline-delimited JSON) by using
JsonStreamDelimiter.Array instead of JsonStreamDelimiter.Newline.

Methods added for File, Path, String, and URL inputs, following the same
pattern as the existing readJsonLinesAs family.

Closes zio#1071
…tive

The AbstractJsonDecoder and AbstractJsonEncoder classes introduced in
v0.9.0 inherit JVM-specific streaming methods from platform-specific
traits. Since JS and Native have empty platform-specific traits, MiMa
detects these inherited methods as missing when comparing against the
JVM-published artifact. Add wildcard exclusions for both inner classes
in the JS and Native MiMa filters, consistent with the existing
exclusions for JsonDecoderPlatformSpecific and JsonEncoderPlatformSpecific.
@buildingvibes
Copy link
Copy Markdown
Author

Hi! Following up on this PR which adds readJsonArrayAs methods (complementing the existing readJsonLinesAs family). All 25+ CI checks pass including the MiMa compatibility check. The implementation is minimal - just 4 new method overloads using JsonStreamDelimiter.Array. Happy to address any feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant