Android Open Source - BaseAndroid Parser






From Project

Back to project page BaseAndroid.

License

The source code is released under:

MIT License

If you think the Android project BaseAndroid 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

/**
 * File: Parser.java/*from   w w w  .  java2 s  .com*/
 * CreationDate: 09/07/13
 * Author: "M. en C. Javier Silva Perez (JSP)"
 * Description:
 *  Parser class for AmaneceNET objects, this functions receives JSON string and parse them into the corresponding model
 * objects
 *
 */

package com.cmovil.baseandroid.model.ws;

import android.util.Log;

import com.cmovil.baseandroid.dao.ws.InvalidResponseException;
import com.cmovil.baseandroid.util.KeyDictionary;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;

import java.util.ArrayList;
import java.util.List;

/**
 * Parser class for AmaneceNET objects, this functions receives JSON string and parse them into the corresponding model
 * objects
 *
 * @param <T>
 *   Generic type for parsing json lists
 * @author "M. en C. Javier Silva Perez (JSP)"
 * @version 1.0
 * @since 09/07/13
 */
public class Parser<T> {

  /**
   * Parse a JSON string to the corresponding list of objects.
   *
   * @param json
   *   Corresponding JSON string
   * @return A list of objects if the JSON string was correct
   *
   * @throws com.google.gson.JsonSyntaxException
   *   If the string is not valid JSON
   */
  public List<T> parseWSObjects(String json, Class<T> type) throws InvalidResponseException {
    Gson gson = new Gson();
    JsonParser jsonParser = new JsonParser();
    List<T> list = new ArrayList<T>();
    try {
      JsonElement jsonElem = jsonParser.parse(json);
      if (jsonElem.isJsonArray()) {
        JsonArray arr = jsonElem.getAsJsonArray();
        for (JsonElement je : arr)
          list.add(gson.fromJson(je, type));
      } else {
        throw new InvalidResponseException("Server response invalid or unsupported format");
      }
      return list;
    } catch (ClassCastException ex) {
      Log.e(KeyDictionary.TAG, ex.getMessage(), ex);
      throw new InvalidResponseException(ex.getMessage());
    } catch (JsonSyntaxException ex) {
      Log.e(KeyDictionary.TAG, ex.getMessage(), ex);
      throw new InvalidResponseException(ex.getMessage());
    }
  }
}




Java Source Code List

com.cmovil.baseandroid.controller.BaseDBController.java
com.cmovil.baseandroid.controller.SampleController.java
com.cmovil.baseandroid.dao.db.BaseDBDAO.java
com.cmovil.baseandroid.dao.db.DBException.java
com.cmovil.baseandroid.dao.db.DatabaseDictionary.java
com.cmovil.baseandroid.dao.db.SampleDAO.java
com.cmovil.baseandroid.dao.db.helper.BaseDatabaseOpenHelper.java
com.cmovil.baseandroid.dao.db.helper.SampleOpenHelper.java
com.cmovil.baseandroid.dao.ws.BaseMessageWS.java
com.cmovil.baseandroid.dao.ws.InvalidResponseException.java
com.cmovil.baseandroid.dao.ws.WSClient.java
com.cmovil.baseandroid.model.db.BaseModel.java
com.cmovil.baseandroid.model.db.State.java
com.cmovil.baseandroid.model.ws.MessageErrorCode.java
com.cmovil.baseandroid.model.ws.Parser.java
com.cmovil.baseandroid.util.CMUtils.java
com.cmovil.baseandroid.util.CustomCatalogComparator.java
com.cmovil.baseandroid.util.KeyDictionary.java
com.cmovil.baseandroid.view.BaseActionBarActivity.java
com.cmovil.baseandroid.view.BaseDrawerActivity.java
com.cmovil.baseandroid.view.SplashActivity.java