Java tutorial
/** * Copyright 2013 Jeremy Handcock * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.aperte.lp.movies; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import org.aperte.lp.ParseException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Collections; import java.util.List; /** * Parser for JSON responses from the Rotten Tomatoes opening movies API. * See http://developer.rottentomatoes.com/docs/read/json/v10/Opening_Movies */ public class OpeningParser { /** * Returns a list of opening movies given a JSON input stream conforming * to the Rotten Tomatoes opening movie model. * * @param in the input stream * @return the opening movies * @throws ParseException if there's an error parsing the input stream * @throws IOException if there's an error reading the input stream */ List<OpeningMovie> parseMovies(InputStream in) throws ParseException, IOException { ObjectMapper mapper = new ObjectMapper(); JsonNode root = null; try { root = mapper.readTree(in); } catch (JsonProcessingException e) { throw new ParseException("Unable to parse JSON", e); } if (root != null) { JsonNode movies = root.get("movies"); if (movies == null) { return Collections.<OpeningMovie>emptyList(); } List<OpeningMovie> result = new ArrayList<OpeningMovie>(movies.size()); for (JsonNode movie : movies) { OpeningMovie o = new OpeningMovie(); JsonNode title = movie.get("title"); if (title != null) { o.setTitle(title.textValue()); } JsonNode ratings = movie.get("ratings"); if (ratings != null) { JsonNode criticsRating = ratings.get("critics_rating"); if (criticsRating != null) { o.setCriticsRating(criticsRating.textValue()); } JsonNode criticsScore = ratings.get("critics_score"); if (criticsScore != null) { o.setCriticsScore(criticsScore.intValue()); } JsonNode audienceRating = ratings.get("audience_rating"); if (audienceRating != null) { o.setAudienceRating(audienceRating.textValue()); } JsonNode audienceScore = ratings.get("audience_score"); if (audienceScore != null) { o.setAudienceScore(audienceScore.intValue()); } } JsonNode posters = movie.get("posters"); if (posters != null) { JsonNode thumbnail = posters.get("thumbnail"); if (thumbnail != null) { o.setPosterThumbnailUrl(thumbnail.textValue()); } JsonNode profile = posters.get("profile"); if (profile != null) { o.setPosterProfileUrl(profile.textValue()); } } JsonNode cast = movie.get("abridged_cast"); if (cast != null) { List<String> actorNames = new ArrayList<String>(cast.size()); for (JsonNode actor : cast) { JsonNode name = actor.get("name"); if (name != null) { actorNames.add(name.textValue()); } } o.setCast(actorNames); } result.add(o); } return result; } return Collections.<OpeningMovie>emptyList(); } }