Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved okio support #1982

Merged
merged 5 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.fasterxml.jackson.databind.*
import com.fasterxml.jackson.module.kotlin.*
import kotlinx.serialization.*
import kotlinx.serialization.json.*
import kotlinx.serialization.json.okio.encodeToSink
import kotlinx.serialization.json.okio.encodeToBufferedSink
import okio.blackholeSink
import okio.buffer
import org.openjdk.jmh.annotations.*
Expand Down Expand Up @@ -97,7 +97,7 @@ open class JacksonComparisonBenchmark {
fun kotlinToStream() = Json.encodeToStream(DefaultPixelEvent.serializer(), data, devNullStream)

@Benchmark
fun kotlinToOkio() = Json.encodeToSink(DefaultPixelEvent.serializer(), data, devNullSink)
fun kotlinToOkio() = Json.encodeToBufferedSink(DefaultPixelEvent.serializer(), data, devNullSink)

@Benchmark
fun kotlinToStringWithEscapes(): String = Json.encodeToString(DefaultPixelEvent.serializer(), dataWithEscapes)
Expand All @@ -109,7 +109,7 @@ open class JacksonComparisonBenchmark {
fun kotlinSmallToStream() = Json.encodeToStream(SmallDataClass.serializer(), smallData, devNullStream)

@Benchmark
fun kotlinSmallToOkio() = Json.encodeToSink(SmallDataClass.serializer(), smallData, devNullSink)
fun kotlinSmallToOkio() = Json.encodeToBufferedSink(SmallDataClass.serializer(), smallData, devNullSink)

@Benchmark
fun jacksonFromString(): DefaultPixelEvent = objectMapper.readValue(stringData, DefaultPixelEvent::class.java)
Expand Down
8 changes: 4 additions & 4 deletions formats/json-okio/api/kotlinx-serialization-json-okio.api
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
public final class kotlinx/serialization/json/okio/OkioStreamsKt {
public static final fun decodeFromSource (Lkotlinx/serialization/json/Json;Lkotlinx/serialization/DeserializationStrategy;Lokio/Source;)Ljava/lang/Object;
public static final fun decodeSourceToSequence (Lkotlinx/serialization/json/Json;Lokio/Source;Lkotlinx/serialization/DeserializationStrategy;Lkotlinx/serialization/json/DecodeSequenceMode;)Lkotlin/sequences/Sequence;
public static synthetic fun decodeSourceToSequence$default (Lkotlinx/serialization/json/Json;Lokio/Source;Lkotlinx/serialization/DeserializationStrategy;Lkotlinx/serialization/json/DecodeSequenceMode;ILjava/lang/Object;)Lkotlin/sequences/Sequence;
public static final fun encodeToSink (Lkotlinx/serialization/json/Json;Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;Lokio/Sink;)V
public static final fun decodeBufferedSourceToSequence (Lkotlinx/serialization/json/Json;Lokio/BufferedSource;Lkotlinx/serialization/DeserializationStrategy;Lkotlinx/serialization/json/DecodeSequenceMode;)Lkotlin/sequences/Sequence;
public static synthetic fun decodeBufferedSourceToSequence$default (Lkotlinx/serialization/json/Json;Lokio/BufferedSource;Lkotlinx/serialization/DeserializationStrategy;Lkotlinx/serialization/json/DecodeSequenceMode;ILjava/lang/Object;)Lkotlin/sequences/Sequence;
public static final fun decodeFromBufferedSource (Lkotlinx/serialization/json/Json;Lkotlinx/serialization/DeserializationStrategy;Lokio/BufferedSource;)Ljava/lang/Object;
public static final fun encodeToBufferedSink (Lkotlinx/serialization/json/Json;Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;Lokio/BufferedSink;)V
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Copyright 2017-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")

package kotlinx.serialization.json.okio

import kotlinx.serialization.*
Expand All @@ -16,19 +18,16 @@ import okio.*
/**
* Serializes the [value] with [serializer] into a [target] using JSON format and UTF-8 encoding.
*
* If [target] is not a [BufferedSink] then called [Sink.buffer] for it to create buffered wrapper.
*
* @throws [SerializationException] if the given value cannot be serialized to JSON.
* @throws [okio.IOException] If an I/O error occurs and sink can't be written to.
*/
@ExperimentalSerializationApi
public fun <T> Json.encodeToSink(
public fun <T> Json.encodeToBufferedSink(
serializer: SerializationStrategy<T>,
value: T,
target: Sink
target: BufferedSink
) {
val buffered = if (target is BufferedSink) target else target.buffer()
val writer = JsonToOkioStreamWriter(buffered)
val writer = JsonToOkioStreamWriter(target)
try {
encodeByWriter(writer, serializer, value)
} finally {
Expand All @@ -45,54 +44,47 @@ public fun <T> Json.encodeToSink(
* @throws [okio.IOException] If an I/O error occurs and sink can't be written to.
*/
@ExperimentalSerializationApi
public inline fun <reified T> Json.encodeToSink(
public inline fun <reified T> Json.encodeToBufferedSink(
value: T,
target: Sink
): Unit = encodeToSink(serializersModule.serializer(), value, target)
target: BufferedSink
): Unit = encodeToBufferedSink(serializersModule.serializer(), value, target)


/**
* Deserializes JSON from [source] using UTF-8 encoding to a value of type [T] using [deserializer].
*
* If [source] is not a [BufferedSource] then called [Source.buffer] for it to create buffered wrapper.
*
* Note that this functions expects that exactly one object would be present in the source
shanshin marked this conversation as resolved.
Show resolved Hide resolved
* and throws an exception if there are any dangling bytes after an object.
*
* @throws [SerializationException] if the given JSON input cannot be deserialized to the value of type [T].
* @throws [okio.IOException] If an I/O error occurs and source can't be read from.
*/
@ExperimentalSerializationApi
public fun <T> Json.decodeFromSource(
public fun <T> Json.decodeFromBufferedSource(
deserializer: DeserializationStrategy<T>,
source: Source
source: BufferedSource
): T {
val buffered = if (source is BufferedSource) source else source.buffer()
return decodeByReader(deserializer, OkioSerialReader(buffered))
return decodeByReader(deserializer, OkioSerialReader(source))
}

/**
* Deserializes the contents of given [source] to the value of type [T] using UTF-8 encoding and
* deserializer retrieved from the reified type parameter.
*
* If [source] is not a [BufferedSource] then called [Source.buffer] for it to create buffered wrapper.
*
* Note that this functions expects that exactly one object would be present in the stream
shanshin marked this conversation as resolved.
Show resolved Hide resolved
* and throws an exception if there are any dangling bytes after an object.
*
* @throws [SerializationException] if the given JSON input cannot be deserialized to the value of type [T].
* @throws [okio.IOException] If an I/O error occurs and source can't be read from.
*/
@ExperimentalSerializationApi
public inline fun <reified T> Json.decodeFromSource(source: Source): T =
decodeFromSource(serializersModule.serializer(), source)
public inline fun <reified T> Json.decodeFromBufferedSource(source: BufferedSource): T =
decodeFromBufferedSource(serializersModule.serializer(), source)


/**
* Transforms the given [source] into lazily deserialized sequence of elements of type [T] using UTF-8 encoding and [deserializer].
* Unlike [decodeFromSource], [source] is allowed to have more than one element, separated as [format] declares.
*
* If [source] is not a [BufferedSource] then called [Source.buffer] for it to create buffered wrapper.
* Unlike [decodeFromBufferedSource], [source] is allowed to have more than one element, separated as [format] declares.
*
* Elements must all be of type [T].
* Elements are parsed lazily when resulting [Sequence] is evaluated.
Expand All @@ -106,20 +98,17 @@ public inline fun <reified T> Json.decodeFromSource(source: Source): T =
* @throws [okio.IOException] If an I/O error occurs and source can't be read from.
*/
@ExperimentalSerializationApi
public fun <T> Json.decodeSourceToSequence(
source: Source,
public fun <T> Json.decodeBufferedSourceToSequence(
source: BufferedSource,
deserializer: DeserializationStrategy<T>,
format: DecodeSequenceMode = DecodeSequenceMode.AUTO_DETECT
): Sequence<T> {
val buffered = if (source is BufferedSource) source else source.buffer()
return decodeToSequenceByReader(OkioSerialReader(buffered), deserializer, format)
return decodeToSequenceByReader(OkioSerialReader(source), deserializer, format)
}

/**
* Transforms the given [source] into lazily deserialized sequence of elements of type [T] using UTF-8 encoding and deserializer retrieved from the reified type parameter.
* Unlike [decodeFromSource], [source] is allowed to have more than one element, separated as [format] declares.
*
* If [source] is not a [BufferedSource] then called [Source.buffer] for it to create buffered wrapper.
* Unlike [decodeFromBufferedSource], [source] is allowed to have more than one element, separated as [format] declares.
*
* Elements must all be of type [T].
* Elements are parsed lazily when resulting [Sequence] is evaluated.
Expand All @@ -133,7 +122,7 @@ public fun <T> Json.decodeSourceToSequence(
* @throws [okio.IOException] If an I/O error occurs and source can't be read from.
*/
@ExperimentalSerializationApi
public inline fun <reified T> Json.decodeSourceToSequence(
source: Source,
public inline fun <reified T> Json.decodeBufferedSourceToSequence(
source: BufferedSource,
format: DecodeSequenceMode = DecodeSequenceMode.AUTO_DETECT
): Sequence<T> = decodeSourceToSequence(source, serializersModule.serializer(), format)
): Sequence<T> = decodeBufferedSourceToSequence(source, serializersModule.serializer(), format)
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal class JsonToOkioStreamWriter(private val target: BufferedSink) : JsonWr
}

override fun release() {
target.flush()
target.emitCompleteSegments()
shanshin marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package kotlinx.serialization.json

import kotlinx.serialization.*
import kotlinx.serialization.json.internal.*
import kotlinx.serialization.json.okio.decodeFromSource
import kotlinx.serialization.json.okio.encodeToSink
import kotlinx.serialization.json.okio.decodeFromBufferedSource
import kotlinx.serialization.json.okio.encodeToBufferedSink
import kotlinx.serialization.modules.EmptySerializersModule
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.test.*
Expand Down Expand Up @@ -53,7 +53,7 @@ abstract class JsonTestBase {
}
JsonTestingMode.OKIO_STREAMS -> {
val buffer = Buffer()
encodeToSink(serializer, value, buffer)
encodeToBufferedSink(serializer, value, buffer)
buffer.readUtf8()
}
}
Expand Down Expand Up @@ -82,7 +82,7 @@ abstract class JsonTestBase {
JsonTestingMode.OKIO_STREAMS -> {
val buffer = Buffer()
buffer.writeUtf8(source)
decodeFromSource(deserializer, buffer)
decodeFromBufferedSource(deserializer, buffer)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public interface SerialReader {
}

/** @suppress */
@InternalSerializationApi
public fun <T> Json.encodeByWriter(writer: JsonWriter, serializer: SerializationStrategy<T>, value: T) {
@PublishedApi
internal fun <T> Json.encodeByWriter(writer: JsonWriter, serializer: SerializationStrategy<T>, value: T) {
shanshin marked this conversation as resolved.
Show resolved Hide resolved
val encoder = StreamingJsonEncoder(
writer, this,
WriteMode.OBJ,
Expand All @@ -32,8 +32,8 @@ public fun <T> Json.encodeByWriter(writer: JsonWriter, serializer: Serialization
}

/** @suppress */
@InternalSerializationApi
public fun <T> Json.decodeByReader(
@PublishedApi
internal fun <T> Json.decodeByReader(
deserializer: DeserializationStrategy<T>,
reader: SerialReader
): T {
Expand All @@ -45,9 +45,9 @@ public fun <T> Json.decodeByReader(
}

/** @suppress */
@InternalSerializationApi
@PublishedApi
@ExperimentalSerializationApi
public fun <T> Json.decodeToSequenceByReader(
internal fun <T> Json.decodeToSequenceByReader(
reader: SerialReader,
deserializer: DeserializationStrategy<T>,
format: DecodeSequenceMode = DecodeSequenceMode.AUTO_DETECT
Expand All @@ -58,9 +58,9 @@ public fun <T> Json.decodeToSequenceByReader(
}

/** @suppress */
shanshin marked this conversation as resolved.
Show resolved Hide resolved
@InternalSerializationApi
@PublishedApi
@ExperimentalSerializationApi
public inline fun <reified T> Json.decodeToSequenceByReader(
internal inline fun <reified T> Json.decodeToSequenceByReader(
reader: SerialReader,
format: DecodeSequenceMode = DecodeSequenceMode.AUTO_DETECT
): Sequence<T> = decodeToSequenceByReader(reader, serializersModule.serializer(), format)