Android Open Source - Munin-for-Android J S O N Helper






From Project

Back to project page Munin-for-Android.

License

The source code is released under:

GNU General Public License

If you think the Android project Munin-for-Android 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 com.chteuchteu.munin.hlpr;
// www  .jav  a 2  s. co  m
import com.chteuchteu.munin.obj.MuninMaster;
import com.chteuchteu.munin.obj.MuninMaster.DynazoomAvailability;
import com.chteuchteu.munin.obj.MuninPlugin;
import com.chteuchteu.munin.obj.MuninServer;
import com.chteuchteu.munin.obj.MuninServer.AuthType;

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

import java.util.ArrayList;

/**
 * Serialization util class
 * Used for import/export
 */
public class JSONHelper {
  /**
   * Serializes the Masters objects tree
   * Encrypts the password using the Util.Encryption util methods
   * @param masters Masters to be serialized
   * @param seed Seed used in order to encrypt passwords
   * @return JSON representation of the masters parameter
   */
  public static String getMastersJSONString(ArrayList<MuninMaster> masters, String seed) {
    try {
      JSONObject obj = new JSONObject();
      JSONArray jsonMasters = new JSONArray();

      for (MuninMaster master : masters) {
        if (master.getChildren().size() > 0) {
          JSONObject jsonMaster = new JSONObject();
          jsonMaster.put("id", master.getId());
          jsonMaster.put("name", master.getName());
          jsonMaster.put("url", master.getUrl());
          jsonMaster.put("hdGraphs", master.isDynazoomAvailable().getVal());
          jsonMaster.put("ssl", master.getSSL());
          switch (master.getAuthType()) {
            case NONE: case UNKNOWN:
              jsonMaster.put("authType", "none");
              break;
            case BASIC:
              jsonMaster.put("authType", "basic");
              jsonMaster.put("authLogin", master.getAuthLogin());
              String basicPassword = master.getAuthPassword();
              String encryptedBasicPassword = EncryptionHelper.encrypt(seed, basicPassword);
              jsonMaster.put("authPassword", encryptedBasicPassword);
              break;
            case DIGEST:
              jsonMaster.put("authType", "digest");
              jsonMaster.put("authLogin", master.getAuthLogin());
              String digestPassword = master.getAuthPassword();
              String encryptedDigestPassword = EncryptionHelper.encrypt(seed, digestPassword);
              jsonMaster.put("authPassword", encryptedDigestPassword);
              jsonMaster.put("authString", master.getAuthString());
              break;
          }
          
          JSONArray jsonServers = new JSONArray();
          for (MuninServer server : master.getChildren()) {
            JSONObject jsonServer = new JSONObject();
            
            jsonServer.put("id", server.getId());
            jsonServer.put("name", server.getName());
            jsonServer.put("serverUrl", server.getServerUrl());
            jsonServer.put("position", server.getPosition());
            jsonServer.put("graphURL", server.getGraphURL());
            jsonServer.put("hdGraphURL", server.getHdGraphURL());

            JSONArray jsonPlugins = new JSONArray();
            for (MuninPlugin plugin : server.getPlugins()) {
              JSONObject jsonPlugin = new JSONObject();
              
              jsonPlugin.put("id", plugin.getId());
              jsonPlugin.put("name", plugin.getName());
              jsonPlugin.put("fancyName", plugin.getFancyName());
              jsonPlugin.put("category", plugin.getCategory());
              jsonPlugin.put("pluginPageUrl", plugin.getPluginPageUrl());
              
              jsonPlugins.put(jsonPlugin);
            }
            jsonServer.put("plugins", jsonPlugins);
            
            jsonServers.put(jsonServer);
          }
          jsonMaster.put("servers", jsonServers);
          
          jsonMasters.put(jsonMaster);
        }
      }
      
      obj.put("masters", jsonMasters);
      return obj.toString();
    } catch (Exception ex) {
      ex.printStackTrace();
      return "";
    }
  }
  
