Skip to content

Migrate labkey-client-api to use a modern JSON library: JSON-java #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 51 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
7faa6e9
Migrate labkey-client-api to use a modern JSON library: JSON-java
labkey-adam Sep 27, 2022
5a58bc1
Publish with new SNAPSHOT version
labkey-adam Sep 27, 2022
8b4ad73
Fixes for BasicTest
labkey-adam Sep 27, 2022
4fd094e
Sync up org.json.* version
labkey-adam Sep 27, 2022
7eaf718
Hey, "exceptionClass" is optional
labkey-adam Sep 27, 2022
66391e1
Pass JSONObject instead of Map for better null handling
labkey-adam Sep 27, 2022
ecc87bf
Attempt to fix Domain response object
labkey-adam Sep 27, 2022
2a4cad6
Optional
labkey-adam Sep 28, 2022
78b73a2
More optional
labkey-adam Sep 28, 2022
416e41f
Default to null instead of empty string
labkey-adam Sep 28, 2022
420573b
Optional
labkey-adam Sep 28, 2022
3bb8399
Fix subtle issue in CommandResponse that was causing our standard fix…
labkey-adam Sep 28, 2022
1ec4b21
Null check
labkey-adam Sep 28, 2022
2275190
Optional
labkey-adam Sep 28, 2022
72d193d
More optional properties
labkey-adam Sep 28, 2022
83e7e19
Even more optional properties
labkey-adam Sep 28, 2022
8d0f5b5
These are all optional as well
labkey-adam Sep 28, 2022
a319ddd
JSONArray -> List<Map<String, Object>>
labkey-adam Sep 29, 2022
3ee740f
Fix expanded format handling
labkey-adam Sep 29, 2022
979c40d
Optional
labkey-adam Sep 29, 2022
6e23d9a
Optional
labkey-adam Sep 29, 2022
3a38c4d
Long -> Integer
labkey-adam Sep 29, 2022
c8f3ac6
Try optNumber()
labkey-adam Sep 29, 2022
01d6d37
Protocol tweaks
labkey-adam Sep 29, 2022
b392418
Optional dataFileURL
labkey-adam Sep 29, 2022
2df41a5
Optional
labkey-adam Sep 29, 2022
aa965ed
Adjust for NULL object
labkey-adam Sep 30, 2022
7854b59
Optional
labkey-adam Oct 3, 2022
1c81d84
Revamp ExpObject to remove json-simple workaround
labkey-adam Oct 4, 2022
1139715
Optional id
labkey-adam Oct 5, 2022
8461515
Optional
labkey-adam Oct 5, 2022
34b4a7e
Merge branch 'develop' into fb_jsonUpgrade
labkey-adam Oct 5, 2022
96b6d90
Optional
labkey-adam Oct 5, 2022
22f2515
Optional
labkey-adam Oct 5, 2022
1aa4dcd
Optional
labkey-adam Oct 5, 2022
be9f882
Optional PHI
labkey-adam Oct 6, 2022
477b2da
Optional Run properties
labkey-adam Oct 6, 2022
455d327
User IDs are Integers
labkey-adam Oct 6, 2022
7b98b03
PHI is optional
labkey-adam Oct 6, 2022
2730c78
Another round with ExpObject
labkey-adam Oct 6, 2022
1d51965
Chipping away
labkey-adam Oct 7, 2022
86f4613
Casting, fix invalid JSON
labkey-adam Oct 7, 2022
30e1b01
Wrap 'null' values in SaveRowsCommand
labkey-tchad Oct 7, 2022
dc20416
Get correct default value for ExpObject ID
labkey-tchad Oct 10, 2022
636ba05
Fix material ids
labkey-tchad Oct 11, 2022
1f5796e
Simplify optional boolean properties
labkey-tchad Oct 17, 2022
2c53948
Tidy more optional properties
labkey-tchad Oct 17, 2022
989cb6f
Fix 'PropertyDescriptor' constructor
labkey-tchad Oct 17, 2022
0189daa
Fix null string
labkey-tchad Oct 18, 2022
609af23
Fix ClassCastException
labkey-tchad Oct 18, 2022
b8cf1a8
Update to non-fb client API version
labkey-tchad Oct 24, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion labkey-client-api/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# The LabKey Remote API Library for Java - Change Log

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

Expand Down
9 changes: 2 additions & 7 deletions labkey-client-api/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import org.labkey.gradle.plugin.LabKey
import org.labkey.gradle.util.BuildUtils
import org.labkey.gradle.util.GroupNames
import org.labkey.gradle.util.PomFileHelper

