Android Open Source - Dumbledroid Json Reflector






From Project

Back to project page Dumbledroid.

License

The source code is released under:

Copyright (c) 2013, Leocadio Tin? All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: ...

If you think the Android project Dumbledroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package io.leocad.dumbledroid.data;
/*w ww . java 2  s  .c  o  m*/
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Vector;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JsonReflector {

  public static void reflectJsonString(Object model, String contentString) throws JSONException {

    JSONObject jsonObj = null;

    try {
      jsonObj = new JSONObject(contentString);

    } catch (JSONException e) {
      //This is a JSONArray
    }

    if (jsonObj != null) {
      reflectJsonObject(model, jsonObj);
    } else {
      reflectJsonArray(model, new JSONArray(contentString));
    }
  }

  public static void reflectJsonObject(Object model, JSONObject jsonObj) throws JSONException {
    Class<?> modelClass = model.getClass();
    JSONArray names = jsonObj.names();

    if (names != null) {
      for (int i = 0; i < names.length(); i++) {

        String name = names.getString(i);

                if (jsonObj.isNull(name)) {
                    continue;
                }

        try {
          Field field = ReflectionHelper.getFieldInHierarchy(modelClass, name);

          if (field.getType() == List.class) {
            processListField(model, field, jsonObj.getJSONArray(name));

          } else {
            processSingleField(model, field, jsonObj, name);
          }

        } catch (NoSuchFieldException e) {
          Log.w(JsonReflector.class.getName(), "Can not locate field named " + name);

        } catch (IllegalAccessException e) {
          Log.w(JsonReflector.class.getName(), "Can not access field named " + name);

        } catch (InstantiationException e) {
          Log.w(JsonReflector.class.getName(), "Can not create an instance of the type defined in the field named " + name);
        }
      }
    }
  }

  private static void reflectJsonArray(Object model, JSONArray jsonArray) throws JSONException {

    Class<?> modelClass = model.getClass();
    try {
      Field listField = ReflectionHelper.getFieldInHierarchy(modelClass, "list");

      processListField(model, listField, jsonArray);

    } catch (NoSuchFieldException e) {
      Log.w(JsonReflector.class.getName(), "Can not locate field named list");

    } catch (IllegalArgumentException e) {
      Log.w(JsonReflector.class.getName(), "Can not put a List in the field named list");

    } catch (IllegalAccessException e) {
      Log.w(JsonReflector.class.getName(), "Can not access field named list");

    } catch (InstantiationException e) {
      Log.w(JsonReflector.class.getName(), "Can not create an instance of the type defined in the field named list");
    }
  }

  private static void processSingleField(Object model, Field field, JSONObject jsonObj, String nodeName) throws IllegalArgumentException, IllegalAccessException, JSONException, InstantiationException {

    Class<?> type = field.getType();

    field.set(model, getObject(jsonObj, type, nodeName));
  }

  private static Object getObject(JSONObject jsonObj, Class<?> type, String nodeName) throws JSONException, InstantiationException {

    if (type == String.class) {
      return jsonObj.getString(nodeName);

    } else if (type == boolean.class || type == Boolean.class) {
      return jsonObj.getBoolean(nodeName);

    } else if (type == int.class || type == Integer.class) {
      return jsonObj.getInt(nodeName);

    } else if (type == double.class || type == Double.class) {
      return jsonObj.getDouble(nodeName);

    } else {
      Object obj;
      try {
        obj = type.newInstance();

      } catch (IllegalAccessException e) {
        e.printStackTrace();
        return null;
      }

      if (!jsonObj.isNull(nodeName)) {
        reflectJsonObject(obj, jsonObj.getJSONObject(nodeName));

      } else {
        obj = null;
      }

      return obj;
    }
  }

  private static void processListField(Object object, Field field, JSONArray jsonArray) throws IllegalArgumentException, IllegalAccessException, JSONException, InstantiationException {

    ParameterizedType genericType = (ParameterizedType) field.getGenericType();
    Class<?> childrenType = (Class<?>) genericType.getActualTypeArguments()[0];

    field.set(object, getList(jsonArray, childrenType));
  }

  private static List<?> getList(JSONArray jsonArray, Class<?> childrenType) throws JSONException, IllegalAccessException, InstantiationException {

    List<Object> list = new Vector<Object>(jsonArray.length());

    for (int i = 0; i < jsonArray.length(); i++) {

      Object child = null;

      if (childrenType == List.class) {
        child = getList(jsonArray.getJSONArray(i), childrenType);

      } else if (childrenType == String.class) {
        child = jsonArray.getString(i);

      } else if (childrenType == boolean.class || childrenType == Boolean.class) {
        child = jsonArray.getBoolean(i);

      } else if (childrenType == int.class || childrenType == Integer.class) {
        child = jsonArray.getInt(i);

      } else if (childrenType == double.class || childrenType == Double.class) {
        child = jsonArray.getDouble(i);

      } else {
        child = childrenType.newInstance();
        reflectJsonObject(child, jsonArray.getJSONObject(i));
      }

      list.add(child);
    }

    return list;
  }
}




