Skip to content

Commit

Permalink
Add support for encoding JSON object into a JSON string
Browse files Browse the repository at this point in the history
Introduced a JsonObject field that will preserve the current JSON object. This field is set in BoxJSONObject.update. BoxJSONObject.toString is overriden to convert this object to a string literal.

Closes box#52.
  • Loading branch information
richiehowelll committed Aug 12, 2020
1 parent ad0b9f3 commit 84f9d90
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/main/java/com/box/sdk/BoxJSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public abstract class BoxJSONObject {
*/
private JsonObject pendingChanges;

/**
* The current JSON object
*/
private JsonObject jsonObject;

/**
* A map of other BoxJSONObjects which will be lazily converted to a JsonObject once getPendingChanges is called.
* This allows changes to be made to a child BoxJSONObject and still have those changes reflected in the JSON
Expand Down Expand Up @@ -181,7 +186,7 @@ void removePendingChange(String key) {
}

/**
* Updates this BoxJSONObject using the information in a JSON object.
* Updates this BoxJSONObject using the information in a JSON object and preserves the JSON object.
* @param jsonObject the JSON object containing updated information.
*/
void update(JsonObject jsonObject) {
Expand All @@ -192,7 +197,7 @@ void update(JsonObject jsonObject) {

this.parseJSONMember(member);
}

this.jsonObject = jsonObject;
this.clearPendingChanges();
}

Expand All @@ -214,4 +219,13 @@ private JsonObject getPendingJSONObject() {
}
return this.pendingChanges;
}

/**
* Converts the JSON object into a string literal.
* @return a string representation of the JSON object.
*/
@Override
public String toString() {
return jsonObject.toString();
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/box/sdk/BoxUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,14 @@ public Info() {
super();
}

/**
* Constructs an Info object by parsing information from a JSON string.
* @param json the JSON string to parse.
*/
public Info(String json) {
super(json);
}

Info(JsonObject jsonObject) {
super(jsonObject);
}
Expand Down

0 comments on commit 84f9d90

Please sign in to comment.