Android Open Source - Android-Lib-Database J S O N Row Mapper






From Project

Back to project page Android-Lib-Database.

License

The source code is released under:

Apache License

If you think the Android project Android-Lib-Database 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 android.lib.database;
//from w  w  w  .  java  2 s .  co  m
import java.lang.reflect.Field;
import java.util.Iterator;

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

/**
 * Maps a {@link android.database.Cursor} or a {@link org.json.JSONObject} object to an instance of <code>T</code>.
 * @param <T> the type of instance to map.
 */
public class JSONRowMapper<T> extends RowMapper<T> {
    /**
     * Creates a new instance of {@link RowMapper} with the given class type.
     * @param clazz the class of the type to map.
     */
    public JSONRowMapper(final Class<T> clazz) {
        super(clazz);
    }

    /**
     * Maps a {@link org.json.JSONObject} object to an instance of <code>T</code>.
     * @param json the {@link org.json.JSONObject} object to map.
     * @return an instance of <code>T</code>.
     * @throws JSONException if no such mapping exists.
     * @throws IllegalAccessException if the if the default constructor of <code>T</code> is not visible,
     * or the default constructor of the {@link TypeConverter} associated with any field of <code>T</code> is not visible.
     * @throws InstantiationException if the instance of <code>T</code> can not be created,
     * or the instance of the {@link TypeConverter} associated with any field of <code>T</code> can not be created.
     * @throws IllegalArgumentException if the column name is not found from the given <code>cursor</code>.
     */
    public final T mapRow(final JSONObject json) throws IllegalArgumentException, IllegalAccessException, InstantiationException, JSONException {
        final T t = this.clazz.newInstance();

        for (final Iterator<String> key = json.keys(); key.hasNext();) {
            final String name = key.next();

            Field field = null;

            try {
                field = this.clazz.getDeclaredField(name);
            } catch (final SecurityException e) {
            } catch (final NoSuchFieldException e) {
            }

            if (field == null) {
                try {
                    field = this.clazz.getField(name);
                } catch (final SecurityException e) {
                } catch (final NoSuchFieldException e) {
                }
            }

            if (field != null) {
                field.setAccessible(true);

                field.set(this, json.get(name));
            }
        }

        return t;
    }
}




Java Source Code List

android.lib.database.Column.java
android.lib.database.CompositeIndex.java
android.lib.database.DatabaseOpenHelper.java
android.lib.database.Database.java
android.lib.database.DateConverter.java
android.lib.database.Index.java
android.lib.database.JSONRowMapper.java
android.lib.database.RowMapper.java
android.lib.database.Table.java
android.lib.database.TypeConverter.java
android.lib.database.UniqueCompositeIndex.java
android.lib.database.UnsupportedTypeException.java
android.lib.database.UseConverter.java
android.lib.database.predicate.ManySidedPredicate.java
android.lib.database.predicate.Predicate.java
android.lib.database.predicate.ThreeSidedPredicate.java
android.lib.database.predicate.TwoSidedPredicate.java
android.lib.database.query.Delete.java
android.lib.database.query.Insert.java
android.lib.database.query.QueryBuilder.java
android.lib.database.query.Query.java
android.lib.database.query.Select.java
android.lib.database.query.Update.java