Skip to content

Commit

Permalink
Rename json-okio target variables to sink (Kotlin#2226)
Browse files Browse the repository at this point in the history
The Okio codebase names Sink/BufferedSink variables "sink", so this aligns the naming convention
  • Loading branch information
BenWoodworth authored and xBaank committed Apr 20, 2023
1 parent da91066 commit a978cf0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
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

0 comments on commit a978cf0

Please sign in to comment.