@@ -10,19 +10,64 @@ import kotlinx.serialization.encoding.*
10
10
import kotlinx.serialization.internal.*
11
11
import kotlinx.serialization.modules.*
12
12
13
+ /* *
14
+ * Transforms a [Serializable] class' properties into a single flat [String] representing the class data
15
+ * in the properties format.
16
+ *
17
+ * If the given class has non-primitive property `d` of arbitrary type `D`, `D` values are inserted
18
+ * into the same map; keys for such values are prefixed with string `d.`:
19
+ *
20
+ * ```
21
+ * @Serializable
22
+ * class Data(val property1: String)
23
+ *
24
+ * @Serializable
25
+ * class DataHolder(val data: Data, val property2: String)
26
+ *
27
+ * val string = StringProperties.encodeToString(properties)
28
+ * // string contents will be the following:
29
+ * """
30
+ * property2 = value2
31
+ * data.property1 = value1
32
+ * """
33
+ * ```
34
+ *
35
+ * If the given class has a [List] property `l`, each value from the list
36
+ * would be prefixed with `l.N.`, where N is an index for a particular value.
37
+ * [Map] is treated as a `[key,value,...]` list.
38
+
39
+ * Conversely, this class can convert a properties string into a [Serializable] class instance.
40
+ * ```
41
+ * @Serializable
42
+ * class Data(val property1: String)
43
+ *
44
+ * @Serializable
45
+ * class DataHolder(val data: Data, val property2: String)
46
+ *
47
+ * val string = """
48
+ * property2 = value2
49
+ * data.property1 = value1
50
+ * """
51
+ * val data = StringProperties.decodeToString(string, DataHolder.serializer())
52
+ * // data contents will be the following:
53
+ * // DataHolder(data = Data(property1 = "value1"), property2 = "value2")
54
+ * ```
55
+ *
56
+ * @param conf A [PropertiesConf] which contain configuration for customising the output string.
57
+ */
13
58
@ExperimentalSerializationApi
14
59
@Suppress(" UNUSED_PARAMETER" )
15
60
public sealed class StringProperties (
16
61
private val conf : PropertiesConf ,
17
62
private val properties : Properties = Properties (conf.serializersModule),
18
- ) : SerialFormat by properties {
63
+ ) : SerialFormat by properties, StringFormat {
19
64
20
65
/* *
21
66
* Encodes properties from the given [value] to a properties String using the given [serializer].
22
67
* `null` values are omitted from the output.
23
68
*/
24
69
@ExperimentalSerializationApi
25
- public fun <T > encodeToString (serializer : SerializationStrategy <T >, value : T ): String {
70
+ public override fun <T > encodeToString (serializer : SerializationStrategy <T >, value : T ): String {
26
71
val map = properties.encodeToMap(serializer, value)
27
72
val builder = StringBuilder ()
28
73
for ((k, v) in map) {
@@ -45,12 +90,10 @@ public sealed class StringProperties(
45
90
* [String] values are converted to respective primitive types using default conversion methods.
46
91
* [T] may contain properties of nullable types; they will be filled by non-null values from the [map], if present.
47
92
*/
48
- public fun <T > decodeFromString (deserializer : DeserializationStrategy <T >, string : String ): T {
93
+ public override fun <T > decodeFromString (deserializer : DeserializationStrategy <T >, string : String ): T {
49
94
val result = mutableMapOf<String , String >()
50
95
for (line in string.logicalLines()) {
51
- println (" line=`$line `" )
52
96
val parsedLine = line.unescaped()
53
- println (" parsedLine=`$parsedLine `" )
54
97
var keyEnd = parsedLine.length
55
98
for (i in parsedLine.indices) {
56
99
if (parsedLine[i] in separators) {
@@ -77,8 +120,6 @@ public sealed class StringProperties(
77
120
78
121
result[parsedLine.substring(0 , keyEnd)] = parsedLine.substring(valueBegin)
79
122
}
80
- println (result)
81
- println (result.keys)
82
123
return properties.decodeFromStringMap(deserializer, result)
83
124
}
84
125
@@ -97,8 +138,8 @@ private class StringPropertiesImpl(conf: PropertiesConf) : StringProperties(conf
97
138
* TODO: doc
98
139
*/
99
140
@ExperimentalSerializationApi
100
- public fun StringProperties (builderAction : PropertiesBuilder .() -> Unit = {}): StringProperties {
101
- val builder = PropertiesBuilder (PropertiesConf ())
141
+ public fun StringProperties (builderAction : StringPropertiesBuilder .() -> Unit = {}): StringProperties {
142
+ val builder = StringPropertiesBuilder (PropertiesConf ())
102
143
builder.builderAction()
103
144
return StringPropertiesImpl (builder.build())
104
145
}
@@ -121,13 +162,40 @@ public inline fun <reified T> StringProperties.encodeToString(value: T): String
121
162
public inline fun <reified T > StringProperties.decodeFromString (propertiesString : String ): T =
122
163
decodeFromString(serializersModule.serializer(), propertiesString)
123
164
165
+ /* *
166
+ * Builder of the [StringProperties] instance provided by `StringProperties { ... }` factory function.
167
+ */
124
168
@ExperimentalSerializationApi
125
- public class PropertiesBuilder internal constructor(from : PropertiesConf ) {
169
+ public class StringPropertiesBuilder internal constructor(from : PropertiesConf ) {
126
170
171
+ /* *
172
+ * A [LineSeparator] to be used for separating lines when encoding to a string.
173
+ * Default value is [LineSeparator.LF].
174
+ */
127
175
public var lineSeparator: LineSeparator = from.lineSeparator
176
+
177
+ /* *
178
+ * A [KeyValueSeparator] to be used for separating keys and values when encoding to a string.
179
+ * Default value is [KeyValueSeparator.EQUALS].
180
+ */
128
181
public var keyValueSeparator: KeyValueSeparator = from.keyValueSeparator
182
+
183
+ /* *
184
+ * A number of spaces to be inserted before the [keyValueSeparator] when encoding to a string.
185
+ * Default value is `0`.
186
+ */
129
187
public var spacesBeforeSeparator: Int = from.spacesBeforeSeparator
188
+
189
+ /* *
190
+ * A number of spaces to be inserted after the [keyValueSeparator] when encoding to a string.
191
+ * Default value is `0`.
192
+ */
130
193
public var spacesAfterSeparator: Int = from.spacesAfterSeparator
194
+
195
+ /* *
196
+ * A [SerializersModule] to be used for encoding and decoding.
197
+ * Default value is [EmptySerializersModule].
198
+ */
131
199
public var module: SerializersModule = from.serializersModule
132
200
133
201
internal fun build (): PropertiesConf {
0 commit comments