JSONObject toString and Base64 performance

In my app I am sending base64 encoded files in Json format. My code is:

JSONObject jsonRequest = prepareJsonObject(expense, contentUri);
String jsonString = jsonRequest.toString();
StringEntity se = new StringEntity(jsonString);

The jsonRequest object looks like this:

{
   "category":"660",
   "user":"458",
   "dated_on":"Wed Mar 12 10:38:11 GMT+00:00 2014",
   "description":"document",
   "gross_value":"-2.0",
   "currency":"GBP",
   "attachment":{
        "data":"<base64>"
        "mimetype":"image/jpeg"
   }
}

The problem is that jsonRequest.toString() is taking like 2 minutes for a 700Kb file.

Is there a way to make this quicker? Am I doing something wrong?

thanks.

EDIT: I am testing in an actual Nexus 4 running KITKAT.

For completion purpose, this is the code for prepareJsonObject(), which runs in less than 2 secs.

public static JSONObject prepareJsonObject(Expense expense, String contentUri){
        JSONObject expenseJson = null;
        try{
            expenseJson = new JSONObject();
            if(expense.getUserId()!=null) expenseJson.put("user", expense.getUserId().toString());
            if(expense.getProjectId()!=null) expenseJson.put("project", expense.getProjectId().toString());
            if(expense.getCurrency()!=null) expenseJson.put("currency", expense.getCurrency().toString());
            if(expense.getGrossValue()!=null) expenseJson.put("gross_value", expense.getGrossValue().toString());
            if(expense.getNativeGrossValue()!=null) expenseJson.put("native_gross_value", expense.getNativeGrossValue().toString());
            if(expense.getSalesTaxRate()!=null) expenseJson.put("sales_tax_rate", expense.getSalesTaxRate().toString());
            if(expense.getDescription()!=null)expenseJson.put("description", expense.getDescription().toString());
            if(expense.getDated()!=null)expenseJson.put("dated_on", expense.getDated());
            if(expense.getCategoryId()!=null) expenseJson.put("category", expense.getCategoryId().toString());
            if(expense.getManualSalesTaxAmount()!=null)expenseJson.put("manual_sales_tax_amount", expense.getManualSalesTaxAmount().toString());
            if(contentUri!=null){
                    JSONObject attachmentJson = new JSONObject();
                    String base64data = AttachmentsUtils.getBase64ForUri(Uri.parse(contentUri));
                    attachmentJson.put("data", base64data);
                    attachmentJson.put("content_type", AttachmentsUtils.getMimetypeFromContentUri(Uri.parse(contentUri)));
                    expenseJson.put("attachment", attachmentJson);

            }
        }catch(Exception e){
            Log.e("ExpensesUtils", "Couldn't encode attachment: "+e.getLocalizedMessage());
            return null;
        }
        return expenseJson;
    }

Answers


I think this is because the toString pare the full content of your base64 data to handle unicode characters and escape some specific characters. You can see this in JSONStringer#string, which is called for every string value in your JsonObject when you call toString.

Of course, as your data is base64, you don't actually need this. So I think you will need to implement your own toString implementation, probably based on the JSONStringer without escaping


Need Your Help

class-dump and MacOS frameworks

macos class dump social sharekit

I'm trying to dump some macos' private frameworks but I get nothing as output.

Run plugin updates outwith Eclipse UI

eclipse command-line plugins updates p2

I'm trying to update the plugins for an Eclipse Galileo SR2 installation from the command line.