Package org.codehaus.jettison.json

Examples of org.codehaus.jettison.json.JSONArray


     */
    private JSONObject createRequest(long offset, long chunkSize) throws JSONException {
        JSONObject request = new JSONObject();
        request.put("id", 1L);
        request.put("method", "slim.request");
        JSONArray paramsArray = new JSONArray();
        paramsArray.put("-");
        JSONArray paramsArray2 = new JSONArray();
        paramsArray2.put("socialmusicdiscovery");
        paramsArray2.put("tracks");
        paramsArray2.put("offset:" + offset);
        paramsArray2.put("size:" + chunkSize);
        paramsArray.put(paramsArray2);
        request.put("params", paramsArray);
        return request;
    }
View Full Code Here


        if (entity != null) {
            try {
                JSONObject object = Client.create().resource("http://ws.spotify.com/search/1/artist.json?q=artist:" + URLEncoder.encode(entity.getName(), "utf8")).accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
                result.setCount(object.getJSONObject("info").getInt("num_results"));
                List<ResultItem<SpotifyArtist>> artists = new ArrayList<ResultItem<SpotifyArtist>>();
                JSONArray array = object.getJSONArray("artists");
                result.setCount(array.length());
                for (int i = 0; i < array.length(); i++) {
                    if((firstItem==null || i>=firstItem) && (maxItems==null || maxItems>artists.size())) {
                        artists.add(createFromJSON(array.getJSONObject(i)));
                    }
                }
                result.setItems(artists);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
View Full Code Here

        try {
            System.out.println("Making call to: " + SERVICE_URL + "/playlists.json");
            JSONObject response = c.resource(SERVICE_URL + "/playlists.json").accept("application/json").get(JSONObject.class);
            try {
                JSONArray playlistArray = response.getJSONArray("playlists");
                for (int i = 0; i < playlistArray.length(); i++) {
                    try {
                        playlistUris.add(playlistArray.getJSONObject(i).getString("uri"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                        //TODO: Error handling ?
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
                //TODO: Error handling ?
            }

            int noOfTracks = 0;
            for (String playlistUri : playlistUris) {
                response = c.resource(SERVICE_URL + "/" + playlistUri + "/playlists.json").accept("application/json").get(JSONObject.class);

                try {
                    SpotifyPlaylistData playlistData = mapper.readValue(response.toString(), SpotifyPlaylistData.class);
                    playlists.add(playlistData);
                    noOfTracks += playlistData.getTracks().size();
                } catch (JsonMappingException e) {
                    e.printStackTrace();
                    //TODO: Error handling ?
                } catch (JsonParseException e) {
                    e.printStackTrace();
                    //TODO: Error handling ?
                } catch (IOException e) {
                    e.printStackTrace();
                    //TODO: Error handling ?
                }
            }


            long i = 0;
            long offset = 0;
            final long CHUNK_SIZE = 10;
            for (SpotifyPlaylistData playlist : playlists) {
                for (SpotifyPlaylistTrackData trackData : playlist.getTracks()) {
                    if (!isAborted()) {
                        if (i == 0) {
                            entityManager.getTransaction().begin();
                        }

                        TrackData track = new TrackData();
                        track.setFile(trackData.getUri());
                        track.setFormat("spotify");
                        track.setSmdID(trackData.getUri());
                        track.setUrl(trackData.getUri());
                        track.setTags(new ArrayList<TagData>());
                        track.getTags().add(new TagData(TagData.ALBUM, trackData.getAlbum()));
                        track.getTags().add(new TagData(TagData.TITLE, trackData.getName()));
                        track.getTags().add(new TagData(TagData.SPOTIFY_TRACK_ID, trackData.getUri()));
                        track.getTags().add(new TagData("SPOTIFYIMAGE", trackData.getCover()));
                        track.getTags().add(new TagData("DURATION", "" + trackData.getDuration()));

                        if (trackData.getIndex() != null) {
                            if (trackData.getIndex() != null && trackData.getIndex() != 0) {
                                track.getTags().add(new TagData(TagData.TRACKNUM, "" + trackData.getIndex()));
                            }
                            if (trackData.getDisc() != null && trackData.getDisc() != 0) {
                                track.getTags().add(new TagData(TagData.DISC, "" + trackData.getDisc()));
                            }
                            if (trackData.getAlbumuri() != null) {
                                track.getTags().add(new TagData(TagData.SPOTIFY_ALBUM_ID, trackData.getAlbumuri()));
                            }
                            if (trackData.getArtists() != null) {
                                for (SpotifyArtistData artistData : trackData.getArtists()) {
                                    track.getTags().add(new TagData(TagData.ARTIST, artistData.getName()));
                                    track.getTags().add(new TagData(TagData.SPOTIFY_ARTIST_ID, artistData.getUri()));
                                }
                            }
                        } else {
                            //TODO: Remove this as soon as 2.2.7 of spotifyd has been officially released
                            response = c.resource(SERVICE_URL + "/" + trackData.getUri() + "/browse.json").accept("application/json").get(JSONObject.class);
                            try {
                                if (response.has("index") && !response.getString("index").equals("0")) {
                                    track.getTags().add(new TagData(TagData.TRACKNUM, response.getString("index")));
                                }
                                if (response.has("disc") && !response.getString("disc").equals("0")) {
                                    track.getTags().add(new TagData(TagData.DISC, response.getString("disc")));
                                }
                                if (response.has("albumuri") && response.getString("albumuri").length() > 0) {
                                    track.getTags().add(new TagData(TagData.SPOTIFY_ALBUM_ID, response.getString("albumuri")));
                                }
                                if (response.has("artists")) {
                                    JSONArray artistsArray = response.getJSONArray("artists");
                                    for (int j = 0; j < artistsArray.length(); j++) {
                                        JSONObject artistObject = artistsArray.getJSONObject(j);
                                        track.getTags().add(new TagData(TagData.ARTIST, artistObject.getString("name")));
                                        track.getTags().add(new TagData(TagData.SPOTIFY_ARTIST_ID, artistObject.getString("uri")));
                                    }
                                }
                            } catch (JSONException e) {
View Full Code Here

        }
    }

    @Test
    public void testBrowseTypes() throws Exception {
        JSONArray resultItems = Client.create().resource(HOSTURL + "/browse").accept(MediaType.APPLICATION_JSON).get(JSONArray.class);
        assert resultItems.length() == 9;

        for (int i = 0; i < resultItems.length(); i++) {
            String id = ((JSONObject) resultItems.get(i)).getString("id");
            assert id!=null;
            Long count = ((JSONObject) resultItems.get(i)).optLong("count");
            assert count==0;
        }
        String release = "Release:bb348240-8d6d-4a22-8b92-ab8446d54235";
        resultItems = Client.create().resource(HOSTURL + "/browse?criteria="+release+"&counters=true").accept(MediaType.APPLICATION_JSON).get(JSONArray.class);
        assert resultItems.length() == 7;

        for (int i = 0; i < resultItems.length(); i++) {
            String id = ((JSONObject) resultItems.get(i)).getString("id");
            assert id!=null;
            Long count = ((JSONObject) resultItems.get(i)).optLong("count");
            assert count!=null;
            assert count>0;

            JSONObject result = Client.create().resource(HOSTURL + "/browse/"+id+"?criteria="+release+"&itemInfo=true&childs=true").accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
            assert result.getLong("size") == count;

            JSONArray childResultItems = result.getJSONArray("items");
            for (int j = 0; j < childResultItems.length(); j++) {
                JSONObject item = ((JSONObject) childResultItems.get(j)).getJSONObject("item");
                String childId = item.getString("id");
                JSONArray childItems = ((JSONObject) childResultItems.get(j)).optJSONArray("childItems");
                assert childItems != null;

                Map<String,Long> childMap = new HashMap<String,Long>();
                for (int k=0;k<childItems.length();k++) {
                    childMap.put(((JSONObject)childItems.get(k)).getString("id"),((JSONObject)childItems.get(k)).getLong("count"));
                }

                JSONArray childCounters = Client.create().resource(HOSTURL + "/browse?criteria="+release+"&criteria="+id+":"+childId+"&counters=true").accept(MediaType.APPLICATION_JSON).get(JSONArray.class);
                for (int v = 0; v < childCounters.length(); v++) {
                    String childCounterId = ((JSONObject) childCounters.get(v)).getString("id");
                    assert childCounterId!=null;
                    Long childCounter = ((JSONObject) childCounters.get(v)).getLong("count");
                    assert childCounter!=null;
                    assert childMap.containsKey(childCounterId);
                    if(!childMap.get(childCounterId).equals(childCounter)) {
                        JSONObject testing = Client.create().resource(HOSTURL + "/browse/"+childCounterId+"?criteria="+release+"&criteria="+id+":"+childId).accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
                        testing.toString();
View Full Code Here

    public void testBrowseArtists() throws Exception {
        JSONObject result = Client.create().resource(HOSTURL + "/browse/Artist?itemInfo=true").accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
        assert result.getLong("size") == 50;
        assert result.getLong("offset") == 0;
        assert result.getLong("totalSize") == 50;
        JSONArray resultItems = result.getJSONArray("items");

        for (int i = 0; i < resultItems.length(); i++) {
            JSONObject item = ((JSONObject) resultItems.get(i)).getJSONObject("item");
            Artist artist = jsonProvider.fromJson(item.toString(), Artist.class);
            assert artist != null;
            assert artist.getName() != null;
            JSONArray childItems = ((JSONObject) resultItems.get(i)).optJSONArray("childItems");
            assert childItems == null;
        }

        result = Client.create().resource(HOSTURL + "/browse/Artist?itemInfo=true&offset=5&size=10&childs=true").accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
        assert result.getLong("size") == 10;
        assert result.getLong("offset") == 5;
        assert result.getLong("totalSize") == 50;
        resultItems = result.getJSONArray("items");

        for (int i = 0; i < resultItems.length(); i++) {
            JSONObject item = ((JSONObject) resultItems.get(i)).getJSONObject("item");
            Artist artist = jsonProvider.fromJson(item.toString(), Artist.class);
            assert artist != null;
            assert artist.getName() != null;
            JSONArray childItems = ((JSONObject) resultItems.get(i)).optJSONArray("childItems");
            assert childItems != null;
        }
    }
View Full Code Here

    public void testBrowseReleases() throws Exception {
        JSONObject result = Client.create().resource(HOSTURL + "/browse/Release?itemInfo=true").accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
        assert result.getLong("size") == 5;
        assert result.getLong("offset") == 0;
        assert result.getLong("totalSize") == 5;
        JSONArray resultItems = result.getJSONArray("items");

        int images = 0;
        for (int i = 0; i < resultItems.length(); i++) {
            JSONObject item = ((JSONObject) resultItems.get(i)).getJSONObject("item");
            Release release = jsonProvider.fromJson(item.toString(), Release.class);
            assert release != null;
            assert release.getName() != null;
            JSONArray childItems = ((JSONObject) resultItems.get(i)).optJSONArray("childItems");
            assert childItems == null;
            if(resultItems.getJSONObject(i).optJSONObject("image")!=null && resultItems.getJSONObject(i).getJSONObject("image").getString("providerId")!=null && resultItems.getJSONObject(i).getJSONObject("image").getString("url")!=null) {
                images++;
            }
        }
        assert images==4;

        result = Client.create().resource(HOSTURL + "/browse/Release?itemInfo=true&offset=2&size=10&childs=true").accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
        assert result.getLong("size") == 3;
        assert result.getLong("offset") == 2;
        assert result.getLong("totalSize") == 5;
        resultItems = result.getJSONArray("items");

        for (int i = 0; i < resultItems.length(); i++) {
            JSONObject item = ((JSONObject) resultItems.get(i)).getJSONObject("item");
            Release release = jsonProvider.fromJson(item.toString(), Release.class);
            assert release != null;
            assert release.getName() != null;
            JSONArray childItems = ((JSONObject) resultItems.get(i)).optJSONArray("childItems");
            assert childItems != null;
        }
    }
View Full Code Here

    public void testBrowseTracks() throws Exception {
        JSONObject result = Client.create().resource(HOSTURL + "/browse/Track?itemInfo=true").accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
        assert result.getLong("size") == 79;
        assert result.getLong("offset") == 0;
        assert result.getLong("totalSize") == 79;
        JSONArray resultItems = result.getJSONArray("items");

        boolean foundWithImage = false;
        boolean foundWithoutImage = false;
        for (int i = 0; i < resultItems.length(); i++) {
            JSONObject item = ((JSONObject) resultItems.get(i)).getJSONObject("item");
            Track track = jsonProvider.fromJson(item.toString(), Track.class);
            assert track != null;
            assert track.getNumber() != null;
            JSONArray childItems = ((JSONObject) resultItems.get(i)).optJSONArray("childItems");
            assert childItems == null;
            if(resultItems.getJSONObject(i).optJSONObject("image")!=null && resultItems.getJSONObject(i).getJSONObject("image").getString("providerId")!=null && resultItems.getJSONObject(i).getJSONObject("image").getString("url")!=null) {
                foundWithImage = true;
            }else if(resultItems.getJSONObject(i).optJSONObject("image")==null) {
                foundWithoutImage = true;
            }

        }
        assert foundWithImage;
        assert foundWithoutImage;

        result = Client.create().resource(HOSTURL + "/browse/Track?itemInfo=true&offset=5&size=10&childs=true").accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
        assert result.getLong("size") == 10;
        assert result.getLong("offset") == 5;
        assert result.getLong("totalSize") == 79;
        resultItems = result.getJSONArray("items");

        for (int i = 0; i < resultItems.length(); i++) {
            JSONObject item = ((JSONObject) resultItems.get(i)).getJSONObject("item");
            Track track = jsonProvider.fromJson(item.toString(), Track.class);
            assert track != null;
            assert track.getNumber() != null;
            JSONArray childItems = ((JSONObject) resultItems.get(i)).optJSONArray("childItems");
            assert childItems != null;
        }
    }
View Full Code Here

    public void testBrowseWorks() throws Exception {
        JSONObject result = Client.create().resource(HOSTURL + "/browse/Work?itemInfo=true").accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
        assert result.getLong("size") == 79;
        assert result.getLong("offset") == 0;
        assert result.getLong("totalSize") == 79;
        JSONArray resultItems = result.getJSONArray("items");

        for (int i = 0; i < resultItems.length(); i++) {
            JSONObject item = ((JSONObject) resultItems.get(i)).getJSONObject("item");
            Work work = jsonProvider.fromJson(item.toString(), Work.class);
            assert work != null;
            assert work.getName() != null;
            JSONArray childItems = ((JSONObject) resultItems.get(i)).optJSONArray("childItems");
            assert childItems == null;
        }

        result = Client.create().resource(HOSTURL + "/browse/Work?itemInfo=true&offset=5&size=10&childs=true").accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
        assert result.getLong("size") == 10;
        assert result.getLong("offset") == 5;
        assert result.getLong("totalSize") == 79;
        resultItems = result.getJSONArray("items");

        for (int i = 0; i < resultItems.length(); i++) {
            JSONObject item = ((JSONObject) resultItems.get(i)).getJSONObject("item");
            Work work = jsonProvider.fromJson(item.toString(), Work.class);
            assert work != null;
            assert work.getName() != null;
            JSONArray childItems = ((JSONObject) resultItems.get(i)).optJSONArray("childItems");
            assert childItems != null;
        }
    }
View Full Code Here

    public void testBrowseClassifications() throws Exception {
        JSONObject result = Client.create().resource(HOSTURL + "/browse/Classification?itemInfo=true").accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
        assert result.getLong("size") == 17;
        assert result.getLong("offset") == 0;
        assert result.getLong("totalSize") == 17;
        JSONArray resultItems = result.getJSONArray("items");

        for (int i = 0; i < resultItems.length(); i++) {
            JSONObject item = ((JSONObject) resultItems.get(i)).getJSONObject("item");
            Classification classification = jsonProvider.fromJson(item.toString(), Classification.class);
            assert classification != null;
            assert classification.getName() != null;
            JSONArray childItems = ((JSONObject) resultItems.get(i)).optJSONArray("childItems");
            assert childItems == null;
        }

        result = Client.create().resource(HOSTURL + "/browse/Classification?itemInfo=true&offset=5&size=10&childs=true").accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
        assert result.getLong("size") == 10;
        assert result.getLong("offset") == 5;
        assert result.getLong("totalSize") == 17;
        resultItems = result.getJSONArray("items");

        for (int i = 0; i < resultItems.length(); i++) {
            JSONObject item = ((JSONObject) resultItems.get(i)).getJSONObject("item");
            Classification classification = jsonProvider.fromJson(item.toString(), Classification.class);
            assert classification != null;
            assert classification.getName() != null;
            JSONArray childItems = ((JSONObject) resultItems.get(i)).optJSONArray("childItems");
            assert childItems != null;
        }
    }
View Full Code Here

        assertThat(properties, is(notNullValue()));
        assertThat(properties.length(), is(2));
        assertThat(properties.getString("jcr:primaryType"), is("dna:root"));
        assertThat(properties.get("jcr:uuid"), is(notNullValue()));

        JSONArray children = body.getJSONArray("children");
        assertThat(children.length(), is(1));
        assertThat(children.getString(0), is("jcr:system"));

        assertThat(connection.getResponseCode(), is(HttpURLConnection.HTTP_OK));
        connection.disconnect();
    }
View Full Code Here

TOP

Related Classes of org.codehaus.jettison.json.JSONArray

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.