Skip to content

Commit

Permalink
Removed charset from public api
Browse files Browse the repository at this point in the history
  • Loading branch information
sandwwraith committed Aug 23, 2021
1 parent 084dc0b commit cea326e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 22 deletions.
6 changes: 2 additions & 4 deletions formats/json/api/kotlinx-serialization-json.api
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,7 @@ public abstract class kotlinx/serialization/json/JsonTransformingSerializer : ko
}

public final class kotlinx/serialization/json/JvmStreamsKt {
public static final fun decodeFromStream (Lkotlinx/serialization/json/Json;Lkotlinx/serialization/DeserializationStrategy;Ljava/io/InputStream;Ljava/nio/charset/Charset;)Ljava/lang/Object;
public static synthetic fun decodeFromStream$default (Lkotlinx/serialization/json/Json;Lkotlinx/serialization/DeserializationStrategy;Ljava/io/InputStream;Ljava/nio/charset/Charset;ILjava/lang/Object;)Ljava/lang/Object;
public static final fun encodeToStream (Lkotlinx/serialization/json/Json;Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;Ljava/io/OutputStream;Ljava/nio/charset/Charset;)V
public static synthetic fun encodeToStream$default (Lkotlinx/serialization/json/Json;Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;Ljava/io/OutputStream;Ljava/nio/charset/Charset;ILjava/lang/Object;)V
public static final fun decodeFromStream (Lkotlinx/serialization/json/Json;Lkotlinx/serialization/DeserializationStrategy;Ljava/io/InputStream;)Ljava/lang/Object;
public static final fun encodeToStream (Lkotlinx/serialization/json/Json;Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;Ljava/io/OutputStream;)V
}

28 changes: 12 additions & 16 deletions formats/json/jvmMain/src/kotlinx/serialization/json/JvmStreams.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package kotlinx.serialization.json
import kotlinx.serialization.*
import kotlinx.serialization.json.internal.*
import java.io.*
import java.nio.charset.Charset

/**
* Serializes the [value] with [serializer] into a [stream] using JSON format and given [charset].
* Serializes the [value] with [serializer] into a [stream] using JSON format and UTF-8 encoding..
*
* @throws [SerializationException] if the given value cannot be serialized to JSON.
* @throws [IOException] If an I/O error occurs and stream can't be written to.
Expand All @@ -15,10 +14,9 @@ import java.nio.charset.Charset
public fun <T> Json.encodeToStream(
serializer: SerializationStrategy<T>,
value: T,
stream: OutputStream,
charset: Charset = Charsets.UTF_8
stream: OutputStream
) {
val result = JsonToWriterStringBuilder(stream, charset)
val result = JsonToWriterStringBuilder(stream)
try {
val encoder = StreamingJsonEncoder(
result, this,
Expand All @@ -32,43 +30,41 @@ public fun <T> Json.encodeToStream(
}

/**
* Serializes given [value] to [stream] using [charset] and serializer retrieved from the reified type parameter.
* Serializes given [value] to [stream] using UTF-8 encoding and serializer retrieved from the reified type parameter.
*
* @throws [SerializationException] if the given value cannot be serialized to JSON.
* @throws [IOException] If an I/O error occurs and stream can't be written to.
*/
@ExperimentalSerializationApi
public inline fun <reified T> Json.encodeToStream(
value: T,
stream: OutputStream,
charset: Charset = Charsets.UTF_8
stream: OutputStream
): Unit =
encodeToStream(serializersModule.serializer(), value, stream, charset)
encodeToStream(serializersModule.serializer(), value, stream)

/**
* Deserializes JSON from [stream] using [charset] to a value of type [T] using [deserializer].
* Deserializes JSON from [stream] using UTF-8 encoding to a value of type [T] using [deserializer].
*
* @throws [SerializationException] if the given JSON input cannot be deserialized to the value of type [T].
* @throws [IOException] If an I/O error occurs and stream can't be read from.
*/
@ExperimentalSerializationApi
public fun <T> Json.decodeFromStream(
deserializer: DeserializationStrategy<T>,
stream: InputStream,
charset: Charset = Charsets.UTF_8
stream: InputStream
): T {
val lexer = ReaderJsonLexer(stream, charset)
val lexer = ReaderJsonLexer(stream)
val input = StreamingJsonDecoder(this, WriteMode.OBJ, lexer, deserializer.descriptor)
return input.decodeSerializableValue(deserializer)
}

/**
* Deserializes the contents of given [stream] to to the value of type [T] using [charset] and
* Deserializes the contents of given [stream] to to the value of type [T] using UTF-8 encoding and
* deserializer retrieved from the reified type parameter.
*
* @throws [SerializationException] if the given JSON input cannot be deserialized to the value of type [T].
* @throws [IOException] If an I/O error occurs and stream can't be read from.
*/
@ExperimentalSerializationApi
public inline fun <reified T> Json.decodeFromStream(stream: InputStream, charset: Charset = Charsets.UTF_8): T =
decodeFromStream(serializersModule.serializer(), stream, charset)
public inline fun <reified T> Json.decodeFromStream(stream: InputStream): T =
decodeFromStream(serializersModule.serializer(), stream)
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal class ReaderJsonLexer(
) : AbstractJsonLexer() {
private var threshold: Int = DEFAULT_THRESHOLD // chars

constructor(i: InputStream, charset: Charset) : this(i.reader(charset).buffered(READER_BUF_SIZE))
constructor(i: InputStream, charset: Charset = Charsets.UTF_8) : this(i.reader(charset).buffered(READER_BUF_SIZE))

override var source: CharSequence = ArrayAsSequence(_source)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal class JsonToWriterStringBuilder(private val writer: Writer) : JsonStrin
// maybe this can also be taken from the pool, but currently initial char array size there is 128, which is too low.
CharArray(BATCH_SIZE)
) {
constructor(os: OutputStream, charset: Charset): this(os.writer(charset).buffered(READER_BUF_SIZE))
constructor(os: OutputStream, charset: Charset = Charsets.UTF_8): this(os.writer(charset).buffered(READER_BUF_SIZE))

override fun ensureTotalCapacity(oldSize: Int, additional: Int): Int {
val requiredSize = oldSize + additional
Expand Down

0 comments on commit cea326e

Please sign in to comment.