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

json-okio: rename target variables to sink #2226

Merged
merged 1 commit into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -16,7 +16,7 @@ import kotlinx.serialization.json.okio.internal.OkioSerialReader
import okio.*

/**
* Serializes the [value] with [serializer] into a [target] using JSON format and UTF-8 encoding.
* Serializes the [value] with [serializer] into a [sink] using JSON format and UTF-8 encoding.
*
* @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.
Expand All @@ -25,9 +25,9 @@ import okio.*
public fun <T> Json.encodeToBufferedSink(
serializer: SerializationStrategy<T>,
value: T,
target: BufferedSink
sink: BufferedSink
) {
val writer = JsonToOkioStreamWriter(target)
val writer = JsonToOkioStreamWriter(sink)
try {
encodeByWriter(writer, serializer, value)
} finally {
Expand All @@ -36,16 +36,16 @@ public fun <T> Json.encodeToBufferedSink(
}

/**
* Serializes given [value] to a [target] using UTF-8 encoding and serializer retrieved from the reified type parameter.
* Serializes given [value] to a [sink] using UTF-8 encoding and serializer retrieved from the reified type parameter.
*
* @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 inline fun <reified T> Json.encodeToBufferedSink(
value: T,
target: BufferedSink
): Unit = encodeToBufferedSink(serializersModule.serializer(), value, target)
sink: BufferedSink
): Unit = encodeToBufferedSink(serializersModule.serializer(), value, sink)


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,34 @@ import kotlinx.serialization.json.internal.JsonWriter
import kotlinx.serialization.json.internal.SerialReader
import okio.*

internal class JsonToOkioStreamWriter(private val target: BufferedSink) : JsonWriter {
internal class JsonToOkioStreamWriter(private val sink: BufferedSink) : JsonWriter {
override fun writeLong(value: Long) {
write(value.toString())
}

override fun writeChar(char: Char) {
target.writeUtf8CodePoint(char.code)
sink.writeUtf8CodePoint(char.code)
}

override fun write(text: String) {
target.writeUtf8(text)
sink.writeUtf8(text)
}

override fun writeQuoted(text: String) {
target.writeUtf8CodePoint('"'.code)
sink.writeUtf8CodePoint('"'.code)
var lastPos = 0
for (i in text.indices) {
val c = text[i].code
if (c < ESCAPE_STRINGS.size && ESCAPE_STRINGS[c] != null) {
target.writeUtf8(text, lastPos, i) // flush prev
target.writeUtf8(ESCAPE_STRINGS[c]!!)
sink.writeUtf8(text, lastPos, i) // flush prev
sink.writeUtf8(ESCAPE_STRINGS[c]!!)
lastPos = i + 1
}
}

if (lastPos != 0) target.writeUtf8(text, lastPos, text.length)
else target.writeUtf8(text)
target.writeUtf8CodePoint('"'.code)
if (lastPos != 0) sink.writeUtf8(text, lastPos, text.length)
else sink.writeUtf8(text)
sink.writeUtf8CodePoint('"'.code)
}

override fun release() {
Expand Down