public static JSONObject toJSON(Account account) throws JSONException {
    if (account == null) {
      return null;
    }
    JSONObject json = new JSONObject();
    json.put("user_id", account.getUserId());
    if (account.getUser() != null) {
      json.put("user", UserJSONConverter.toJSON(account.getUser()));
    }
    json.put("service_provider", account.getServiceProviderNo());

    json.put("auth_version", account.getAuthVersion());
    json.put("access_token", account.getAuthToken());
    String encryptedSecret =
        RsaUtil.encryptWithPublicKey(account.getAuthSecret(), Constants.PUBLIC_KEY);
    json.put("token_secret", encryptedSecret);

    json.put("app_key", account.getAppKey());
    encryptedSecret = RsaUtil.encryptWithPublicKey(account.getAppSecret(), Constants.PUBLIC_KEY);
    json.put("app_secret", encryptedSecret);

    json.put("state", account.getState());
    DateFormat dateFormat = DateTimeUtil.getGMTDateFormat();
    String createdTimeString =
        account.getCreatedAt() == null ? null : dateFormat.format(account.getCreatedAt());
    json.put("created_at", createdTimeString);
    json.put("is_default", account.isDefault());
    json.put("rest_proxy_url", account.getRestProxyUrl());
    json.put("search_proxy_url", account.getSearchProxyUrl());
    String expiresTimeString =
        account.getTokenExpiresAt() == null ? null : dateFormat.format(account.getTokenExpiresAt());
    json.put("token_expires_at", expiresTimeString);
    json.put("token_scopes", account.getTokenScopes());
    return json;
  }
  public static Account toAccount(JSONObject json) throws JSONException, ParseException {
    if (json == null) {
      return null;
    }
    Account account = new Account();
    account.setAccountId(0L);

    account.setAuthToken(ParseUtil.getRawString("access_token", json));
    String secretString = ParseUtil.getRawString("token_secret", json);
    String plainSecret = RsaUtil.decryptWithPublicKey(secretString, Constants.PUBLIC_KEY);
    account.setAuthSecret(plainSecret);

    account.setAppKey(ParseUtil.getRawString("app_key", json));
    secretString = ParseUtil.getRawString("app_secret", json);
    plainSecret = RsaUtil.decryptWithPublicKey(secretString, Constants.PUBLIC_KEY);
    account.setAppSecret(plainSecret);

    account.setAuthVersion(ParseUtil.getInt("auth_version", json));
    account.setServiceProviderNo(ParseUtil.getInt("service_provider", json));
    if (json.isNull("state")) {
      account.setState(Account.STATE_SYNCED);
    } else {
      account.setState(ParseUtil.getInt("state", json));
    }
    account.setUserId(ParseUtil.getRawString("user_id", json));
    if (!json.isNull("user")) {
      account.setUser(UserJSONConverter.toUser(json.getJSONObject("user")));
    }
    account.setCreatedAt(ParseUtil.getDate("created_at", json));
    account.setDefault(ParseUtil.getBoolean("is_default", json));
    account.setRestProxyUrl(ParseUtil.getRawString("rest_proxy_url", json));
    account.setSearchProxyUrl(ParseUtil.getRawString("search_proxy_url", json));
    account.setTokenExpiresAt(ParseUtil.getDate("token_expires_at", json));
    account.setTokenScopes(ParseUtil.getRawString("token_scopes", json));
    return account;
  }