Skip to content

Commit

Permalink
Use JSONParser.setUseLongsInstance and JSONParser.setUseBooleanInstan…
Browse files Browse the repository at this point in the history
…ce (#3796)
  • Loading branch information
nprice-flextrade authored Mar 12, 2024
1 parent ce54118 commit c7f843d
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions CodenameOne/src/com/codename1/io/rest/RequestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import com.codename1.properties.PropertyBusinessObject;
import com.codename1.ui.CN;
import com.codename1.ui.events.ActionListener;
import com.codename1.ui.util.EventDispatcher;
import com.codename1.util.Base64;
import com.codename1.util.Callback;
import com.codename1.util.FailureCallback;
Expand Down Expand Up @@ -88,6 +87,7 @@ public class RequestBuilder {
private Byte priority;
private boolean insecure;
private Boolean useBoolean;
private Boolean useLongs;

RequestBuilder(String method, String url) {
this.method = method;
Expand Down Expand Up @@ -121,6 +121,18 @@ public RequestBuilder useBoolean(boolean useBoolean) {
this.useBoolean = useBoolean;
return this;
}

/**
* Indicates if JSON should treat non-decimal numeric values as Long. If not set, uses the current default
* from the static value in JSONParser.isUseLongs
*
* @param useLongs true to return Long objects in JSON Maps
* @return this request builder
*/
public RequestBuilder useLongs(boolean useLongs) {
this.useLongs = useLongs;
return this;
}

/**
* Sets the caching mode for this request, see {@link com.codename1.io.ConnectionRequest#getCacheMode()}
Expand Down Expand Up @@ -863,20 +875,14 @@ protected void readUnzipedResponse(InputStream input) throws IOException {
return;
}
if(jsonErrorCallback != null) {
JSONParser jp = new JSONParser();
if(useBoolean != null) {
jp.setUseBoolean(useBoolean);
}
JSONParser jp = createJSONParser();
errorObject = jp.parseJSON(new InputStreamReader(input, "UTF-8"));
errorHandler = jsonErrorCallback;
return;
}
if(propertyErrorCallback != null) {
try {
JSONParser jp = new JSONParser();
if(useBoolean != null) {
jp.setUseBoolean(useBoolean);
}
JSONParser jp = createJSONParser();
Map m = jp.parseJSON(new InputStreamReader(input, "UTF-8"));
PropertyBusinessObject pb = (PropertyBusinessObject)errorHandlerPropertyType.newInstance();
pb.getPropertyIndex().populateFromMap(m);
Expand All @@ -893,10 +899,7 @@ protected void readUnzipedResponse(InputStream input) throws IOException {
return;
}
if(parseJSON) {
JSONParser parser = new JSONParser();
if(useBoolean != null) {
parser.setUseBoolean(useBoolean);
}
JSONParser parser = createJSONParser();
json = parser.parseJSON(new InputStreamReader(input, "UTF-8"));
if(hasResponseListeners() && !isKilled()) {
fireResponseListener(new NetworkEvent(this, json));
Expand All @@ -915,6 +918,17 @@ protected void postResponse() {

}

private JSONParser createJSONParser() {
JSONParser jp = new JSONParser();
if (useBoolean != null) {
jp.setUseBooleanInstance(useBoolean);
}
if (useLongs != null) {
jp.setUseLongsInstance(useLongs);
}
return jp;
}

private Connection createRequest(boolean parseJson) {
Connection req = new Connection(parseJson);
for (String key : pathParams.keySet()) {
Expand Down

0 comments on commit c7f843d

Please sign in to comment.