Expand Down Expand Up @@ -49,14 +48,10 @@ repositories {

group "org.labkey.api"

version "3.2.0-SNAPSHOT"
version "4.0.0-SNAPSHOT"

dependencies {
api ("com.googlecode.json-simple:json-simple:${jsonSimpleVersion}")
{
// exclude this because it gets in the way of our own JSON object implementations from server/api
exclude group: "org.json", module:"json"
}
api "org.json:json:${jsonObjectVersion}"
api "org.apache.httpcomponents.client5:httpclient5:${httpclient5Version}"
api "org.apache.httpcomponents.core5:httpcore5:${httpcore5Version}"
implementation "net.sf.opencsv:opencsv:${opencsvVersion}"
Expand Down
1 change: 1 addition & 0 deletions labkey-client-api/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ httpclient5Version=5.1.3
httpcore5Version=5.1.4

jsonSimpleVersion=1.1.1
jsonObjectVersion=20220924

junitVersion=4.13.2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package org.labkey.remoteapi;

import java.util.Map;
import org.json.JSONObject;

/**
* User: adam
Expand All @@ -24,8 +24,8 @@
*/
public class ApiVersionException extends CommandException
{
ApiVersionException(String message, int statusCode, Map<String, Object> properties, String responseText, String contentType)
ApiVersionException(String message, int statusCode, JSONObject jsonProperties, String responseText, String contentType)
{
super(message, statusCode, properties, responseText, contentType);
super(message, statusCode, jsonProperties, responseText, contentType);
}
}
26 changes: 11 additions & 15 deletions labkey-client-api/src/org/labkey/remoteapi/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,10 @@
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.net.URIBuilder;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -218,7 +211,10 @@ public ResponseType execute(Connection connection, String folderPath) throws IOE
if (null != contentType && contentType.contains(Command.CONTENT_TYPE_JSON))
{
// Read entire response body and parse into JSON object
json = (JSONObject) JSONValue.parse(response.getReader());
try (Reader reader = response.getReader())
{
json = new JSONObject(new JSONTokener(reader));
}
}
else
{
Expand Down Expand Up @@ -387,12 +383,12 @@ private void throwError(Response r, boolean throwByDefault) throws IOException,
// Parse JSON
if (responseText != null && responseText.length() > 0)
{
json = (JSONObject) JSONValue.parse(responseText);
if (json != null && json.containsKey("exception"))
json = new JSONObject(responseText);
if (json.has("exception"))
{
message = (String)json.get("exception");

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

throw new CommandException(message, r.getStatusCode(), json, responseText, contentType);
Expand Down
11 changes: 6 additions & 5 deletions labkey-client-api/src/org/labkey/remoteapi/CommandException.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.labkey.remoteapi;

import org.apache.hc.core5.http.impl.EnglishReasonPhraseCatalog;
import org.json.JSONObject;

import java.util.Locale;
import java.util.Map;
Expand All @@ -38,7 +39,7 @@ public class CommandException extends Exception
{
private final String _contentType;
private final int _statusCode;
private final Map<String, Object> _properties;
private final JSONObject _jsonProperties;
private final String _responseText;

/**
Expand All @@ -56,15 +57,15 @@ public CommandException(String message)
* exception property map, responseText, and contentType.
* @param message The message text (should not be null).
* @param statusCode The HTTP status code.
* @param properties The exception property map (may be null)
* @param jsonProperties The exception property JSONObject (may be null)
* @param responseText The full response text (may be null)
* @param contentType The response content type (may be null)
*/
public CommandException(String message, int statusCode, Map<String, Object> properties, String responseText, String contentType)
public CommandException(String message, int statusCode, JSONObject jsonProperties, String responseText, String contentType)
{
super(buildMessage(message, statusCode));
_statusCode = statusCode;
_properties = properties;
_jsonProperties = jsonProperties;
_responseText = responseText;
_contentType = contentType;
}
Expand Down Expand Up @@ -119,6 +120,6 @@ public String getResponseText()
*/
public Map<String, Object> getProperties()
{
return _properties;
return null == _jsonProperties ? null : _jsonProperties.toMap();
}
}
32 changes: 16 additions & 16 deletions labkey-client-api/src/org/labkey/remoteapi/CommandResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
*/
package org.labkey.remoteapi;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.JSONObject;

import java.util.List;
import java.util.Map;
Expand All @@ -43,11 +42,12 @@
*/
public class CommandResponse
{
private String _text;
private int _statusCode;
private String _contentType;
private JSONObject _data = null;
private Command _sourceCommand;
private final String _text;
private final int _statusCode;
private final String _contentType;
private final Command _sourceCommand;

private Map<String, Object> _data;

/**
* Constructs a new CommandResponse, initialized with the provided
Expand All @@ -63,7 +63,7 @@ public CommandResponse(String text, int statusCode, String contentType, JSONObje
_text = text;
_statusCode = statusCode;
_contentType = contentType;
_data = json;
_data = null != json ? json.toMap() : null;
_sourceCommand = sourceCommand;
}

Expand Down Expand Up @@ -134,9 +134,9 @@ public Command getSourceCommand()
@SuppressWarnings("unchecked")
public Map<String, Object> getParsedData()
{
if(null == _data && null != getText()
if (null == _data && null != getText()
&& null != _contentType && _contentType.contains(Command.CONTENT_TYPE_JSON))
_data = (JSONObject)JSONValue.parse(getText());
_data = new JSONObject(getText()).toMap();
return _data;
}

Expand All @@ -158,9 +158,9 @@ public <T> T getProperty(String path)
{
assert null != path;
String[] pathParts = path.split("\\.");
if(null == pathParts || pathParts.length == 0)
if (pathParts.length == 0)
return null;
return (T)getProperty(pathParts, 0, getParsedData());
return getProperty(pathParts, 0, getParsedData());
}

/**
Expand All @@ -176,7 +176,7 @@ public <T> T getProperty(String path)
@SuppressWarnings("unchecked")
protected <T> T getProperty(String[] path, int pathIndex, Map<String, Object> parent)
{
if(null == parent)
if (null == parent)
return null;

String key = path[pathIndex];
Expand All @@ -192,20 +192,20 @@ protected <T> T getProperty(String[] path, int pathIndex, Map<String, Object> pa
Object prop = parent.get(key);
if (arrayIndex != null)
{
if (prop != null && prop instanceof List && ((List)prop).size() > arrayIndex)
if (prop instanceof List && ((List) prop).size() > arrayIndex)
prop = ((List)prop).get(arrayIndex);
else
return null;
}

//if this was the last path part, return the prop
//will return null if final path part not found
if(pathIndex == (path.length -1))
if (pathIndex == (path.length -1))
return (T)prop;
else
{
//recurse if prop is non-null and instance of map
return (null != prop && prop instanceof Map)
return (prop instanceof Map)
? (T)getProperty(path, pathIndex + 1, (Map<String, Object>)prop)
: null;
}
Expand Down
4 changes: 2 additions & 2 deletions labkey-client-api/src/org/labkey/remoteapi/PostCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.apache.hc.client5.http.classic.methods.HttpUriRequest;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.json.simple.JSONObject;
import org.json.JSONObject;

import java.net.URI;

Expand Down Expand Up @@ -108,7 +108,7 @@ protected HttpUriRequest createRequest(URI uri)

if (null != json)
{
if (!json.containsKey(CommonParameters.apiVersion.name()) && getRequiredVersion() > 0)
if (!json.has(CommonParameters.apiVersion.name()) && getRequiredVersion() > 0)
json.put(CommonParameters.apiVersion.name(), getRequiredVersion());

request.setEntity(new StringEntity(json.toString(), ContentType.APPLICATION_JSON));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package org.labkey.remoteapi.assay;

import org.json.simple.JSONObject;
import org.json.JSONObject;
import org.labkey.remoteapi.PostCommand;

import java.util.HashMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package org.labkey.remoteapi.assay;

import org.json.simple.JSONObject;
import org.json.JSONObject;
import org.labkey.remoteapi.CommandResponse;

import java.util.List;
Expand Down
10 changes: 5 additions & 5 deletions labkey-client-api/src/org/labkey/remoteapi/assay/Batch.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package org.labkey.remoteapi.assay;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -37,9 +37,9 @@ public Batch()
public Batch(JSONObject json)
{
super(json);
if (json.containsKey("runs"))
if (json.has("runs"))
{
JSONArray array = (JSONArray)json.get("runs");
JSONArray array = json.getJSONArray("runs");
for (Object o : array)
{
JSONObject run = (JSONObject) o;
Expand All @@ -65,7 +65,7 @@ public JSONObject toJSONObject()
JSONArray runs = new JSONArray();
for (Run run : _runs)
{
runs.add(run.toJSONObject());
runs.put(run.toJSONObject());
}
result.put("runs", runs);
return result;
Expand Down
8 changes: 4 additions & 4 deletions labkey-client-api/src/org/labkey/remoteapi/assay/Data.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package org.labkey.remoteapi.assay;

import org.json.simple.JSONObject;
import org.json.JSONObject;

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

@Override
Expand Down
Loading