Skip to content

Commit

Permalink
Refactor util.JsonUtil pretty printing to use no GSON and deduplicate…
Browse files Browse the repository at this point in the history
… code. #6810
  • Loading branch information
poikilotherm committed Sep 11, 2020
1 parent cb93788 commit aa6b66e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 43 deletions.
48 changes: 17 additions & 31 deletions src/main/java/edu/harvard/iq/dataverse/util/json/JsonUtil.java
Original file line number Diff line number Diff line change
@@ -1,57 +1,43 @@
package edu.harvard.iq.dataverse.util.json;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Collections;
import java.util.logging.Logger;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonWriter;
import javax.json.JsonWriterFactory;
import javax.json.*;
import javax.json.stream.JsonGenerator;

public class JsonUtil {

private static final Logger logger = Logger.getLogger(JsonUtil.class.getCanonicalName());
private static JsonWriterFactory jsonWriterFactory = Json.createWriterFactory(Collections.singletonMap(JsonGenerator.PRETTY_PRINTING, true));

/**
* Make an attempt at pretty printing a String but will return the original
* string if it isn't JSON or if there is any exception.
* Null- and empty-safe.
*/
public static String prettyPrint(String jsonString) {
if (jsonString == null || "".equals(jsonString)) return jsonString;
try {
com.google.gson.JsonParser jsonParser = new com.google.gson.JsonParser();
JsonObject jsonObject = jsonParser.parse(jsonString).getAsJsonObject();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String prettyJson = gson.toJson(jsonObject);
return prettyJson;
JsonReader reader = Json.createReader(new StringReader(jsonString));
JsonStructure struct = reader.read();
return prettyPrint(struct);
} catch (Exception ex) {
logger.info("Returning original string due to exception: " + ex);
return jsonString;
}
}

public static String prettyPrint(JsonArray jsonArray) {
Map<String, Boolean> config = new HashMap<>();
config.put(JsonGenerator.PRETTY_PRINTING, true);
JsonWriterFactory jsonWriterFactory = Json.createWriterFactory(config);
StringWriter stringWriter = new StringWriter();
try (JsonWriter jsonWriter = jsonWriterFactory.createWriter(stringWriter)) {
jsonWriter.writeArray(jsonArray);
}
return stringWriter.toString();
}

public static String prettyPrint(javax.json.JsonObject jsonObject) {
Map<String, Boolean> config = new HashMap<>();
config.put(JsonGenerator.PRETTY_PRINTING, true);
JsonWriterFactory jsonWriterFactory = Json.createWriterFactory(config);

/**
* Prettyprint any Jakarta EE JSON-P structure (object, array, value, ...).
* @param jsonStructure
* @return Pretty JSON string
*/
public static String prettyPrint(JsonStructure jsonStructure) {
StringWriter stringWriter = new StringWriter();
try (JsonWriter jsonWriter = jsonWriterFactory.createWriter(stringWriter)) {
jsonWriter.writeObject(jsonObject);
jsonWriter.write(jsonStructure);
}
return stringWriter.toString();
}
Expand Down
37 changes: 25 additions & 12 deletions src/test/java/edu/harvard/iq/dataverse/util/json/JsonUtilTest.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
package edu.harvard.iq.dataverse.util.json;

import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.EmptySource;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.NullSource;

public class JsonUtilTest {
import java.util.Optional;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;

@Test
public void testPrettyPrint() {
JsonUtil jsonUtil = new JsonUtil();
String nullString = null;
assertEquals(null, JsonUtil.prettyPrint(nullString));
assertEquals("", JsonUtil.prettyPrint(""));
assertEquals("junk", JsonUtil.prettyPrint("junk"));
assertEquals("{}", JsonUtil.prettyPrint("{}"));
assertEquals("{\n" + " \"foo\": \"bar\"\n" + "}", JsonUtil.prettyPrint("{\"foo\": \"bar\"}"));
public class JsonUtilTest {

private static Stream<Arguments> testArgs() {
return Stream.of(
Arguments.of(null, null),
Arguments.of("", ""),
Arguments.of("junk", "junk"),
Arguments.of("{}", "\n{\n}"),
Arguments.of("{\"foo\": \"bar\"}", "\n{\n" + " \"foo\": \"bar\"\n" + "}"));
}


@ParameterizedTest
@MethodSource("testArgs")
public void testPrettyPrint(String input, String expected) {
assertEquals(expected, JsonUtil.prettyPrint(input));
}

}

0 comments on commit aa6b66e

Please sign in to comment.