Skip to content

Commit

Permalink
Complete the Javadoc docs for JsonUtil.getJsonX
Browse files Browse the repository at this point in the history
  • Loading branch information
bencomp committed Oct 30, 2023
1 parent 47ea303 commit 235c038
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions src/main/java/edu/harvard/iq/dataverse/util/json/JsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.logging.Logger;
import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonException;
import jakarta.json.JsonObject;
import jakarta.json.JsonReader;
import jakarta.json.JsonWriter;
Expand Down Expand Up @@ -61,33 +62,51 @@ public static String prettyPrint(JsonObject jsonObject) {
}
return stringWriter.toString();
}


/**
* Return the contents of the string as a JSON object.
* This method closes its resources when an exception occurs, but does
* not catch any exceptions.
* @param serializedJson the JSON object serialized as a {@code String}
* @throws JsonException when parsing fails.
* @see #getJsonObject(InputStream)
* @see #getJsonObjectFromFile(String)
* @see #getJsonArray(String)
*/
public static JsonObject getJsonObject(String serializedJson) {
try (StringReader rdr = new StringReader(serializedJson)) {
try (JsonReader jsonReader = Json.createReader(rdr)) {
return jsonReader.readObject();
}
}
}

/**
* Return the contents of the {@link InputStream} as a JSON object.
*
* This method closes its resources when an exception occurs, but does
* not catch any exceptions.
* The caller of this method is responsible for closing the provided stream.
* @param stream the input stream to read from
* @throws JsonException when parsing fails.
* @see #getJsonObject(String)
* @see #getJsonObjectFromFile(String)
*/
public static JsonObject getJsonObject(InputStream stream) {
try (JsonReader jsonReader = Json.createReader(stream)) {
return jsonReader.readObject();
}
}

/**
* Return the contents of the file as a JSON object.
* This method closes its resources when an exception occurs, but does
* not catch any exceptions.
* @param fileName the name of the file to read from
* @throws FileNotFoundException when the file cannot be opened for reading
* @throws JsonException when parsing fails.
* @see #getJsonObject(String)
* @see #getJsonObject(InputStream)
*/
public static JsonObject getJsonObjectFromFile(String fileName) throws IOException {
try (FileReader rdr = new FileReader(fileName)) {
Expand All @@ -96,7 +115,15 @@ public static JsonObject getJsonObjectFromFile(String fileName) throws IOExcepti
}
}
}


/**
* Return the contents of the string as a JSON array.
* This method closes its resources when an exception occurs, but does
* not catch any exceptions.
* @param serializedJson the JSON array serialized as a {@code String}
* @throws JsonException when parsing fails.
* @see #getJsonObject(String)
*/
public static JsonArray getJsonArray(String serializedJson) {
try (StringReader rdr = new StringReader(serializedJson)) {
try (JsonReader jsonReader = Json.createReader(rdr)) {
Expand Down

0 comments on commit 235c038

Please sign in to comment.