@@ -18,21 +18,28 @@ import kotlinx.serialization.json.Json
18
18
import kotlinx.serialization.json.JsonElement
19
19
import kotlinx.serialization.json.JsonObject
20
20
21
- internal val nonstrictJson =
22
- Json {
23
- isLenient = true
24
- ignoreUnknownKeys = true
25
- allowSpecialFloatingPointValues = true
26
- useArrayPolymorphism = true
27
- }
21
+ internal val nonstrictJson = Json {
22
+ isLenient = true
23
+ ignoreUnknownKeys = true
24
+ allowSpecialFloatingPointValues = true
25
+ useArrayPolymorphism = true
26
+ }
28
27
29
- internal inline fun <reified T : Any > String.toObjectNullable (serializer : KSerializer <T >, api : GenericSpotifyApi ? , json : Json ): T ? = try {
28
+ internal inline fun <reified T : Any > String.toObjectNullable (
29
+ serializer : KSerializer <T >,
30
+ api : GenericSpotifyApi ? ,
31
+ json : Json
32
+ ): T ? = try {
30
33
toObject(serializer, api, json)
31
34
} catch (e: Exception ) {
32
35
null
33
36
}
34
37
35
- internal inline fun <reified T : Any > String.toObject (serializer : KSerializer <T >, api : GenericSpotifyApi ? , json : Json ): T {
38
+ internal inline fun <reified T : Any > String.toObject (
39
+ serializer : KSerializer <T >,
40
+ api : GenericSpotifyApi ? ,
41
+ json : Json
42
+ ): T {
36
43
return this .parseJson {
37
44
val obj = json.decodeFromString(serializer, this )
38
45
api?.let {
@@ -44,7 +51,11 @@ internal inline fun <reified T : Any> String.toObject(serializer: KSerializer<T>
44
51
}
45
52
}
46
53
47
- internal inline fun <reified T > String.toList (serializer : KSerializer <List <T >>, api : GenericSpotifyApi ? , json : Json ): List <T > {
54
+ internal inline fun <reified T > String.toList (
55
+ serializer : KSerializer <List <T >>,
56
+ api : GenericSpotifyApi ? ,
57
+ json : Json
58
+ ): List <T > {
48
59
return this .parseJson {
49
60
json.decodeFromString(serializer, this ).apply {
50
61
if (api != null ) {
@@ -72,15 +83,15 @@ internal fun <T : Any> String.toPagingObject(
72
83
json.decodeFromString(MapSerializer (t.first, t.second), this )
73
84
}
74
85
return (map[innerObjectName] ? : if (arbitraryInnerNameAllowed) map.keys.firstOrNull()?.let { map[it] }
75
- ? : error(" " ) else error(" " ))
76
- .apply {
77
- this .endpoint = endpoint
78
- this .itemClazz = tClazz
79
- this .items.map { obj ->
80
- if (obj is NeedsApi ) obj.api = endpoint.api
81
- if (obj is PagingObjectBase <* , * >) obj.endpoint = endpoint
82
- }
86
+ ? : error(" " ) else error(" " ))
87
+ .apply {
88
+ this .endpoint = endpoint
89
+ this .itemClazz = tClazz
90
+ this .items.map { obj ->
91
+ if (obj is NeedsApi ) obj.api = endpoint.api
92
+ if (obj is PagingObjectBase <* , * >) obj.endpoint = endpoint
83
93
}
94
+ }
84
95
}
85
96
86
97
return try {
@@ -97,19 +108,23 @@ internal fun <T : Any> String.toPagingObject(
97
108
} catch (jde: SpotifyException .ParseException ) {
98
109
if (arbitraryInnerNameAllowed && jde.message?.contains(" unable to parse" , true ) == true ) {
99
110
toPagingObject(
100
- tClazz,
101
- tSerializer,
102
- innerObjectName,
103
- endpoint,
104
- json,
105
- arbitraryInnerNameAllowed = true ,
106
- skipInnerNameFirstIfPossible = false
111
+ tClazz,
112
+ tSerializer,
113
+ innerObjectName,
114
+ endpoint,
115
+ json,
116
+ arbitraryInnerNameAllowed = true ,
117
+ skipInnerNameFirstIfPossible = false
107
118
)
108
119
} else throw jde
109
120
}
110
121
}
111
122
112
- internal fun <T : Any > initPagingObject (tClazz : KClass <T >, pagingObject : PagingObjectBase <T , * >, endpoint : SpotifyEndpoint ) {
123
+ internal fun <T : Any > initPagingObject (
124
+ tClazz : KClass <T >,
125
+ pagingObject : PagingObjectBase <T , * >,
126
+ endpoint : SpotifyEndpoint
127
+ ) {
113
128
pagingObject.apply {
114
129
this .endpoint = endpoint
115
130
this .itemClazz = tClazz
@@ -127,7 +142,14 @@ internal inline fun <reified T : Any> String.toPagingObject(
127
142
json : Json ,
128
143
arbitraryInnerNameAllowed : Boolean = false,
129
144
skipInnerNameFirstIfPossible : Boolean = true
130
- ): PagingObject <T > = toNullablePagingObject(tSerializer, innerObjectName, endpoint, json, arbitraryInnerNameAllowed, skipInnerNameFirstIfPossible).toPagingObject()
145
+ ): PagingObject <T > = toNullablePagingObject(
146
+ tSerializer,
147
+ innerObjectName,
148
+ endpoint,
149
+ json,
150
+ arbitraryInnerNameAllowed,
151
+ skipInnerNameFirstIfPossible
152
+ ).toPagingObject()
131
153
132
154
internal inline fun <reified T : Any > String.toNullablePagingObject (
133
155
tSerializer : KSerializer <T >,
@@ -136,7 +158,15 @@ internal inline fun <reified T : Any> String.toNullablePagingObject(
136
158
json : Json ,
137
159
arbitraryInnerNameAllowed : Boolean = false,
138
160
skipInnerNameFirstIfPossible : Boolean = true
139
- ): NullablePagingObject <T > = toPagingObject(T ::class , tSerializer, innerObjectName, endpoint, json, arbitraryInnerNameAllowed, skipInnerNameFirstIfPossible)
161
+ ): NullablePagingObject <T > = toPagingObject(
162
+ T ::class ,
163
+ tSerializer,
164
+ innerObjectName,
165
+ endpoint,
166
+ json,
167
+ arbitraryInnerNameAllowed,
168
+ skipInnerNameFirstIfPossible
169
+ )
140
170
141
171
internal fun <T : Any > String.toCursorBasedPagingObject (
142
172
tClazz : KClass <T >,
@@ -153,25 +183,26 @@ internal fun <T : Any> String.toCursorBasedPagingObject(
153
183
json.decodeFromString(MapSerializer (t.first, t.second), this )
154
184
}
155
185
return (map[innerObjectName] ? : if (arbitraryInnerNameAllowed) map.keys.firstOrNull()?.let { map[it] }
156
- ? : error(" " ) else error(" " ))
157
- .apply { initPagingObject(tClazz, this , endpoint) }
186
+ ? : error(" " ) else error(" " ))
187
+ .apply { initPagingObject(tClazz, this , endpoint) }
158
188
}
159
189
return try {
160
- val pagingObject = this .parseJson { json.decodeFromString(CursorBasedPagingObject .serializer(tSerializer), this ) }
190
+ val pagingObject =
191
+ this .parseJson { json.decodeFromString(CursorBasedPagingObject .serializer(tSerializer), this ) }
161
192
162
193
initPagingObject(tClazz, pagingObject, endpoint)
163
194
164
195
pagingObject
165
196
} catch (jde: SpotifyException .ParseException ) {
166
197
if (! arbitraryInnerNameAllowed && jde.message?.contains(" unable to parse" , true ) == true ) {
167
198
toCursorBasedPagingObject(
168
- tClazz,
169
- tSerializer,
170
- innerObjectName,
171
- endpoint,
172
- json,
173
- arbitraryInnerNameAllowed = true ,
174
- skipInnerNameFirstIfPossible = false
199
+ tClazz,
200
+ tSerializer,
201
+ innerObjectName,
202
+ endpoint,
203
+ json,
204
+ arbitraryInnerNameAllowed = true ,
205
+ skipInnerNameFirstIfPossible = false
175
206
)
176
207
} else throw jde
177
208
}
@@ -185,7 +216,15 @@ internal inline fun <reified T : Any> String.toCursorBasedPagingObject(
185
216
arbitraryInnerNameAllowed : Boolean = false,
186
217
skipInnerNameFirstIfPossible : Boolean = true
187
218
): CursorBasedPagingObject <T > =
188
- toCursorBasedPagingObject(T ::class , tSerializer, innerObjectName, endpoint, json, arbitraryInnerNameAllowed, skipInnerNameFirstIfPossible)
219
+ toCursorBasedPagingObject(
220
+ T ::class ,
221
+ tSerializer,
222
+ innerObjectName,
223
+ endpoint,
224
+ json,
225
+ arbitraryInnerNameAllowed,
226
+ skipInnerNameFirstIfPossible
227
+ )
189
228
190
229
internal inline fun <reified T > String.toInnerObject (serializer : KSerializer <T >, innerName : String , json : Json ): T {
191
230
val map = this .parseJson {
@@ -195,7 +234,11 @@ internal inline fun <reified T> String.toInnerObject(serializer: KSerializer<T>,
195
234
return (map[innerName] ? : error(" Inner object with name $innerName doesn't exist in $map " ))
196
235
}
197
236
198
- internal inline fun <reified T > String.toInnerArray (serializer : KSerializer <List <T >>, innerName : String , json : Json ): List <T > {
237
+ internal inline fun <reified T > String.toInnerArray (
238
+ serializer : KSerializer <List <T >>,
239
+ innerName : String ,
240
+ json : Json
241
+ ): List <T > {
199
242
val map = this .parseJson {
200
243
val t = (String .serializer() to serializer)
201
244
json.decodeFromString(MapSerializer (t.first, t.second), this )
@@ -205,17 +248,20 @@ internal inline fun <reified T> String.toInnerArray(serializer: KSerializer<List
205
248
206
249
internal fun Map <String , JsonElement >.toJson () = JsonObject (this ).toString()
207
250
208
- internal fun <A , B > createMapSerializer (aSerializer : KSerializer <A >, bSerializer : KSerializer <B >): KSerializer <Map <A , B >> {
251
+ internal fun <A , B > createMapSerializer (
252
+ aSerializer : KSerializer <A >,
253
+ bSerializer : KSerializer <B >
254
+ ): KSerializer <Map <A , B >> {
209
255
val t = (aSerializer to bSerializer)
210
256
return MapSerializer (t.first, t.second)
211
257
}
212
258
213
259
internal fun <T > String.parseJson (producer : String .() -> T ): T =
214
- try {
215
- producer(this )
216
- } catch (e: Exception ) {
217
- throw SpotifyException .ParseException (
218
- " Unable to parse $this " ,
219
- e
220
- )
221
- }
260
+ try {
261
+ producer(this )
262
+ } catch (e: Exception ) {
263
+ throw SpotifyException .ParseException (
264
+ " Unable to parse $this " ,
265
+ e
266
+ )
267
+ }
0 commit comments