Back to project page game_guess_lib.
The source code is released under:
MIT License
If you think the Android project game_guess_lib listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.wkmf.guess.lib.common.api; /* ww w . j av a 2 s . c o m*/ import com.wkmf.guess.lib.structure.GuessConfig; import com.wkmf.guess.lib.structure.GuessGame; import com.wkmf.guess.lib.structure.GuessLevel; import com.wkmf.guess.lib.structure.GuessQuestion; import org.codehaus.jackson.map.DeserializationConfig; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.ObjectWriter; import org.codehaus.jackson.type.TypeReference; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * Created by ernestofndz on 12/02/14. */ public class GuessApi { // JSON -> Obj public static GuessGame fromJsonGuessGame(String json) { GuessGame guessGame = null; try { ObjectMapper mapper = new ObjectMapper().configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); guessGame = mapper.readValue(json, new TypeReference<GuessGame>() { }); } catch (IOException ioe) { ioe.printStackTrace(); } return guessGame; } // Obj -> JSON public static String toJsonGuessGame(GuessGame guessGame) { String json = null; try { ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); json = ow.writeValueAsString(guessGame); } catch (IOException ioe) { ioe.printStackTrace(); } return json; } // JSON -> Config public static GuessConfig fromJsonGuessConfig(String json) { GuessConfig guessConfig = null; try { ObjectMapper mapper = new ObjectMapper().configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); guessConfig = mapper.readValue(json, new TypeReference<GuessConfig>() { }); } catch (IOException ioe) { ioe.printStackTrace(); } return guessConfig; } // Config -> JSON public static String toJsonGuessConfig(GuessConfig guessConfig) { String json = null; try { ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); json = ow.writeValueAsString(guessConfig); } catch (IOException ioe) { ioe.printStackTrace(); } return json; } // JSON -> Levels public static List<GuessLevel> toLevels(String json) { List<GuessLevel> levels = null; try { ObjectMapper mapper = new ObjectMapper().configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); levels = mapper.readValue(json, new TypeReference<ArrayList<GuessLevel>>() { }); } catch (IOException ioe) { ioe.printStackTrace(); } return levels; } // JSON -> Questions public static List<GuessQuestion> toQuestions(String json) { List<GuessQuestion> questions = null; try { ObjectMapper mapper = new ObjectMapper().configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); questions = mapper.readValue(json, new TypeReference<ArrayList<GuessQuestion>>() { }); } catch (IOException ioe) { ioe.printStackTrace(); } return questions; } }