Java Source Code List

io.leocad.dumbledoreexample.activities.AboutActivity.java
io.leocad.dumbledoreexample.activities.BaseActivity.java
io.leocad.dumbledoreexample.activities.FlickrActivity.java
io.leocad.dumbledoreexample.activities.JediActivity.java
io.leocad.dumbledoreexample.activities.MainActivity.java
io.leocad.dumbledoreexample.activities.SithActivity.java
io.leocad.dumbledoreexample.adapters.FlickrAdapter.java
io.leocad.dumbledoreexample.models.FlickrPhotos.java
io.leocad.dumbledoreexample.models.Jedi.java
io.leocad.dumbledoreexample.models.Media.java
io.leocad.dumbledoreexample.models.PhotoItem.java
io.leocad.dumbledoreexample.models.Sith.java
io.leocad.dumbledoreexample.models.Suit.java
io.leocad.dumbledroid.data.AbstractModel.java
io.leocad.dumbledroid.data.DataController.java
io.leocad.dumbledroid.data.DataType.java
io.leocad.dumbledroid.data.JsonReflector.java
io.leocad.dumbledroid.data.ReflectionHelper.java
io.leocad.dumbledroid.data.XmlReflector.java
io.leocad.dumbledroid.data.cache.DiskCache.java
io.leocad.dumbledroid.data.cache.FileController.java
io.leocad.dumbledroid.data.cache.MemoryCache.java
io.leocad.dumbledroid.data.cache.ModelHolder.java
io.leocad.dumbledroid.data.cache.ObjectCopier.java
io.leocad.dumbledroid.data.xml.Node.java
io.leocad.dumbledroid.data.xml.SaxHandler.java
io.leocad.dumbledroid.data.xml.SaxParser.java
io.leocad.dumbledroid.net.HttpLoader.java
io.leocad.dumbledroid.net.HttpMethod.java
io.leocad.dumbledroid.net.NoConnectionException.java
io.leocad.dumbledroid.net.TimeoutException.java
io.leocad.dumbledroidplugin.core.ClassMapper.java
io.leocad.dumbledroidplugin.core.ClassWriter.java
io.leocad.dumbledroidplugin.core.DumbledroidClassCreator.java
io.leocad.dumbledroidplugin.core.FileUtils.java
io.leocad.dumbledroidplugin.core.JsonReverseReflector.java
io.leocad.dumbledroidplugin.core.XmlReverseReflector.java
io.leocad.dumbledroidplugin.exceptions.InvalidContentException.java
io.leocad.dumbledroidplugin.exceptions.InvalidUrlException.java
io.leocad.dumbledroidplugin.exceptions.UnsupportedContentTypeException.java
io.leocad.dumbledroidplugin.wizards.DataInputPage.java
io.leocad.dumbledroidplugin.wizards.FileCreationPage.java
io.leocad.dumbledroidplugin.wizards.NewModelWizard.java
org.apache.commons.validator.routines.DomainValidator.java
org.apache.commons.validator.routines.InetAddressValidator.java
org.apache.commons.validator.routines.RegexValidator.java
org.apache.commons.validator.routines.UrlValidator.java