Note that there are some explanatory texts on larger screens.

plurals
  1. POA better approach to creating a JSON string in GWT?
    primarykey
    data
    text
    <p>I have a variable arguments defined as Object[][] enable my app to send a series of KEY/VALUE pairs to a function. Below the code handles Strings, Integers and ArrayLists of Strings and Integers. I'd like to extend this to support Doubles and Booleans (and ArrayLists of both).</p> <p>The function has a lot of repetitive code.</p> <p>Update: I am using the GWT framework.</p> <p>Q1. Is this the right approach but in need of a generic class provide easier maintenance? If so how? Q2. Is there a better alternative approach?</p> <pre><code>final static int KEY = 0; final static int VALUE = 1; ... String buildParamaters(Object[][] arguments) { JSONObject parameters = new JSONObject(); for (Object[] arg : arguments) { if (isType(arg[VALUE], "java.util.ArrayList")) { JSONArray array = new JSONArray(); if (isType(arg[VALUE], "java.lang.Integer")) { ArrayList&lt;String&gt; values = (ArrayList&lt;String&gt;) arg[VALUE]; for (int i=0; i&lt;values.size(); i++) { array.set(i, new JSONString(values.get(i))); } } else { ArrayList&lt;Integer&gt; values = (ArrayList&lt;Integer&gt;) arg[VALUE]; for (int i=0; i&lt;values.size(); i++) { array.set(i, new JSONNumber(values.get(i))); } } parameters.put(arg[KEY].toString(), array); } else if (isType(arg[VALUE], "java.lang.Integer")) { parameters.put(arg[KEY].toString(), new JSONNumber(((Integer)arg[VALUE]).doubleValue())); } else { parameters.put(arg[KEY].toString(), new JSONString((String)arg[VALUE])); } } return parameters.toString(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload