Skip to content

Commit f608469

Browse files
Migrate labkey-client-api to use a modern JSON library: JSON-java (#38)
Co-authored-by: labkey-tchad <tchad@labkey.com>
1 parent 2883418 commit f608469

File tree

102 files changed

+369
-432
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+369
-432
lines changed

labkey-client-api/CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
# The LabKey Remote API Library for Java - Change Log
22

3-
## version TBD
3+
## version 4.0.0
44
*Released*: TBD
5+
* Replace the internal JSON library with [JSON-java](https://github.com/stleary/JSON-java). The previous library,
6+
[json-simple](https://github.com/fangyidong/json-simple) is no longer maintained (last released in early 2012) and
7+
lacks support for basic features like generics. This is an incompatible API change for developers who *write* their own
8+
Command classes; they will need to update their Command classes if/when they upgrade to v4.0.0. Developers who simply
9+
*use* Command classes should be able to upgrade without changes.
510
* Issue 46321: Remove `lib` directory from `fatJar` in favor of pulling dependencies via the published pom files when needed
611
* Remove artifactory plugin since we use the maven `publish` command now
712

labkey-client-api/build.gradle

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import org.labkey.gradle.plugin.LabKey
2-
import org.labkey.gradle.util.BuildUtils
32
import org.labkey.gradle.util.GroupNames
43
import org.labkey.gradle.util.PomFileHelper
54

@@ -49,14 +48,10 @@ repositories {
4948

5049
group "org.labkey.api"
5150

52-
version "3.2.0-SNAPSHOT"
51+
version "4.0.0-SNAPSHOT"
5352

5453
dependencies {
55-
api ("com.googlecode.json-simple:json-simple:${jsonSimpleVersion}")
56-
{
57-
// exclude this because it gets in the way of our own JSON object implementations from server/api
58-
exclude group: "org.json", module:"json"
59-
}
54+
api "org.json:json:${jsonObjectVersion}"
6055
api "org.apache.httpcomponents.client5:httpclient5:${httpclient5Version}"
6156
api "org.apache.httpcomponents.core5:httpcore5:${httpcore5Version}"
6257
implementation "net.sf.opencsv:opencsv:${opencsvVersion}"

labkey-client-api/gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ httpclient5Version=5.1.3
1818
httpcore5Version=5.1.4
1919

2020
jsonSimpleVersion=1.1.1
21+
jsonObjectVersion=20220924
2122

2223
junitVersion=4.13.2
2324

labkey-client-api/src/org/labkey/remoteapi/ApiVersionException.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.labkey.remoteapi;
1717

18-
import java.util.Map;
18+
import org.json.JSONObject;
1919

2020
/**
2121
* User: adam
@@ -24,8 +24,8 @@
2424
*/
2525
public class ApiVersionException extends CommandException
2626
{
27-
ApiVersionException(String message, int statusCode, Map<String, Object> properties, String responseText, String contentType)
27+
ApiVersionException(String message, int statusCode, JSONObject jsonProperties, String responseText, String contentType)
2828
{
29-
super(message, statusCode, properties, responseText, contentType);
29+
super(message, statusCode, jsonProperties, responseText, contentType);
3030
}
3131
}

labkey-client-api/src/org/labkey/remoteapi/Command.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,10 @@
2222
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
2323
import org.apache.hc.core5.http.Header;
2424
import org.apache.hc.core5.net.URIBuilder;
25-
import org.json.simple.JSONObject;
26-
import org.json.simple.JSONValue;
27-
28-
import java.io.BufferedReader;
29-
import java.io.ByteArrayInputStream;
30-
import java.io.Closeable;
31-
import java.io.IOException;
32-
import java.io.InputStream;
33-
import java.io.InputStreamReader;
34-
import java.io.Reader;
35-
import java.io.StringReader;
25+
import org.json.JSONObject;
26+
import org.json.JSONTokener;
27+
28+
import java.io.*;
3629
import java.net.URI;
3730
import java.net.URISyntaxException;
3831
import java.nio.charset.StandardCharsets;
@@ -218,7 +211,10 @@ public ResponseType execute(Connection connection, String folderPath) throws IOE
218211
if (null != contentType && contentType.contains(Command.CONTENT_TYPE_JSON))
219212
{
220213
// Read entire response body and parse into JSON object
221-
json = (JSONObject) JSONValue.parse(response.getReader());
214+
try (Reader reader = response.getReader())
215+
{
216+
json = new JSONObject(new JSONTokener(reader));
217+
}
222218
}
223219
else
224220
{
@@ -387,12 +383,12 @@ private void throwError(Response r, boolean throwByDefault) throws IOException,
387383
// Parse JSON
388384
if (responseText != null && responseText.length() > 0)
389385
{
390-
json = (JSONObject) JSONValue.parse(responseText);
391-
if (json != null && json.containsKey("exception"))
386+
json = new JSONObject(responseText);
387+
if (json.has("exception"))
392388
{
393389
message = (String)json.get("exception");
394390

395-
if ("org.labkey.api.action.ApiVersionException".equals(json.get("exceptionClass")))
391+
if ("org.labkey.api.action.ApiVersionException".equals(json.opt("exceptionClass")))
396392
throw new ApiVersionException(message, r.getStatusCode(), json, responseText, contentType);
397393

398394
throw new CommandException(message, r.getStatusCode(), json, responseText, contentType);

labkey-client-api/src/org/labkey/remoteapi/CommandException.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.labkey.remoteapi;
1717

1818
import org.apache.hc.core5.http.impl.EnglishReasonPhraseCatalog;
19+
import org.json.JSONObject;
1920

2021
import java.util.Locale;
2122
import java.util.Map;
@@ -38,7 +39,7 @@ public class CommandException extends Exception
3839
{
3940
private final String _contentType;
4041
private final int _statusCode;
41-
private final Map<String, Object> _properties;
42+
private final JSONObject _jsonProperties;
4243
private final String _responseText;
4344

4445
/**
@@ -56,15 +57,15 @@ public CommandException(String message)
5657
* exception property map, responseText, and contentType.
5758
* @param message The message text (should not be null).
5859
* @param statusCode The HTTP status code.
59-
* @param properties The exception property map (may be null)
60+
* @param jsonProperties The exception property JSONObject (may be null)
6061
* @param responseText The full response text (may be null)
6162
* @param contentType The response content type (may be null)
6263
*/
63-
public CommandException(String message, int statusCode, Map<String, Object> properties, String responseText, String contentType)
64+
public CommandException(String message, int statusCode, JSONObject jsonProperties, String responseText, String contentType)
6465
{
6566
super(buildMessage(message, statusCode));
6667
_statusCode = statusCode;
67-
_properties = properties;
68+
_jsonProperties = jsonProperties;
6869
_responseText = responseText;
6970
_contentType = contentType;
7071
}
@@ -119,6 +120,6 @@ public String getResponseText()
119120
*/
120121
public Map<String, Object> getProperties()
121122
{
122-
return _properties;
123+
return null == _jsonProperties ? null : _jsonProperties.toMap();
123124
}
124125
}

labkey-client-api/src/org/labkey/remoteapi/CommandResponse.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
*/
1616
package org.labkey.remoteapi;
1717

18-
import org.json.simple.JSONObject;
19-
import org.json.simple.JSONValue;
18+
import org.json.JSONObject;
2019

2120
import java.util.List;
2221
import java.util.Map;
@@ -43,11 +42,12 @@
4342
*/
4443
public class CommandResponse
4544
{
46-
private String _text;
47-
private int _statusCode;
48-
private String _contentType;
49-
private JSONObject _data = null;
50-
private Command _sourceCommand;
45+
private final String _text;
46+
private final int _statusCode;
47+
private final String _contentType;
48+
private final Command _sourceCommand;
49+
50+
private Map<String, Object> _data;
5151

5252
/**
5353
* Constructs a new CommandResponse, initialized with the provided
@@ -63,7 +63,7 @@ public CommandResponse(String text, int statusCode, String contentType, JSONObje
6363
_text = text;
6464
_statusCode = statusCode;
6565
_contentType = contentType;
66-
_data = json;
66+
_data = null != json ? json.toMap() : null;
6767
_sourceCommand = sourceCommand;
6868
}
6969

@@ -134,9 +134,9 @@ public Command getSourceCommand()
134134
@SuppressWarnings("unchecked")
135135
public Map<String, Object> getParsedData()
136136
{
137-
if(null == _data && null != getText()
137+
if (null == _data && null != getText()
138138
&& null != _contentType && _contentType.contains(Command.CONTENT_TYPE_JSON))
139-
_data = (JSONObject)JSONValue.parse(getText());
139+
_data = new JSONObject(getText()).toMap();
140140
return _data;
141141
}
142142

@@ -158,9 +158,9 @@ public <T> T getProperty(String path)
158158
{
159159
assert null != path;
160160
String[] pathParts = path.split("\\.");
161-
if(null == pathParts || pathParts.length == 0)
161+
if (pathParts.length == 0)
162162
return null;
163-
return (T)getProperty(pathParts, 0, getParsedData());
163+
return getProperty(pathParts, 0, getParsedData());
164164
}
165165

166166
/**
@@ -176,7 +176,7 @@ public <T> T getProperty(String path)
176176
@SuppressWarnings("unchecked")
177177
protected <T> T getProperty(String[] path, int pathIndex, Map<String, Object> parent)
178178
{
179-
if(null == parent)
179+
if (null == parent)
180180
return null;
181181

182182
String key = path[pathIndex];
@@ -192,20 +192,20 @@ protected <T> T getProperty(String[] path, int pathIndex, Map<String, Object> pa
192192
Object prop = parent.get(key);
193193
if (arrayIndex != null)
194194
{
195-
if (prop != null && prop instanceof List && ((List)prop).size() > arrayIndex)
195+
if (prop instanceof List && ((List) prop).size() > arrayIndex)
196196
prop = ((List)prop).get(arrayIndex);
197197
else
198198
return null;
199199
}
200200

201201
//if this was the last path part, return the prop
202202
//will return null if final path part not found
203-
if(pathIndex == (path.length -1))
203+
if (pathIndex == (path.length -1))
204204
return (T)prop;
205205
else
206206
{
207207
//recurse if prop is non-null and instance of map
208-
return (null != prop && prop instanceof Map)
208+
return (prop instanceof Map)
209209
? (T)getProperty(path, pathIndex + 1, (Map<String, Object>)prop)
210210
: null;
211211
}

labkey-client-api/src/org/labkey/remoteapi/PostCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import org.apache.hc.client5.http.classic.methods.HttpUriRequest;
2020
import org.apache.hc.core5.http.ContentType;
2121
import org.apache.hc.core5.http.io.entity.StringEntity;
22-
import org.json.simple.JSONObject;
22+
import org.json.JSONObject;
2323

2424
import java.net.URI;
2525

@@ -108,7 +108,7 @@ protected HttpUriRequest createRequest(URI uri)
108108

109109
if (null != json)
110110
{
111-
if (!json.containsKey(CommonParameters.apiVersion.name()) && getRequiredVersion() > 0)
111+
if (!json.has(CommonParameters.apiVersion.name()) && getRequiredVersion() > 0)
112112
json.put(CommonParameters.apiVersion.name(), getRequiredVersion());
113113

114114
request.setEntity(new StringEntity(json.toString(), ContentType.APPLICATION_JSON));

labkey-client-api/src/org/labkey/remoteapi/assay/AssayListCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.labkey.remoteapi.assay;
1717

18-
import org.json.simple.JSONObject;
18+
import org.json.JSONObject;
1919
import org.labkey.remoteapi.PostCommand;
2020

2121
import java.util.HashMap;

labkey-client-api/src/org/labkey/remoteapi/assay/AssayListResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.labkey.remoteapi.assay;
1717

18-
import org.json.simple.JSONObject;
18+
import org.json.JSONObject;
1919
import org.labkey.remoteapi.CommandResponse;
2020

2121
import java.util.List;

labkey-client-api/src/org/labkey/remoteapi/assay/Batch.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*/
1616
package org.labkey.remoteapi.assay;
1717

18-
import org.json.simple.JSONArray;
19-
import org.json.simple.JSONObject;
18+
import org.json.JSONArray;
19+
import org.json.JSONObject;
2020

2121
import java.util.ArrayList;
2222
import java.util.List;
@@ -37,9 +37,9 @@ public Batch()
3737
public Batch(JSONObject json)
3838
{
3939
super(json);
40-
if (json.containsKey("runs"))
40+
if (json.has("runs"))
4141
{
42-
JSONArray array = (JSONArray)json.get("runs");
42+
JSONArray array = json.getJSONArray("runs");
4343
for (Object o : array)
4444
{
4545
JSONObject run = (JSONObject) o;
@@ -65,7 +65,7 @@ public JSONObject toJSONObject()
6565
JSONArray runs = new JSONArray();
6666
for (Run run : _runs)
6767
{
68-
runs.add(run.toJSONObject());
68+
runs.put(run.toJSONObject());
6969
}
7070
result.put("runs", runs);
7171
return result;

labkey-client-api/src/org/labkey/remoteapi/assay/Data.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.labkey.remoteapi.assay;
1717

18-
import org.json.simple.JSONObject;
18+
import org.json.JSONObject;
1919

2020
/**
2121
* Represents a data file that may be used in a run. In order to successfully look up a data file when saving a run,
@@ -38,9 +38,9 @@ public Data()
3838
public Data(JSONObject json)
3939
{
4040
super(json);
41-
_dataFileURL = (String)json.get("dataFileURL");
42-
_absolutePath = (String)json.get("absolutePath");
43-
_pipelinePath = (String)json.get("pipelinePath");
41+
_dataFileURL = json.optString("dataFileURL", null);
42+
_absolutePath = json.optString("absolutePath", null);
43+
_pipelinePath = json.optString("pipelinePath", null);
4444
}
4545

4646
@Override

0 commit comments

Comments
 (0)