AbstractModel.java
Go to the documentation of this file.
00001 /*
00002  *      Copyright (C) 2005-2015 Team XBMC
00003  *      http://xbmc.org
00004  *
00005  *  This Program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2, or (at your option)
00008  *  any later version.
00009  *
00010  *  This Program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with XBMC Remote; see the file license.  If not, write to
00017  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
00018  *  http://www.gnu.org/copyleft/gpl.html
00019  *
00020  */
00021 
00022 package org.xbmc.android.jsonrpc.api;
00023 
00024 import java.util.ArrayList;
00025 import java.util.HashMap;
00026 import java.util.Iterator;
00027 
00028 import org.codehaus.jackson.JsonNode;
00029 import org.codehaus.jackson.map.ObjectMapper;
00030 import org.codehaus.jackson.node.ArrayNode;
00031 import org.codehaus.jackson.node.ObjectNode;
00032 
00033 import android.os.Parcelable;
00034 
00035 public abstract class AbstractModel implements JsonSerializable, Parcelable {
00036 
00040         protected final static ObjectMapper OM = new ObjectMapper();
00041 
00042         protected String mType;
00043 
00051         public static int parseInt(JsonNode node, String key) {
00052                 return node.has(key) ? node.get(key).getIntValue() : -1;
00053         }
00054 
00062         public static String parseString(JsonNode node, String key) {
00063                 return node.has(key) ? node.get(key).getTextValue() : null;
00064         }
00065 
00073         public static Boolean parseBoolean(JsonNode node, String key) {
00074                 final boolean hasKey = node.has(key);
00075                 if (hasKey) {
00076                         return node.get(key).getBooleanValue();
00077                 } else {
00078                         return null;
00079                 }
00080         }
00081 
00082         public static Double parseDouble(JsonNode node, String key) {
00083                 return node.has(key) ? node.get(key).getDoubleValue() : null;
00084         }
00085 
00086         public static ArrayList<String> getStringArray(JsonNode node, String key) {
00087                 if (node.has(key)) {
00088                         final ArrayNode a = (ArrayNode)node.get(key);
00089                         final ArrayList<String> l = new ArrayList<String>(a.size());
00090                         for (int i = 0; i < a.size(); i++) {
00091                                 l.add(a.get(i).getTextValue());
00092                         }
00093                         return l;
00094                 }
00095                 return new ArrayList<String>(0);
00096         }
00097 
00098         public static ArrayList<Integer> getIntegerArray(JsonNode node, String key) {
00099                 if (node.has(key)) {
00100                         final ArrayNode a = (ArrayNode)node.get(key);
00101                         final ArrayList<Integer> l = new ArrayList<Integer>(a.size());
00102                         for (int i = 0; i < a.size(); i++) {
00103                                 l.add(a.get(i).getIntValue());
00104                         }
00105                         return l;
00106                 }
00107                 return new ArrayList<Integer>(0);
00108         }
00109 
00110         public static HashMap<String, String> getStringMap(JsonNode node, String key) {
00111                 if (node.has(key)) {
00112                         final ObjectNode n = (ObjectNode)node.get(key);
00113                         final HashMap<String, String> m = new HashMap<String, String>();
00114                         final Iterator<String> it = n.getFieldNames();
00115                         while (it.hasNext()) {
00116                                 final String fieldName = it.next();
00117                                 m.put(fieldName, n.get(fieldName).getValueAsText());
00118                         }
00119                         return m;
00120                 }
00121                 return new HashMap<String, String>();
00122         }
00123 
00124 }


smarthome_media_kodi_driver
Author(s): Mickael Gaillard , Erwan Le Huitouze
autogenerated on Thu Jun 6 2019 21:03:49