  public static ArrayList<MuninMaster> getMastersFromJSON(JSONObject obj, String seed) {
    try {
      ArrayList<MuninMaster> muninMasters = new ArrayList<>();
      
      JSONArray jsonMasters = obj.getJSONArray("masters");
      for (int i=0; i<jsonMasters.length(); i++) {
        JSONObject jsonMaster = jsonMasters.getJSONObject(i);
        MuninMaster master = new MuninMaster();
        master.setId(jsonMaster.getLong("id"));
        master.setName(jsonMaster.getString("name"));
        master.setUrl(jsonMaster.getString("url"));
        master.setDynazoomAvailable(DynazoomAvailability.get(jsonMaster.getString("hdGraphs")));
        String authType = jsonMaster.getString("authType");
        switch (authType) {
          case "none":
            master.setAuthType(AuthType.NONE);
            break;
          case "basic": {
            master.setAuthType(AuthType.BASIC);
            String login = jsonMaster.getString("authLogin");
            String encryptedBasicPassword = jsonMaster.getString("authPassword");
            String decryptedBasicPassword = EncryptionHelper.decrypt(seed, encryptedBasicPassword);
            master.setAuthIds(login, decryptedBasicPassword);
            break;
          }
          case "digest": {
            master.setAuthType(AuthType.DIGEST);
            String login = jsonMaster.getString("authLogin");
            String encryptedDigestPassword = jsonMaster.getString("authPassword");
            String decryptedDigestPassword = EncryptionHelper.decrypt(seed, encryptedDigestPassword);
            master.setAuthIds(login, decryptedDigestPassword);
            master.setAuthString(jsonMaster.getString("authString"));
            break;
          }
        }
        
        JSONArray jsonServers = jsonMaster.getJSONArray("servers");
        for (int y=0; y<jsonServers.length(); y++) {
          JSONObject jsonServer = jsonServers.getJSONObject(y);
          MuninServer server = new MuninServer();
          
          server.setId(jsonServer.getLong("id"));
          server.setName(jsonServer.getString("name"));
          server.setServerUrl(jsonServer.getString("serverUrl"));
          server.setPosition(jsonServer.getInt("position"));
          server.setGraphURL(jsonServer.getString("graphURL"));
          server.setHdGraphURL(jsonServer.getString("hdGraphURL"));

          JSONArray jsonPlugins = jsonServer.getJSONArray("plugins");
          for (int z=0; z<jsonPlugins.length(); z++) {
            JSONObject jsonPlugin = jsonPlugins.getJSONObject(z);
            MuninPlugin plugin = new MuninPlugin();
            plugin.setInstalledOn(server);
            plugin.setId(jsonPlugin.getLong("id"));
            plugin.setName(jsonPlugin.getString("name"));
            plugin.setFancyName(jsonPlugin.getString("fancyName"));
            plugin.setCategory(jsonPlugin.getString("category"));
            plugin.setPluginPageUrl(jsonPlugin.getString("pluginPageUrl"));
            server.getPlugins().add(plugin);
          }
          
          server.setParent(master);
        }
        
        muninMasters.add(master);
      }
      
      return muninMasters;
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
}




Java Source Code List

com.chteuchteu.munin.BootReceiver.java
com.chteuchteu.munin.CustomSSLFactory.java
com.chteuchteu.munin.MuninFoo.java
com.chteuchteu.munin.Service_Notifications.java
com.chteuchteu.munin.adptr.Adapter_ExpandableListView.java
com.chteuchteu.munin.adptr.Adapter_GraphView.java
com.chteuchteu.munin.adptr.Adapter_IconList.java
com.chteuchteu.munin.adptr.Adapter_SeparatedList.java
com.chteuchteu.munin.exc.ImportExportWebserviceException.java
com.chteuchteu.munin.exc.NullMuninFooException.java
com.chteuchteu.munin.exc.TrialExpirationDateReached.java
com.chteuchteu.munin.hlpr.BillingService.java
com.chteuchteu.munin.hlpr.DatabaseHelper.java
com.chteuchteu.munin.hlpr.DigestUtils.java
com.chteuchteu.munin.hlpr.DocumentationHelper.java
com.chteuchteu.munin.hlpr.DrawerHelper.java
com.chteuchteu.munin.hlpr.DynazoomHelper.java
com.chteuchteu.munin.hlpr.EncryptionHelper.java
com.chteuchteu.munin.hlpr.GridDownloadHelper.java
com.chteuchteu.munin.hlpr.I18nHelper.java
com.chteuchteu.munin.hlpr.ImportExportHelper.java
com.chteuchteu.munin.hlpr.JSONHelper.java
com.chteuchteu.munin.hlpr.MediaScannerUtil.java
com.chteuchteu.munin.hlpr.NetHelper.java
com.chteuchteu.munin.hlpr.SQLite.java
com.chteuchteu.munin.hlpr.Util.java
com.chteuchteu.munin.obj.AlertsWidget.java
com.chteuchteu.munin.obj.GraphWidget.java
com.chteuchteu.munin.obj.GridItem.java
com.chteuchteu.munin.obj.Grid.java
com.chteuchteu.munin.obj.HTTPResponse_Bitmap.java
com.chteuchteu.munin.obj.HTTPResponse.java
com.chteuchteu.munin.obj.Label.java
com.chteuchteu.munin.obj.MuninMaster.java
com.chteuchteu.munin.obj.MuninPlugin.java
com.chteuchteu.munin.obj.MuninServer.java
com.chteuchteu.munin.obj.SearchResult.java
com.chteuchteu.munin.ui.Activity_About.java
com.chteuchteu.munin.ui.Activity_AlertsPluginSelection.java
com.chteuchteu.munin.ui.Activity_Alerts.java
com.chteuchteu.munin.ui.Activity_GoPremium.java
com.chteuchteu.munin.ui.Activity_GraphView.java
com.chteuchteu.munin.ui.Activity_Grid.java
com.chteuchteu.munin.ui.Activity_Grids.java
com.chteuchteu.munin.ui.Activity_Label.java
com.chteuchteu.munin.ui.Activity_Labels.java
com.chteuchteu.munin.ui.Activity_Main.java
com.chteuchteu.munin.ui.Activity_Notifications.java
com.chteuchteu.munin.ui.Activity_Plugins.java
com.chteuchteu.munin.ui.Activity_Server.java
com.chteuchteu.munin.ui.Activity_ServersEdit.java
com.chteuchteu.munin.ui.Activity_Servers.java
com.chteuchteu.munin.ui.Activity_Settings.java
com.chteuchteu.munin.ui.HackyDrawerLayout.java
com.chteuchteu.munin.ui.MuninActivity.java
com.chteuchteu.munin.wdget.Widget_AlertsWidget_Configure.java
com.chteuchteu.munin.wdget.Widget_AlertsWidget_ViewsFactory.java
com.chteuchteu.munin.wdget.Widget_AlertsWidget_WidgetProvider.java
com.chteuchteu.munin.wdget.Widget_AlertsWidget_WidgetService.java
com.chteuchteu.munin.wdget.Widget_GraphWidget_Configure.java
com.chteuchteu.munin.wdget.Widget_GraphWidget_WidgetProvider.java
com.mobeta.android.dslv.DragSortController.java
com.mobeta.android.dslv.DragSortCursorAdapter.java
com.mobeta.android.dslv.DragSortItemViewCheckable.java
com.mobeta.android.dslv.DragSortItemView.java
com.mobeta.android.dslv.DragSortListView.java
com.mobeta.android.dslv.ResourceDragSortCursorAdapter.java
com.mobeta.android.dslv.SimpleDragSortCursorAdapter.java
com.mobeta.android.dslv.SimpleFloatViewManager.java
org.taptwo.android.widget.CircleFlowIndicator.java
org.taptwo.android.widget.FlowIndicator.java
org.taptwo.android.widget.TitleFlowIndicator.java
org.taptwo.android.widget.TitleProvider.java
org.taptwo.android.widget.ViewFlow.java
uk.co.senab.photoview.Compat.java
uk.co.senab.photoview.IPhotoView.java
uk.co.senab.photoview.PhotoViewAttacher.java
uk.co.senab.photoview.PhotoView.java
uk.co.senab.photoview.SDK16.java
uk.co.senab.photoview.ScrollerProxy.java
uk.co.senab.photoview.VersionedGestureDetector.java