|
| 1 | +/* |
| 2 | + * Copyright 2017-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
| 3 | + */ |
| 4 | + |
| 5 | +package kotlinx.serialization.json.io |
| 6 | + |
| 7 | +import kotlinx.serialization.* |
| 8 | +import kotlinx.serialization.json.DecodeSequenceMode |
| 9 | +import kotlinx.serialization.json.Json |
| 10 | +import kotlinx.serialization.json.internal.* |
| 11 | +import kotlinx.serialization.json.io.internal.JsonToIoStreamWriter |
| 12 | +import kotlinx.serialization.json.internal.decodeToSequenceByReader |
| 13 | +import kotlinx.serialization.json.io.internal.IoSerialReader |
| 14 | +import kotlinx.io.* |
| 15 | + |
| 16 | +/** |
| 17 | + * Serializes the [value] with [serializer] into a [sink] using JSON format and UTF-8 encoding. |
| 18 | + * |
| 19 | + * @throws [SerializationException] if the given value cannot be serialized to JSON. |
| 20 | + * @throws [kotlinx.io.IOException] If an I/O error occurs and sink can't be written to. |
| 21 | + */ |
| 22 | +@ExperimentalSerializationApi |
| 23 | +public fun <T> Json.encodeToSink( |
| 24 | + serializer: SerializationStrategy<T>, |
| 25 | + value: T, |
| 26 | + sink: Sink |
| 27 | +) { |
| 28 | + val writer = JsonToIoStreamWriter(sink) |
| 29 | + try { |
| 30 | + encodeByWriter(this, writer, serializer, value) |
| 31 | + } finally { |
| 32 | + writer.release() |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Serializes given [value] to a [sink] using UTF-8 encoding and serializer retrieved from the reified type parameter. |
| 38 | + * |
| 39 | + * @throws [SerializationException] if the given value cannot be serialized to JSON. |
| 40 | + * @throws [kotlinx.io.IOException] If an I/O error occurs and sink can't be written to. |
| 41 | + */ |
| 42 | +@ExperimentalSerializationApi |
| 43 | +public inline fun <reified T> Json.encodeToSink( |
| 44 | + value: T, |
| 45 | + sink: Sink |
| 46 | +): Unit = encodeToSink(serializersModule.serializer(), value, sink) |
| 47 | + |
| 48 | + |
| 49 | +/** |
| 50 | + * Deserializes JSON from [source] using UTF-8 encoding to a value of type [T] using [deserializer]. |
| 51 | + * |
| 52 | + * Note that this functions expects that exactly one object would be present in the source |
| 53 | + * and throws an exception if there are any dangling bytes after an object. |
| 54 | + * |
| 55 | + * @throws [SerializationException] if the given JSON input cannot be deserialized to the value of type [T]. |
| 56 | + * @throws [kotlinx.io.IOException] If an I/O error occurs and source can't be read from. |
| 57 | + */ |
| 58 | +@ExperimentalSerializationApi |
| 59 | +public fun <T> Json.decodeFromSource( |
| 60 | + deserializer: DeserializationStrategy<T>, |
| 61 | + source: Source |
| 62 | +): T { |
| 63 | + return decodeByReader(this, deserializer, IoSerialReader(source)) |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Deserializes the contents of given [source] to the value of type [T] using UTF-8 encoding and |
| 68 | + * deserializer retrieved from the reified type parameter. |
| 69 | + * |
| 70 | + * Note that this functions expects that exactly one object would be present in the stream |
| 71 | + * and throws an exception if there are any dangling bytes after an object. |
| 72 | + * |
| 73 | + * @throws [SerializationException] if the given JSON input cannot be deserialized to the value of type [T]. |
| 74 | + * @throws [kotlinx.io.IOException] If an I/O error occurs and source can't be read from. |
| 75 | + */ |
| 76 | +@ExperimentalSerializationApi |
| 77 | +public inline fun <reified T> Json.decodeFromSource(source: Source): T = |
| 78 | + decodeFromSource(serializersModule.serializer(), source) |
| 79 | + |
| 80 | + |
| 81 | +/** |
| 82 | + * Transforms the given [source] into lazily deserialized sequence of elements of type [T] using UTF-8 encoding and [deserializer]. |
| 83 | + * Unlike [decodeFromSource], [source] is allowed to have more than one element, separated as [format] declares. |
| 84 | + * |
| 85 | + * Elements must all be of type [T]. |
| 86 | + * Elements are parsed lazily when resulting [Sequence] is evaluated. |
| 87 | + * Resulting sequence is tied to the stream and can be evaluated only once. |
| 88 | + * |
| 89 | + * **Resource caution:** this method neither closes the [source] when the parsing is finished nor provides a method to close it manually. |
| 90 | + * It is a caller responsibility to hold a reference to a source and close it. Moreover, because source is parsed lazily, |
| 91 | + * closing it before returned sequence is evaluated completely will result in [Exception] from decoder. |
| 92 | + * |
| 93 | + * @throws [SerializationException] if the given JSON input cannot be deserialized to the value of type [T]. |
| 94 | + * @throws [kotlinx.io.IOException] If an I/O error occurs and source can't be read from. |
| 95 | + */ |
| 96 | +@ExperimentalSerializationApi |
| 97 | +public fun <T> Json.decodeSourceToSequence( |
| 98 | + source: Source, |
| 99 | + deserializer: DeserializationStrategy<T>, |
| 100 | + format: DecodeSequenceMode = DecodeSequenceMode.AUTO_DETECT |
| 101 | +): Sequence<T> { |
| 102 | + return decodeToSequenceByReader(this, IoSerialReader(source), deserializer, format) |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * 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. |
| 107 | + * Unlike [decodeSourceToSequence], [source] is allowed to have more than one element, separated as [format] declares. |
| 108 | + * |
| 109 | + * Elements must all be of type [T]. |
| 110 | + * Elements are parsed lazily when resulting [Sequence] is evaluated. |
| 111 | + * Resulting sequence is tied to the stream and constrained to be evaluated only once. |
| 112 | + * |
| 113 | + * **Resource caution:** this method does not close [source] when the parsing is finished neither provides method to close it manually. |
| 114 | + * It is a caller responsibility to hold a reference to a source and close it. Moreover, because source is parsed lazily, |
| 115 | + * closing it before returned sequence is evaluated fully would result in [Exception] from decoder. |
| 116 | + * |
| 117 | + * @throws [SerializationException] if the given JSON input cannot be deserialized to the value of type [T]. |
| 118 | + * @throws [kotlinx.io.IOException] If an I/O error occurs and source can't be read from. |
| 119 | + */ |
| 120 | +@ExperimentalSerializationApi |
| 121 | +public inline fun <reified T> Json.decodeSourceToSequence( |
| 122 | + source: Source, |
| 123 | + format: DecodeSequenceMode = DecodeSequenceMode.AUTO_DETECT |
| 124 | +): Sequence<T> = decodeSourceToSequence(source, serializersModule.serializer(), format) |
0 commit comments