or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

builtin-types.mdcore-serialization.mdindex.mdjson-integration.mdmessagepack-integration.mdsealed-traits.mdstreaming.mdtype-classes.md
tile.json

streaming.mddocs/

Streaming

Functions for writing to streams, OutputStreams, and creating streamable representations. These provide efficient serialization for large data sets and integration with I/O systems.

Capabilities

Writing to java.io.Writer

Writes JSON directly to a java.io.Writer for efficient string output.

/**
 * Write the given Scala value as a JSON string to the given Writer
 * @param t Value to serialize
 * @param out java.io.Writer to write to
 * @param indent Indentation level (-1 for compact, >=0 for pretty printing)
 * @param escapeUnicode Whether to escape unicode characters
 * @param sortKeys Whether to sort object keys alphabetically
 */
def writeTo[T: Writer](t: T, out: java.io.Writer, indent: Int = -1, escapeUnicode: Boolean = false, sortKeys: Boolean = false): Unit

Usage Examples:

import upickle.default._
import java.io.StringWriter

val data = List(Person("Alice", 30), Person("Bob", 25))

// Write to StringWriter
val stringWriter = new StringWriter()
writeTo(data, stringWriter, indent = 2)
val result = stringWriter.toString

// Write to FileWriter
val fileWriter = new java.io.FileWriter("output.json")
try {
  writeTo(data, fileWriter, indent = 2, sortKeys = true)
} finally {
  fileWriter.close()
}

Writing to OutputStream

Writes JSON directly to a java.io.OutputStream as UTF-8 bytes.

/**
 * Write JSON to OutputStream as UTF-8 bytes
 * @param t Value to serialize
 * @param out OutputStream to write to  
 * @param indent Indentation level (-1 for compact, >=0 for pretty printing)
 * @param escapeUnicode Whether to escape unicode characters
 * @param sortKeys Whether to sort object keys alphabetically
 */
def writeToOutputStream[T: Writer](t: T, out: java.io.OutputStream, indent: Int = -1, escapeUnicode: Boolean = false, sortKeys: Boolean = false): Unit

Usage Examples:

import upickle.default._
import java.io.{FileOutputStream, ByteArrayOutputStream}

val data = Map("users" -> List("Alice", "Bob", "Charlie"))

// Write to ByteArrayOutputStream
val byteStream = new ByteArrayOutputStream()
writeToOutputStream(data, byteStream, indent = 2)
val bytes = byteStream.toByteArray

// Write to FileOutputStream  
val fileStream = new FileOutputStream("data.json")
try {
  writeToOutputStream(data, fileStream, sortKeys = true)
} finally {
  fileStream.close()
}

Writing to Byte Array

Writes JSON to a byte array for in-memory handling.

/**
 * Write JSON to byte array
 * @param t Value to serialize
 * @param indent Indentation level (-1 for compact, >=0 for pretty printing)  
 * @param escapeUnicode Whether to escape unicode characters
 * @param sortKeys Whether to sort object keys alphabetically
 * @return UTF-8 encoded JSON as byte array
 */
def writeToByteArray[T: Writer](t: T, indent: Int = -1, escapeUnicode: Boolean = false, sortKeys: Boolean = false): Array[Byte]

Usage Examples:

import upickle.default._

val person = Person("Alice", 30)

// Compact byte array
val compactBytes = writeToByteArray(person)

// Pretty-printed byte array
val prettyBytes = writeToByteArray(person, indent = 2)

// Convert back to string
val jsonString = new String(compactBytes, "UTF-8")

JSON Streaming with geny.Writable

Creates a geny.Writable for efficient streaming JSON output.

/**
 * Write the given Scala value as a JSON string via a geny.Writable
 * @param t Value to serialize
 * @param indent Indentation level (-1 for compact, >=0 for pretty printing)
 * @param escapeUnicode Whether to escape unicode characters  
 * @param sortKeys Whether to sort object keys alphabetically
 * @return geny.Writable with application/json content type
 */
def stream[T: Writer](t: T, indent: Int = -1, escapeUnicode: Boolean = false, sortKeys: Boolean = false): geny.Writable

Usage Examples:

