@@ -18,8 +18,8 @@ import com.adamratzman.spotify.utils.UserURI
1818import com.adamratzman.spotify.utils.encode
1919import com.adamratzman.spotify.utils.toObject
2020import com.adamratzman.spotify.utils.toPagingObject
21- import org.json.JSONArray
22- import org.json.JSONObject
21+ import com.beust.klaxon.JsonArray
22+ import com.beust.klaxon.JsonObject
2323import java.awt.image.BufferedImage
2424import java.io.ByteArrayOutputStream
2525import java.io.File
@@ -57,14 +57,14 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistsAPI(api) {
5757 ): SpotifyRestAction <Playlist > {
5858 if (name.isEmpty()) throw BadRequestException (ErrorObject (400 , " Name cannot be empty" ))
5959 return toAction(Supplier {
60- val json = JSONObject ()
61- json.put( " name" , name)
62- if (description != null ) json.put( " description" , description)
63- if (public != null ) json.put( " public" , public)
64- if (collaborative != null ) json.put( " collaborative" , collaborative)
60+ val json = JsonObject ()
61+ json[ " name" ] = name
62+ if (description != null ) json[ " description" ] = description
63+ if (public != null ) json[ " public" ] = public
64+ if (collaborative != null ) json[ " collaborative" ] = collaborative
6565 post(
6666 EndpointBuilder (" /users/${UserURI (user).id.encode()} /playlists" ).toString(),
67- json.toString ()
67+ json.toJsonString ()
6868 ).toObject<Playlist >(api)
6969 })
7070 }
@@ -81,10 +81,13 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistsAPI(api) {
8181 * @throws BadRequestException if any invalid track ids is provided or the playlist is not found
8282 */
8383 fun addTracksToPlaylist (playlist : String , vararg tracks : String , position : Int? = null): SpotifyRestAction <Unit > {
84- val json = JSONObject ().put( " uris" , tracks.map { TrackURI (TrackURI (it).id.encode()).uri })
85- if (position != null ) json.put( " position" , position)
84+ val json = JsonObject ().apply { this [ " uris" ] = tracks.map { TrackURI (TrackURI (it).id.encode()).uri } }
85+ if (position != null ) json[ " position" ] = position
8686 return toAction(Supplier {
87- post(EndpointBuilder (" /playlists/${PlaylistURI (playlist).id.encode()} /tracks" ).toString(), json.toString())
87+ post(
88+ EndpointBuilder (" /playlists/${PlaylistURI (playlist).id.encode()} /tracks" ).toString(),
89+ json.toJsonString()
90+ )
8891 Unit
8992 })
9093 }
@@ -107,14 +110,14 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistsAPI(api) {
107110 collaborative : Boolean? = null,
108111 description : String? = null
109112 ): SpotifyRestAction <Unit > {
110- val json = JSONObject ()
111- if (name != null ) json.put( " name" , name)
112- if (public != null ) json.put( " public" , public)
113- if (collaborative != null ) json.put( " collaborative" , collaborative)
114- if (description != null ) json.put( " description" , description)
115- if (json.length() == 0 ) throw IllegalArgumentException (" At least one option must not be null" )
113+ val json = JsonObject ()
114+ if (name != null ) json[ " name" ] = name
115+ if (public != null ) json[ " public" ] = public
116+ if (collaborative != null ) json[ " collaborative" ] = collaborative
117+ if (description != null ) json[ " description" ] = description
118+ if (json.isEmpty() ) throw IllegalArgumentException (" At least one option must not be null" )
116119 return toAction(Supplier {
117- put(EndpointBuilder (" /playlists/${PlaylistURI (playlist).id.encode()} " ).toString(), json.toString ())
120+ put(EndpointBuilder (" /playlists/${PlaylistURI (playlist).id.encode()} " ).toString(), json.toJsonString ())
118121 Unit
119122 })
120123 }
@@ -130,7 +133,7 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistsAPI(api) {
130133 fun getClientPlaylists (
131134 limit : Int? = null,
132135 offset : Int? = null
133- ): SpotifyRestActionPaging <SimplePlaylist ,PagingObject <SimplePlaylist >> {
136+ ): SpotifyRestActionPaging <SimplePlaylist , PagingObject <SimplePlaylist >> {
134137 if (limit != null && limit !in 1 .. 50 ) throw IllegalArgumentException (" Limit must be between 1 and 50. Provided $limit " )
135138 if (offset != null && offset !in 0 .. 100000 ) throw IllegalArgumentException (" Offset must be between 0 and 100,000. Provided $limit " )
136139 return toActionPaging(Supplier {
@@ -190,13 +193,15 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistsAPI(api) {
190193 snapshotId : String? = null
191194 ): SpotifyRestAction <Snapshot > {
192195 return toAction(Supplier {
193- val json = JSONObject ()
194- json.put(" range_start" , reorderRangeStart)
195- json.put(" insert_before" , insertionPoint)
196- if (reorderRangeLength != null ) json.put(" range_length" , reorderRangeLength)
197- if (snapshotId != null ) json.put(" snapshot_id" , snapshotId)
198- put(EndpointBuilder (" /playlists/${PlaylistURI (playlist).id.encode()} /tracks" ).toString(), json.toString())
199- .toObject<Snapshot >(api)
196+ val json = JsonObject ()
197+ json[" range_start" ] = reorderRangeStart
198+ json[" insert_before" ] = insertionPoint
199+ if (reorderRangeLength != null ) json[" range_length" ] = reorderRangeLength
200+ if (snapshotId != null ) json[" snapshot_id" ] = snapshotId
201+ put(
202+ EndpointBuilder (" /playlists/${PlaylistURI (playlist).id.encode()} /tracks" ).toString(),
203+ json.toJsonString()
204+ ).toObject<Snapshot >(api)
200205 })
201206 }
202207
@@ -211,9 +216,12 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistsAPI(api) {
211216 */
212217 fun setPlaylistTracks (playlist : String , vararg tracks : String ): SpotifyRestAction <Unit > {
213218 return toAction(Supplier {
214- val json = JSONObject ()
215- json.put(" uris" , tracks.map { TrackURI (TrackURI (it).id.encode()).uri })
216- put(EndpointBuilder (" /playlists/${PlaylistURI (playlist).id.encode()} /tracks" ).toString(), json.toString())
219+ val json = JsonObject ()
220+ json[" uris" ] = tracks.map { TrackURI (TrackURI (it).id.encode()).uri }
221+ put(
222+ EndpointBuilder (" /playlists/${PlaylistURI (playlist).id.encode()} /tracks" ).toString(),
223+ json.toJsonString()
224+ )
217225 Unit
218226 })
219227 }
@@ -298,14 +306,16 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistsAPI(api) {
298306 return toAction(Supplier {
299307 if (tracks.isEmpty()) throw IllegalArgumentException (" You need to provide at least one track to remove" )
300308
301- val json = JSONObject ().also { if (snapshotId != null ) it.put( " snapshot_id" , snapshotId) }
309+ val json = JsonObject ().apply { if (snapshotId != null ) this [ " snapshot_id" ] = snapshotId }
302310
303311 tracks.map { (track, positions) ->
304- JSONObject ().put(" uri" , TrackURI (track).uri)
305- .also { if (positions?.positions?.isNotEmpty() == true ) it.put(" positions" , positions.positions) }
306- }.let { json.put(" tracks" , JSONArray (it)) }
312+ JsonObject ().apply {
313+ this [" uri" ] = TrackURI (track).uri
314+ if (positions?.positions?.isNotEmpty() == true ) this .put(" positions" , positions.positions)
315+ }.also { if (positions?.positions?.isNotEmpty() == true ) it[" positions" ] = positions.positions }
316+ }.let { json.put(" tracks" , JsonArray (it)) }
307317 delete(
308- EndpointBuilder (" /playlists/${PlaylistURI (playlist).id} /tracks" ).toString(), body = json.toString ()
318+ EndpointBuilder (" /playlists/${PlaylistURI (playlist).id} /tracks" ).toString(), body = json.toJsonString ()
309319 )
310320 })
311321 }
0 commit comments