Skip to content

Commit 4ccbbd9

Browse files
committed
replace kotlinx.serialization with klaxon
Signed-off-by: Adam Ratzman <adam@adamratzman.com>
1 parent a3ed372 commit 4ccbbd9

27 files changed

+554
-462
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
.idea/**/gradle.xml
3030
.idea/**/libraries
3131

32-
# Gradle and Maven with auto-import
33-
# When using Gradle or Maven with auto-import, you should exclude module files,
32+
# Gradle and Maven with auto-# When using Gradle or Maven with auto-import, you should exclude module files,
3433
# since they will be recreated, and may cause churn. Uncomment if using
3534
# auto-import.
3635
.idea/modules.xml

README.md

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,7 @@ dependencies {
3838
```
3939

4040
#### Android
41-
This library should work out of the box on Android too.
42-
43-
If you're using Proguard, please add this to `proguard-rules.pro`:
44-
```
45-
-keepattributes *Annotation*, InnerClasses
46-
-dontnote kotlinx.serialization.SerializationKt
47-
-keep,includedescriptorclasses class com.yourcompany.yourpackage.**$$serializer { *; } # <-- change package name to your app's
48-
-keepclassmembers class com.yourcompany.yourpackage.** { # <-- change package name to your app's
49-
*** Companion;
50-
}
51-
-keepclasseswithmembers class com.yourcompany.yourpackage.** { # <-- change package name to your app's
52-
kotlinx.serialization.KSerializer serializer(...);
53-
}
54-
55-
41+
This library should work out of the box on Android.
5642
```
5743
5844
## Creating a SpotifyAPI or SpotifyClientAPI object

build.gradle

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ buildscript {
44

55
dependencies {
66
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
7-
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
87
}
98
}
109

@@ -17,7 +16,6 @@ plugins {
1716
}
1817

1918
apply plugin: 'kotlin'
20-
apply plugin: 'kotlinx-serialization'
2119

2220
group 'com.adamratzman'
2321
version '2.0.0'
@@ -28,20 +26,19 @@ sourceCompatibility = 1.8
2826

2927
repositories {
3028
mavenCentral()
29+
jcenter()
3130
maven { url "https://dl.bintray.com/spekframework/spek" }
32-
maven { url "https://kotlin.bintray.com/kotlinx" }
3331
}
3432

3533
dependencies {
3634
// Actual library dependencies
3735
implementation(group: 'org.json', name: 'json', version: '20180130')
3836

37+
implementation 'com.beust:klaxon:5.0.1'
38+
3939
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
4040
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
4141

42-
43-
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.9.1"
44-
4542
// Spek testing requirements
4643
testImplementation('org.spekframework.spek2:spek-dsl-jvm:2.0.0-rc.1') {
4744
exclude group: 'org.jetbrains.kotlin'

src/main/kotlin/com/adamratzman/spotify/endpoints/client/ClientFollowingAPI.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import com.adamratzman.spotify.endpoints.public.FollowingAPI
55
import com.adamratzman.spotify.main.SpotifyAPI
66
import com.adamratzman.spotify.main.SpotifyClientAPI
77
import com.adamratzman.spotify.main.SpotifyRestAction
8-
import com.adamratzman.spotify.main.SpotifyRestPagingAction
98
import com.adamratzman.spotify.utils.Artist
109
import com.adamratzman.spotify.utils.ArtistURI
1110
import com.adamratzman.spotify.utils.CursorBasedPagingObject
@@ -14,10 +13,8 @@ import com.adamratzman.spotify.utils.PlaylistURI
1413
import com.adamratzman.spotify.utils.SpotifyPublicUser
1514
import com.adamratzman.spotify.utils.UserURI
1615
import com.adamratzman.spotify.utils.encode
16+
import com.adamratzman.spotify.utils.toArray
1717
import com.adamratzman.spotify.utils.toCursorBasedPagingObject
18-
import com.adamratzman.spotify.utils.toObject
19-
import kotlinx.serialization.internal.ArrayListSerializer
20-
import kotlinx.serialization.internal.BooleanSerializer
2118
import java.util.function.Supplier
2219

2320
/**
@@ -69,7 +66,7 @@ class ClientFollowingAPI(api: SpotifyAPI) : FollowingAPI(api) {
6966
get(
7067
EndpointBuilder("/me/following/contains").with("type", "user")
7168
.with("ids", users.joinToString(",") { UserURI(it).id.encode() }).toString()
72-
).toObject(api, ArrayListSerializer(BooleanSerializer)).toList()
69+
).toArray<Boolean>(api)
7370
})
7471
}
7572

@@ -98,7 +95,7 @@ class ClientFollowingAPI(api: SpotifyAPI) : FollowingAPI(api) {
9895
get(
9996
EndpointBuilder("/me/following/contains").with("type", "artist")
10097
.with("ids", artists.joinToString(",") { ArtistURI(it).id.encode() }).toString()
101-
).toObject(api, ArrayListSerializer(BooleanSerializer)).toList()
98+
).toArray<Boolean>(api)
10299
})
103100
}
104101

@@ -111,14 +108,14 @@ class ClientFollowingAPI(api: SpotifyAPI) : FollowingAPI(api) {
111108
fun getFollowedArtists(
112109
limit: Int? = null,
113110
after: String? = null
114-
): SpotifyRestPagingAction<Artist, CursorBasedPagingObject<Artist>> {
115-
return toPagingObjectAction(Supplier {
111+
): SpotifyRestAction<CursorBasedPagingObject<Artist>> {
112+
return toAction(Supplier {
116113
get(
117114
EndpointBuilder("/me/following").with("type", "artist").with("limit", limit).with(
118115
"after",
119116
after
120117
).toString()
121-
).toCursorBasedPagingObject("artists", this, CursorBasedPagingObject.serializer(Artist.serializer()))
118+
).toCursorBasedPagingObject<Artist>("artists", this)
122119
})
123120
}
124121

src/main/kotlin/com/adamratzman/spotify/endpoints/client/ClientLibraryAPI.kt

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.adamratzman.spotify.endpoints.client
33

44
import com.adamratzman.spotify.main.SpotifyAPI
55
import com.adamratzman.spotify.main.SpotifyRestAction
6-
import com.adamratzman.spotify.main.SpotifyRestPagingAction
76
import com.adamratzman.spotify.utils.AlbumURI
87
import com.adamratzman.spotify.utils.EndpointBuilder
98
import com.adamratzman.spotify.utils.Market
@@ -13,10 +12,8 @@ import com.adamratzman.spotify.utils.SavedTrack
1312
import com.adamratzman.spotify.utils.SpotifyEndpoint
1413
import com.adamratzman.spotify.utils.TrackURI
1514
import com.adamratzman.spotify.utils.encode
16-
import com.adamratzman.spotify.utils.toObject
15+
import com.adamratzman.spotify.utils.toArray
1716
import com.adamratzman.spotify.utils.toPagingObject
18-
import kotlinx.serialization.internal.ArrayListSerializer
19-
import kotlinx.serialization.internal.BooleanSerializer
2017
import java.util.function.Supplier
2118

2219
/**
@@ -32,12 +29,12 @@ class ClientLibraryAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
3229
limit: Int? = null,
3330
offset: Int? = null,
3431
market: Market? = null
35-
): SpotifyRestPagingAction<SavedTrack, PagingObject<SavedTrack>> {
36-
return toPagingObjectAction(Supplier {
32+
): SpotifyRestAction<PagingObject<SavedTrack>> {
33+
return toAction(Supplier {
3734
get(
3835
EndpointBuilder("/me/tracks").with("limit", limit).with("offset", offset).with("market", market?.code)
3936
.toString()
40-
).toPagingObject(endpoint = this, serializer = PagingObject.serializer(SavedTrack.serializer()))
37+
).toPagingObject<SavedTrack>(endpoint = this)
4138
})
4239
}
4340

@@ -50,12 +47,12 @@ class ClientLibraryAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
5047
limit: Int? = null,
5148
offset: Int? = null,
5249
market: Market? = null
53-
): SpotifyRestPagingAction<SavedAlbum, PagingObject<SavedAlbum>> {
54-
return toPagingObjectAction(Supplier {
50+
): SpotifyRestAction<PagingObject<SavedAlbum>> {
51+
return toAction(Supplier {
5552
get(
5653
EndpointBuilder("/me/albums").with("limit", limit).with("offset", offset).with("market", market?.code)
5754
.toString()
58-
).toPagingObject(endpoint = this, serializer = PagingObject.serializer(SavedAlbum.serializer()))
55+
).toPagingObject<SavedAlbum>(endpoint = this)
5956
})
6057
}
6158

@@ -80,7 +77,7 @@ class ClientLibraryAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
8077
get(
8178
EndpointBuilder("/me/$type/contains").with("ids", ids.joinToString(",") { type.id(it).encode() })
8279
.toString()
83-
).toObject(api, ArrayListSerializer(BooleanSerializer)).toList()
80+
).toArray<Boolean>(api)
8481
})
8582
}
8683

src/main/kotlin/com/adamratzman/spotify/endpoints/client/ClientPersonalizationAPI.kt

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
package com.adamratzman.spotify.endpoints.client
33

44
import com.adamratzman.spotify.main.SpotifyAPI
5-
import com.adamratzman.spotify.main.SpotifyRestPagingAction
5+
import com.adamratzman.spotify.main.SpotifyRestAction
66
import com.adamratzman.spotify.utils.Artist
77
import com.adamratzman.spotify.utils.EndpointBuilder
88
import com.adamratzman.spotify.utils.PagingObject
@@ -33,11 +33,17 @@ class ClientPersonalizationAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
3333
*
3434
* @return [PagingObject] of full [Artist] objects sorted by affinity
3535
*/
36-
fun getTopArtists(limit: Int? = null, offset: Int? = null, timeRange: TimeRange? = null): SpotifyRestPagingAction<Artist, PagingObject<Artist>> {
37-
return toPagingObjectAction(Supplier {
38-
get(EndpointBuilder("/me/top/artists").with("limit", limit).with("offset", offset)
39-
.with("time_range", timeRange).toString())
40-
.toPagingObject(endpoint = this, serializer = PagingObject.serializer(Artist.serializer()))
36+
fun getTopArtists(
37+
limit: Int? = null,
38+
offset: Int? = null,
39+
timeRange: TimeRange? = null
40+
): SpotifyRestAction<PagingObject<Artist>> {
41+
return toAction(Supplier {
42+
get(
43+
EndpointBuilder("/me/top/artists").with("limit", limit).with("offset", offset)
44+
.with("time_range", timeRange).toString()
45+
)
46+
.toPagingObject<Artist>(endpoint = this)
4147
})
4248
}
4349

@@ -53,11 +59,17 @@ class ClientPersonalizationAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
5359
*
5460
* @return [PagingObject] of full [Track] objects sorted by affinity
5561
*/
56-
fun getTopTracks(limit: Int? = null, offset: Int? = null, timeRange: TimeRange? = null): SpotifyRestPagingAction<Track, PagingObject<Track>> {
57-
return toPagingObjectAction(Supplier {
58-
get(EndpointBuilder("/me/top/tracks").with("limit", limit).with("offset", offset)
59-
.with("time_range", timeRange).toString())
60-
.toPagingObject(endpoint = this, serializer = PagingObject.serializer(Track.serializer()))
62+
fun getTopTracks(
63+
limit: Int? = null,
64+
offset: Int? = null,
65+
timeRange: TimeRange? = null
66+
): SpotifyRestAction<PagingObject<Track>> {
67+
return toAction(Supplier {
68+
get(
69+
EndpointBuilder("/me/top/tracks").with("limit", limit).with("offset", offset)
70+
.with("time_range", timeRange).toString()
71+
)
72+
.toPagingObject<Track>(endpoint = this)
6173
})
6274
}
6375
}

src/main/kotlin/com/adamratzman/spotify/endpoints/client/ClientPlayerAPI.kt

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.adamratzman.spotify.endpoints.client
33

44
import com.adamratzman.spotify.main.SpotifyAPI
55
import com.adamratzman.spotify.main.SpotifyRestAction
6-
import com.adamratzman.spotify.main.SpotifyRestPagingAction
76
import com.adamratzman.spotify.utils.AlbumURI
87
import com.adamratzman.spotify.utils.ArtistURI
98
import com.adamratzman.spotify.utils.BadRequestException
@@ -16,11 +15,11 @@ import com.adamratzman.spotify.utils.PlayHistory
1615
import com.adamratzman.spotify.utils.PlaylistURI
1716
import com.adamratzman.spotify.utils.SpotifyEndpoint
1817
import com.adamratzman.spotify.utils.TrackURI
18+
import com.adamratzman.spotify.utils.catch
1919
import com.adamratzman.spotify.utils.encode
2020
import com.adamratzman.spotify.utils.toCursorBasedPagingObject
2121
import com.adamratzman.spotify.utils.toInnerObject
2222
import com.adamratzman.spotify.utils.toObject
23-
import kotlinx.serialization.list
2423
import org.json.JSONObject
2524
import java.util.function.Supplier
2625

@@ -31,38 +30,37 @@ import java.util.function.Supplier
3130
class ClientPlayerAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
3231
fun getDevices(): SpotifyRestAction<List<Device>> {
3332
return toAction(Supplier {
34-
get(EndpointBuilder("/me/player/devices").toString()).toInnerObject(
35-
"devices",
36-
api,
37-
Device.serializer().list
38-
).toList()
33+
get(EndpointBuilder("/me/player/devices").toString()).toInnerObject<List<Device>>(
34+
"devices", api
35+
)
3936
})
4037
}
4138

4239
fun getCurrentContext(): SpotifyRestAction<CurrentlyPlayingContext?> {
4340
return toAction(Supplier {
44-
val obj: CurrentlyPlayingContext? =
41+
val obj = catch {
4542
get(EndpointBuilder("/me/player").toString())
46-
.toObject(api, CurrentlyPlayingContext.serializer())
43+
.toObject<CurrentlyPlayingContext>(api)
44+
}
4745
if (obj?.timestamp == null) null else obj
4846
})
4947
}
5048

51-
fun getRecentlyPlayed(): SpotifyRestPagingAction<PlayHistory, CursorBasedPagingObject<PlayHistory>> {
52-
return toPagingObjectAction(Supplier {
53-
get(EndpointBuilder("/me/player/recently-played").toString()).toCursorBasedPagingObject(
54-
endpoint = this,
55-
serializer = CursorBasedPagingObject.serializer(PlayHistory.serializer())
49+
fun getRecentlyPlayed(): SpotifyRestAction<CursorBasedPagingObject<PlayHistory>> {
50+
return toAction(Supplier {
51+
get(EndpointBuilder("/me/player/recently-played").toString()).toCursorBasedPagingObject<PlayHistory>(
52+
endpoint = this
5653
)
5754
})
5855
}
5956

6057
fun getCurrentlyPlaying(): SpotifyRestAction<CurrentlyPlayingObject?> {
6158
return toAction(Supplier {
62-
val obj: CurrentlyPlayingObject? = get(EndpointBuilder("/me/player/currently-playing").toString()).toObject(
63-
api,
64-
CurrentlyPlayingObject.serializer()
65-
)
59+
val obj =
60+
catch {
61+
get(EndpointBuilder("/me/player/currently-playing").toString())
62+
.toObject<CurrentlyPlayingObject>(api)
63+
}
6664
if (obj?.timestamp == null) null else obj
6765
})
6866
}

src/main/kotlin/com/adamratzman/spotify/endpoints/client/ClientPlaylistAPI.kt

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import com.adamratzman.spotify.endpoints.public.PlaylistsAPI
55
import com.adamratzman.spotify.main.SpotifyAPI
66
import com.adamratzman.spotify.main.SpotifyClientAPI
77
import com.adamratzman.spotify.main.SpotifyRestAction
8-
import com.adamratzman.spotify.main.SpotifyRestPagingAction
98
import com.adamratzman.spotify.utils.BadRequestException
109
import com.adamratzman.spotify.utils.EndpointBuilder
1110
import com.adamratzman.spotify.utils.ErrorObject
@@ -18,7 +17,6 @@ import com.adamratzman.spotify.utils.UserURI
1817
import com.adamratzman.spotify.utils.encode
1918
import com.adamratzman.spotify.utils.toObject
2019
import com.adamratzman.spotify.utils.toPagingObject
21-
import kotlinx.serialization.Serializable
2220
import org.json.JSONArray
2321
import org.json.JSONObject
2422
import java.awt.image.BufferedImage
@@ -63,8 +61,10 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistsAPI(api) {
6361
if (description != null) json.put("description", description)
6462
if (public != null) json.put("public", public)
6563
if (collaborative != null) json.put("collaborative", collaborative)
66-
post(EndpointBuilder("/users/${UserURI(user).id.encode()}/playlists").toString(),
67-
json.toString()).toObject(api, Playlist.serializer())
64+
post(
65+
EndpointBuilder("/users/${UserURI(user).id.encode()}/playlists").toString(),
66+
json.toString()
67+
).toObject<Playlist>(api)
6868
})
6969
}
7070

@@ -129,12 +129,12 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistsAPI(api) {
129129
fun getClientPlaylists(
130130
limit: Int? = null,
131131
offset: Int? = null
132-
): SpotifyRestPagingAction<SimplePlaylist, PagingObject<SimplePlaylist>> {
132+
): SpotifyRestAction<PagingObject<SimplePlaylist>> {
133133
if (limit != null && limit !in 1..50) throw IllegalArgumentException("Limit must be between 1 and 50. Provided $limit")
134134
if (offset != null && offset !in 0..100000) throw IllegalArgumentException("Offset must be between 0 and 100,000. Provided $limit")
135-
return toPagingObjectAction(Supplier {
135+
return toAction(Supplier {
136136
get(EndpointBuilder("/me/playlists").with("limit", limit).with("offset", offset).toString())
137-
.toPagingObject(endpoint = this, serializer = PagingObject.serializer(SimplePlaylist.serializer()))
137+
.toPagingObject<SimplePlaylist>(endpoint = this)
138138
})
139139
}
140140

@@ -147,7 +147,7 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistsAPI(api) {
147147
*/
148148
fun getClientPlaylist(id: String): SpotifyRestAction<SimplePlaylist?> {
149149
return toAction(Supplier {
150-
val playlists = getClientPlaylists().completeWithPaging()
150+
val playlists = getClientPlaylists().complete()
151151
playlists.items.find { it.id == id } ?: playlists.getAllItems().complete().find { it.id == id }
152152
})
153153
}
@@ -195,7 +195,7 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistsAPI(api) {
195195
if (reorderRangeLength != null) json.put("range_length", reorderRangeLength)
196196
if (snapshotId != null) json.put("snapshot_id", snapshotId)
197197
put(EndpointBuilder("/playlists/${PlaylistURI(playlist).id.encode()}/tracks").toString(), json.toString())
198-
.toObject(api, Snapshot.serializer())
198+
.toObject<Snapshot>(api)
199199
})
200200
}
201201

@@ -330,7 +330,6 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistsAPI(api) {
330330
return DatatypeConverter.printBase64Binary(bos.toByteArray())
331331
}
332332

333-
@Serializable
334333
data class Snapshot(val snapshot_id: String)
335334
}
336335

0 commit comments

Comments
 (0)