import upickle.default._

val data = List(1, 2, 3, 4, 5)

// Create streamable representation
val writable = stream(data, indent = 2)

// Write to OutputStream
val outputStream = new java.io.FileOutputStream("numbers.json")
try {
  writable.writeBytesTo(outputStream)
} finally {
  outputStream.close()
}

// Use with HTTP libraries that accept geny.Writable
// val response = requests.post("http://api.example.com/data", data = writable)

MessagePack Binary Streaming

Functions for streaming MessagePack binary data.

/**
 * Write MessagePack binary to the given OutputStream
 * @param t Value to serialize
 * @param out OutputStream to write to
 * @param sortKeys Whether to sort object keys alphabetically
 */
def writeBinaryTo[T: Writer](t: T, out: java.io.OutputStream, sortKeys: Boolean = false): Unit

/**
 * Write MessagePack binary to byte array
 * @param t Value to serialize  
 * @param sortKeys Whether to sort object keys alphabetically
 * @return MessagePack byte array
 */
def writeBinaryToByteArray[T: Writer](t: T, sortKeys: Boolean = false): Array[Byte]

/**
 * Write MessagePack binary via a geny.Writable
 * @param t Value to serialize
 * @param sortKeys Whether to sort object keys alphabetically  
 * @return geny.Writable with application/octet-stream content type
 */
def streamBinary[T: Writer](t: T, sortKeys: Boolean = false): geny.Writable

Usage Examples:

import upickle.default._
import java.io.{FileOutputStream, ByteArrayOutputStream}

val data = Map("metrics" -> List(1.2, 3.4, 5.6))

// Write MessagePack to OutputStream
val fileOut = new FileOutputStream("data.msgpack")
try {
  writeBinaryTo(data, fileOut, sortKeys = true)
} finally {
  fileOut.close()
}

// Write to byte array
val binaryBytes = writeBinaryToByteArray(data)

// Create streamable binary representation  
val binaryWritable = streamBinary(data, sortKeys = true)

// Write via geny.Writable
val binaryOut = new ByteArrayOutputStream()
binaryWritable.writeBytesTo(binaryOut)
val result = binaryOut.toByteArray

Large Data Streaming

Efficient patterns for handling large datasets.

Usage Examples:

import upickle.default._
import java.io.{BufferedWriter, FileWriter}

// Streaming large collections
def writeChunkedData[T: Writer](data: Iterator[T], filename: String, chunkSize: Int = 1000): Unit = {
  val writer = new BufferedWriter(new FileWriter(filename))
  try {
    writer.write("[")
    var first = true
    
    data.grouped(chunkSize).foreach { chunk =>
      chunk.foreach { item =>
        if (!first) writer.write(",")
        writeTo(item, writer)
        first = false
      }
    }
    
    writer.write("]")
  } finally {
    writer.close()
  }
}

// Usage with large dataset
val largeDataset: Iterator[LogEntry] = getLargeDataset()
writeChunkedData(largeDataset, "logs.json", chunkSize = 500)

Stream Integration Examples

Integration with common streaming libraries and frameworks.

Usage Examples:

import upickle.default._

// Akka Streams integration
// implicit val system = ActorSystem()
// implicit val materializer = ActorMaterializer()
// 
// val source = Source(List(Person("Alice", 30), Person("Bob", 25)))
// val sink = StreamConverters.fromOutputStream(() => new FileOutputStream("people.json"))
// 
// source
//   .map(person => ByteString(write(person) + "\n"))
//   .runWith(sink)

// FS2 integration  
// val stream = fs2.Stream.emits(List(1, 2, 3, 4, 5))
// val jsonStream = stream.map(n => write(n)).intersperse(",")
// val result = jsonStream.compile.toList

// Play Framework integration
// def streamJson = Action {
//   val data = getData()
//   val writable = stream(data, indent = 2)
//   Ok.chunked(Enumerator.fromStream(writable.getBytes)).as(JSON)
// }

Types

/**
 * geny.Writable represents streamable content with optional HTTP content type
 */
trait geny.Writable {
  def httpContentType: Option[String]
  def writeBytesTo(out: java.io.OutputStream): Unit
}