@@ -14,27 +14,33 @@ import kotlin.reflect.KClass
1414import kotlinx.serialization.KSerializer
1515import kotlinx.serialization.builtins.MapSerializer
1616import kotlinx.serialization.builtins.serializer
17- import kotlinx.serialization.decodeFromString
18- import kotlinx.serialization.encodeToString
1917import kotlinx.serialization.json.Json
2018import kotlinx.serialization.json.JsonElement
2119import kotlinx.serialization.json.JsonObject
2220
2321internal val nonstrictJson =
24- Json {
25- isLenient = true
26- ignoreUnknownKeys = true
27- allowSpecialFloatingPointValues = true
28- useArrayPolymorphism = true
29- }
22+ Json {
23+ isLenient = true
24+ ignoreUnknownKeys = true
25+ allowSpecialFloatingPointValues = true
26+ useArrayPolymorphism = true
27+ }
3028
31- internal inline fun <reified T : Any > String.toObjectNullable (serializer : KSerializer <T >, api : GenericSpotifyApi ? , json : Json ): T ? = try {
29+ internal inline fun <reified T : Any > String.toObjectNullable (
30+ serializer : KSerializer <T >,
31+ api : GenericSpotifyApi ? ,
32+ json : Json
33+ ): T ? = try {
3234 toObject(serializer, api, json)
3335} catch (e: Exception ) {
3436 null
3537}
3638
37- internal inline fun <reified T : Any > String.toObject (serializer : KSerializer <T >, api : GenericSpotifyApi ? , json : Json ): T {
39+ internal inline fun <reified T : Any > String.toObject (
40+ serializer : KSerializer <T >,
41+ api : GenericSpotifyApi ? ,
42+ json : Json
43+ ): T {
3844 return this .parseJson {
3945 val obj = json.decodeFromString(serializer, this )
4046 api?.let {
@@ -46,7 +52,11 @@ internal inline fun <reified T : Any> String.toObject(serializer: KSerializer<T>
4652 }
4753}
4854
49- internal inline fun <reified T > String.toList (serializer : KSerializer <List <T >>, api : GenericSpotifyApi ? , json : Json ): List <T > {
55+ internal inline fun <reified T > String.toList (
56+ serializer : KSerializer <List <T >>,
57+ api : GenericSpotifyApi ? ,
58+ json : Json
59+ ): List <T > {
5060 return this .parseJson {
5161 json.decodeFromString(serializer, this ).apply {
5262 if (api != null ) {
@@ -70,22 +80,23 @@ internal fun <T : Any> String.toPagingObject(
7080): NullablePagingObject <T > {
7181 if (innerObjectName != null || (arbitraryInnerNameAllowed && ! skipInnerNameFirstIfPossible)) {
7282 val jsonObjectRoot = (json.parseToJsonElement(this ) as JsonObject )
73- val jsonElement = innerObjectName?.let { jsonObjectRoot[it] } ? : jsonObjectRoot.keys.firstOrNull()?.let { jsonObjectRoot[it] }
74- ? : throw SpotifyException .ParseException (" Json element was null for class $tClazz (json $this )" )
83+ val jsonElement =
84+ innerObjectName?.let { jsonObjectRoot[it] } ? : jsonObjectRoot.keys.firstOrNull()?.let { jsonObjectRoot[it] }
85+ ? : throw SpotifyException .ParseException (" Json element was null for class $tClazz (json $this )" )
7586 val objectString = jsonElement.toString()
7687
7788 val map = objectString.parseJson {
78- json.decodeFromString(NullablePagingObject .serializer(tSerializer),this )
89+ json.decodeFromString(NullablePagingObject .serializer(tSerializer), this )
7990 }
8091
8192 return map.apply {
82- this .endpoint = endpoint
83- this .itemClazz = tClazz
84- this .items.map { obj ->
85- if (obj is NeedsApi ) obj.api = endpoint.api
86- if (obj is PagingObjectBase <* , * >) obj.endpoint = endpoint
87- }
88- }
93+ this .endpoint = endpoint
94+ this .itemClazz = tClazz
95+ this .items.map { obj ->
96+ if (obj is NeedsApi ) obj.api = endpoint.api
97+ if (obj is PagingObjectBase <* , * >) obj.endpoint = endpoint
98+ }
99+ }
89100 }
90101
91102 return try {
@@ -102,19 +113,23 @@ internal fun <T : Any> String.toPagingObject(
102113 } catch (jde: SpotifyException .ParseException ) {
103114 if (arbitraryInnerNameAllowed && jde.message?.contains(" unable to parse" , true ) == true ) {
104115 toPagingObject(
105- tClazz,
106- tSerializer,
107- innerObjectName,
108- endpoint,
109- json,
110- arbitraryInnerNameAllowed = true ,
111- skipInnerNameFirstIfPossible = false
116+ tClazz,
117+ tSerializer,
118+ innerObjectName,
119+ endpoint,
120+ json,
121+ arbitraryInnerNameAllowed = true ,
122+ skipInnerNameFirstIfPossible = false
112123 )
113124 } else throw jde
114125 }
115126}
116127
117- internal fun <T : Any > initPagingObject (tClazz : KClass <T >, pagingObject : PagingObjectBase <T , * >, endpoint : SpotifyEndpoint ) {
128+ internal fun <T : Any > initPagingObject (
129+ tClazz : KClass <T >,
130+ pagingObject : PagingObjectBase <T , * >,
131+ endpoint : SpotifyEndpoint
132+ ) {
118133 pagingObject.apply {
119134 this .endpoint = endpoint
120135 this .itemClazz = tClazz
@@ -132,7 +147,14 @@ internal inline fun <reified T : Any> String.toPagingObject(
132147 json : Json ,
133148 arbitraryInnerNameAllowed : Boolean = false,
134149 skipInnerNameFirstIfPossible : Boolean = true
135- ): PagingObject <T > = toNullablePagingObject(tSerializer, innerObjectName, endpoint, json, arbitraryInnerNameAllowed, skipInnerNameFirstIfPossible).toPagingObject()
150+ ): PagingObject <T > = toNullablePagingObject(
151+ tSerializer,
152+ innerObjectName,
153+ endpoint,
154+ json,
155+ arbitraryInnerNameAllowed,
156+ skipInnerNameFirstIfPossible
157+ ).toPagingObject()
136158
137159internal inline fun <reified T : Any > String.toNullablePagingObject (
138160 tSerializer : KSerializer <T >,
@@ -141,7 +163,15 @@ internal inline fun <reified T : Any> String.toNullablePagingObject(
141163 json : Json ,
142164 arbitraryInnerNameAllowed : Boolean = false,
143165 skipInnerNameFirstIfPossible : Boolean = true
144- ): NullablePagingObject <T > = toPagingObject(T ::class , tSerializer, innerObjectName, endpoint, json, arbitraryInnerNameAllowed, skipInnerNameFirstIfPossible)
166+ ): NullablePagingObject <T > = toPagingObject(
167+ T ::class ,
168+ tSerializer,
169+ innerObjectName,
170+ endpoint,
171+ json,
172+ arbitraryInnerNameAllowed,
173+ skipInnerNameFirstIfPossible
174+ )
145175
146176internal fun <T : Any > String.toCursorBasedPagingObject (
147177 tClazz : KClass <T >,
@@ -158,25 +188,26 @@ internal fun <T : Any> String.toCursorBasedPagingObject(
158188 json.decodeFromString(MapSerializer (t.first, t.second), this )
159189 }
160190 return (map[innerObjectName] ? : if (arbitraryInnerNameAllowed) map.keys.firstOrNull()?.let { map[it] }
161- ? : error(" " ) else error(" " ))
162- .apply { initPagingObject(tClazz, this , endpoint) }
191+ ? : error(" " ) else error(" " ))
192+ .apply { initPagingObject(tClazz, this , endpoint) }
163193 }
164194 return try {
165- val pagingObject = this .parseJson { json.decodeFromString(CursorBasedPagingObject .serializer(tSerializer), this ) }
195+ val pagingObject =
196+ this .parseJson { json.decodeFromString(CursorBasedPagingObject .serializer(tSerializer), this ) }
166197
167198 initPagingObject(tClazz, pagingObject, endpoint)
168199
169200 pagingObject
170201 } catch (jde: SpotifyException .ParseException ) {
171202 if (! arbitraryInnerNameAllowed && jde.message?.contains(" unable to parse" , true ) == true ) {
172203 toCursorBasedPagingObject(
173- tClazz,
174- tSerializer,
175- innerObjectName,
176- endpoint,
177- json,
178- arbitraryInnerNameAllowed = true ,
179- skipInnerNameFirstIfPossible = false
204+ tClazz,
205+ tSerializer,
206+ innerObjectName,
207+ endpoint,
208+ json,
209+ arbitraryInnerNameAllowed = true ,
210+ skipInnerNameFirstIfPossible = false
180211 )
181212 } else throw jde
182213 }
@@ -190,7 +221,15 @@ internal inline fun <reified T : Any> String.toCursorBasedPagingObject(
190221 arbitraryInnerNameAllowed : Boolean = false,
191222 skipInnerNameFirstIfPossible : Boolean = true
192223): CursorBasedPagingObject <T > =
193- toCursorBasedPagingObject(T ::class , tSerializer, innerObjectName, endpoint, json, arbitraryInnerNameAllowed, skipInnerNameFirstIfPossible)
224+ toCursorBasedPagingObject(
225+ T ::class ,
226+ tSerializer,
227+ innerObjectName,
228+ endpoint,
229+ json,
230+ arbitraryInnerNameAllowed,
231+ skipInnerNameFirstIfPossible
232+ )
194233
195234internal inline fun <reified T > String.toInnerObject (serializer : KSerializer <T >, innerName : String , json : Json ): T {
196235 val map = this .parseJson {
@@ -200,7 +239,11 @@ internal inline fun <reified T> String.toInnerObject(serializer: KSerializer<T>,
200239 return (map[innerName] ? : error(" Inner object with name $innerName doesn't exist in $map " ))
201240}
202241
203- internal inline fun <reified T > String.toInnerArray (serializer : KSerializer <List <T >>, innerName : String , json : Json ): List <T > {
242+ internal inline fun <reified T > String.toInnerArray (
243+ serializer : KSerializer <List <T >>,
244+ innerName : String ,
245+ json : Json
246+ ): List <T > {
204247 val map = this .parseJson {
205248 val t = (String .serializer() to serializer)
206249 json.decodeFromString(MapSerializer (t.first, t.second), this )
@@ -210,17 +253,20 @@ internal inline fun <reified T> String.toInnerArray(serializer: KSerializer<List
210253
211254internal fun Map <String , JsonElement >.toJson () = JsonObject (this ).toString()
212255
213- internal fun <A , B > createMapSerializer (aSerializer : KSerializer <A >, bSerializer : KSerializer <B >): KSerializer <Map <A , B >> {
256+ internal fun <A , B > createMapSerializer (
257+ aSerializer : KSerializer <A >,
258+ bSerializer : KSerializer <B >
259+ ): KSerializer <Map <A , B >> {
214260 val t = (aSerializer to bSerializer)
215261 return MapSerializer (t.first, t.second)
216262}
217263
218264internal fun <T > String.parseJson (producer : String .() -> T ): T =
219- try {
220- producer(this )
221- } catch (e: Exception ) {
222- throw SpotifyException .ParseException (
223- " Unable to parse $this (${e.message} )" ,
224- e
225- )
226- }
265+ try {
266+ producer(this )
267+ } catch (e: Exception ) {
268+ throw SpotifyException .ParseException (
269+ " Unable to parse $this (${e.message} )" ,
270+ e
271+ )
272+ }
0 commit comments