Advertisement
Guest User

Generic .toJson(Object) method

a guest
Aug 16th, 2011
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1.     public static JSONObject toJson(Object obj, boolean pConvertArraylists){               
  2.         String data     = "";
  3.         Class<?> c      = obj.getClass();              
  4.         Field[] fields  = c.getDeclaredFields();
  5.         data += "\n====== " + c.getName() + " ========= \n";
  6.         JSONObject json = new JSONObject();
  7.        
  8.        
  9.         for(Field f : fields){
  10.             f.setAccessible(true);
  11.             try {                                                  
  12.                 if(f.getType().equals(String.class))
  13.                     json.put(f.getName(), (String)f.get(obj));                         
  14.                 else if(f.getType().equals(int.class) || f.getType().equals(Integer.class))
  15.                     json.put(f.getName(), f.getInt(obj));
  16.                 else if(f.getType().equals(long.class) || f.getType().equals(Long.class))
  17.                     json.put(f.getName(), f.getLong(obj));
  18.                 else if(f.getType().equals(Double.class) || f.getType().equals(double.class))
  19.                     json.put(f.getName(), f.getDouble(obj));
  20.                 else if(f.getType().equals(Boolean.class) || f.getType().equals(boolean.class))
  21.                     json.put(f.getName(), f.getBoolean(obj));
  22.                 else {
  23.                     Type type = f.getGenericType();
  24.                     if(type instanceof ParameterizedType){
  25.                          ParameterizedType pt = (ParameterizedType) type;  
  26.                          System.out.println("raw type: " + pt.getRawType());  
  27.                          if(pt.getRawType().equals(java.util.ArrayList.class) && pConvertArraylists == true){
  28.                              Console.debug(TAG, "We have an arraylist");
  29.                              for (Type t : pt.getActualTypeArguments()) {  
  30.                                  Console.debug(TAG, "    " + t + " session: " + Session.class);  
  31.                                  if(t.equals(Session.class)){
  32.                                      ArrayList<Session>list = (ArrayList<Session>)f.get(obj);
  33.                                      
  34.                                      if(list != null){
  35.                                          JSONArray jsonArray = new JSONArray();
  36.                                          for(int i = 0; i < list.size(); i++){
  37.                                              jsonArray.put(Convertor.toJson(list.get(i), false));
  38.                                          }
  39.                                          json.put(f.getName(), jsonArray);
  40.                                      }
  41.                                      else
  42.                                          Console.debug(TAG, "list is null in Converter.toJson");
  43.                                            
  44.                                  }
  45.                              }                           
  46.                          }                     
  47.                     }
  48.                 }
  49.             }
  50.             catch (IllegalArgumentException e) {
  51.                 e.printStackTrace();
  52.                 json = null;
  53.             }
  54.             catch (IllegalAccessException e) {
  55.                 e.printStackTrace();
  56.                 json = null;
  57.             } catch (JSONException e) {
  58.                 e.printStackTrace();
  59.                 json = null;
  60.             }                                                  
  61.         }
  62.        
  63.         if(json != null)
  64.             Console.debug(TAG, json.toString());
  65.         return json;
  66.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement