Android Open Source - PrincePolo J S O N Parser






From Project

Back to project page PrincePolo.

License

The source code is released under:

GNU General Public License

If you think the Android project PrincePolo 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 se.agile.model;
/*www .j a  va  2 s . c  o  m*/
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

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

import se.agile.githubdata.Branch;
import se.agile.githubdata.Commit;
import se.agile.githubdata.Directory;
import se.agile.githubdata.File;
import se.agile.githubdata.Folder;
import se.agile.githubdata.Repository;
import se.agile.githubdata.User;
import android.util.Log;

public class JSONParser {
  private final static String logTag = "PrincePolo";

  public static ArrayList<Branch> parseBranches(String json){
    ArrayList<Branch> list = null;
    if(json != null){
      list = new ArrayList<Branch>();
      try {
        JSONArray jsonArray = new JSONArray(json);
        for(int i = 0; i < jsonArray.length(); i++){
          Branch branch = new Branch(jsonArray.getJSONObject(i).getString("name"));
          Commit commit = new Commit(jsonArray.getJSONObject(i).getJSONObject("commit").getString("url"));
          commit.setSha(jsonArray.getJSONObject(i).getJSONObject("commit").getString("sha"));
          commit.setIsComplete(false);
          branch.setLatestCommit(commit);
          list.add(branch);
        }
      } catch (JSONException e) {
        Log.e(logTag, "Couldn't parse JSON String to JSONArray");
        e.printStackTrace();
      }
    }else{
      Log.e(logTag, "parseBranches: json string is null");
    }
    return list;
  }
  
  public static ArrayList<Directory> parseDirectories(String json, String branchName){
    ArrayList<Directory> list = null;
    if(json != null){
      list = new ArrayList<Directory>();
      try {
        JSONArray jsonArray = new JSONArray(json);
        for(int i = 0; i < jsonArray.length(); i++){
          String type = jsonArray.getJSONObject(i).getString("type");
          String name = jsonArray.getJSONObject(i).getString("name");
          Directory dir = null;
          if(type.equals("file")){
            dir = new File(name, true);
          }else if(type.equals("dir")){
            dir = new Folder(name);
          }else{
            continue;
          }
          dir.setPath(jsonArray.getJSONObject(i).getString("path"));
          dir.setSha(jsonArray.getJSONObject(i).getString("sha"));
          dir.setUrl(jsonArray.getJSONObject(i).getString("url"));
          dir.setBranchName(branchName);
          list.add(dir);
        }
      } catch (JSONException e) {
        Log.e(logTag, "Couldn't parse JSON String to JSONArray");
        e.printStackTrace();
      }
    }else{
      Log.e(logTag, "parseBranches: json string is null");
    }
    return list;
  }
  
  public static Branch parseBranch(String json){
    Branch branch = null;
    if(json != null){
      try {
        JSONObject object = new JSONObject(json);
        String branchName = object.getString("name");
        String url = "https://api.github.com/repos/" + Preferences.getSelectedRepository().getName() + "/branches/" + branchName;
        branch = new Branch(branchName);
        branch.setUrl(url);
        branch.setLatestCommit(parseCommitObject(object.getJSONObject("commit")));
      } catch (JSONException e) {
        Log.e(logTag, "Couldn't parse JSON String to JSONArray");
        e.printStackTrace();
      }
    }else{
      Log.e(logTag, "json string is null");
    }
    return branch;
  }
  
  private static Commit parseCommitObject(JSONObject object) throws JSONException{
    String sha = object.getString("sha");
    String url = "https://api.github.com/repos/" + Preferences.getSelectedRepository().getName() + "/commits/" + sha;
    Commit commit = new Commit(url);
    commit.setSha(sha);
    JSONObject object2;
    try{
      object2 = object.getJSONObject("commit");
    }catch(JSONException e){
      object2 = object;
      //when parsing a shortcommit the "commit" object doesn't exist
    }
    
    commit.setCommitter(new User(object2.getJSONObject("committer").getString("name")));
    
      DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); 
      Date startDate = null;
      try {
        String dateString = object2.getJSONObject("committer").getString("date");
          startDate = df.parse(dateString);
      } catch (ParseException e) {
        Log.e(logTag, "couldn't parse date for commit");
          e.printStackTrace();
      }
    commit.setDate(startDate);
    commit.setMessage(object2.getString("message"));
    
    try{
      ArrayList<Commit> parentCommits = new ArrayList<Commit>();
      JSONArray parentArray = object.getJSONArray("parents");
      for(int i = 0; i < parentArray.length(); i++){
        JSONObject fileObject = parentArray.getJSONObject(i);
        Commit parent = new Commit(fileObject.getString("url"));
        parent.setSha(fileObject.getString("sha"));
        parentCommits.add(parent);
      }
      commit.setParentList(parentCommits);
    }catch(JSONException e){
      Log.d(logTag, "The commit has no Parent");
      e.printStackTrace();
      commit.setIsComplete(false);
    }
    
    
    ArrayList<File> changedFiles = new ArrayList<File>();
    try{
      JSONArray fileArray = object.getJSONArray("files");
      for(int i = 0; i < fileArray.length(); i++){
        JSONObject fileObject = fileArray.getJSONObject(i);
        String fileName = fileObject.getString("filename");
        int split = fileName.lastIndexOf("/");
        File file;
        if(split < 0){
          file = new File(fileName, true);
          file.setPath("");
        }else{
          file = new File(fileName.substring(split+1, fileName.length()), true);
          file.setPath(fileName.substring(0, split));
        }
        
        
        file.setStatus(fileObject.getString("status"));
        try{
          int additions = Integer.parseInt(fileObject.getString("additions"));
          int deletions = Integer.parseInt(fileObject.getString("deletions"));
          int changes = Integer.parseInt(fileObject.getString("changes"));
          file.setAdditions(additions);
          file.setChanges(changes);
          file.setDeletions(deletions);
        }catch(NumberFormatException e){
          Log.e(logTag, "Error while parsing commit integers");
          e.printStackTrace();
        }
        changedFiles.add(file);
      }
      commit.setChangedFiles(changedFiles);
      commit.setIsComplete(true);
    }catch(JSONException e){
      //This data is only available when you get a 'whole'/'complete' commit
      commit.setIsComplete(false);
    }
    
    return commit;
    
  }
  
  public synchronized static Commit parseCommit(String json, String branchName){
    Commit commit = null;
    if(json != null){
      try {
        JSONObject object = new JSONObject(json);
        commit = parseCommitObject(object);
        commit.setBranchName(branchName);
      } catch (JSONException e) {
        Log.e(logTag, "Couldn't parse commit");
        e.printStackTrace();
      }
    }else{
      Log.e(logTag, "parseFullCommit: json string is null");
    }
    return commit;
  }
  
  public static ArrayList<Repository> parseRepositories(String json){
    ArrayList<Repository> list = null;
    if(json != null){
      list = new ArrayList<Repository>();
      try {
        JSONArray jsonArray = new JSONArray(json);
        for (int i = 0; i < jsonArray.length(); i++) {
          JSONObject jsonObject = jsonArray.getJSONObject(i);
          try{
            list.add(new Repository(jsonObject.getString("full_name")));
          }catch(JSONException e){
            e.printStackTrace();
          }
        }
      } catch (JSONException e) {
        Log.e(logTag, "Couldn't parse JSON String to JSONArray");
        e.printStackTrace();
      }
    }else{
      Log.e(logTag, "parseRepositories: json string is null");
    }
    return list;
  }
  
  public static User parseUser(String json){
    User user = null;
    if(json != null){
      try {
        JSONObject object = new JSONObject(json);
        user = new User(object.getString("login"));
      } catch (JSONException e) {
        Log.e(logTag, "Error in interpreting JSON");
        e.printStackTrace();
      }
    }else{
      Log.e(logTag, "parseUser: json string is null");
    }
    return user;
  }
  
}




Java Source Code List

se.agile.activities.AboutFragment.java
se.agile.activities.BranchFragment.java
se.agile.activities.BranchesFragment.java
se.agile.activities.CardFragment.java
se.agile.activities.CollaboratorsFragment.java
se.agile.activities.CommitFragment.java
se.agile.activities.ConflictFragment.java
se.agile.activities.ConnectToGitHubFragment.java
se.agile.activities.LoginActivity.java
se.agile.activities.MainActivity.java
se.agile.activities.NotificationFragmentSwitcher.java
se.agile.activities.NotificationsFragment.java
se.agile.activities.PersonalNotesFragment.java
se.agile.activities.PokerFragment.java
se.agile.activities.RepositoryOverviewFragment.java
se.agile.activities.SelectRepositoryFragment.java
se.agile.activities.SelectWorkingFilesActivity.java
se.agile.activities.SelectWorkingFilesFragment.java
se.agile.activities.SelectWorkingFilesOverviewFragment.java
se.agile.activities.SettingsFragment.java
se.agile.activities.SignOutFragment.java
se.agile.asynctasks.RequestAccessToken.java
se.agile.asynctasks.RequestAllCommitsForBranch.java
se.agile.asynctasks.RequestAllFilesForBranch.java
se.agile.asynctasks.RequestBranch.java
se.agile.asynctasks.RequestBranches.java
se.agile.asynctasks.RequestFiles.java
se.agile.asynctasks.RequestFullCommit.java
se.agile.asynctasks.RequestListenerAdapter.java
se.agile.asynctasks.RequestListener.java
se.agile.asynctasks.RequestRepositories.java
se.agile.asynctasks.RequestShortCommit.java
se.agile.asynctasks.RequestTask.java
se.agile.asynctasks.RequestUser.java
se.agile.controller.MainActivityController.java
se.agile.controller.settingsFragmentController.java
se.agile.githubdata.Branch.java
se.agile.githubdata.Commit.java
se.agile.githubdata.Directory.java
se.agile.githubdata.File.java
se.agile.githubdata.Folder.java
se.agile.githubdata.GitHubDataInterface.java
se.agile.githubdata.Repository.java
se.agile.githubdata.User.java
se.agile.model.BranchNotification.java
se.agile.model.BranchSelectionModel.java
se.agile.model.CommitNotification.java
se.agile.model.ConflictNotification.java
se.agile.model.CreateNotificationActivity.java
se.agile.model.DirectoryListArrayAdapter.java
se.agile.model.InteractiveArrayAdapter.java
se.agile.model.JSONParser.java
se.agile.model.NotificationDialog.java
se.agile.model.NotificationHandler.java
se.agile.model.NotificationListArrayAdapter.java
se.agile.model.NotificationListener.java
se.agile.model.Notification.java
se.agile.model.PreferenceListener.java
se.agile.model.Preferences.java
se.agile.model.TemporaryStorage.java
se.agile.model.Tuple.java
se.agile.model.WorkingFileListArrayAdapter.java
se.agile.navigator.NavDrawerItem.java
se.agile.navigator.NavDrawerListAdapter.java