From d311810c2c55d764abc5eb86865d22c1223e92d8 Mon Sep 17 00:00:00 2001 From: Antonin Delpeuch Date: Tue, 20 Nov 2018 11:36:52 +0000 Subject: [PATCH] Migrate SortingConfig to Jackson --- .../google/refine/commands/row/GetRowsCommand.java | 11 +++-------- .../refine/commands/row/ReorderRowsCommand.java | 11 ++++------- .../google/refine/exporters/TemplatingExporter.java | 9 +-------- main/src/com/google/refine/sorting/SortingConfig.java | 6 ++---- .../operations/row/RowReorderOperationTests.java | 6 +----- .../refine/tests/sorting/SortingConfigTests.java | 6 ++---- 6 files changed, 13 insertions(+), 36 deletions(-) diff --git a/main/src/com/google/refine/commands/row/GetRowsCommand.java b/main/src/com/google/refine/commands/row/GetRowsCommand.java index 238c28823..cfeb21cdc 100644 --- a/main/src/com/google/refine/commands/row/GetRowsCommand.java +++ b/main/src/com/google/refine/commands/row/GetRowsCommand.java @@ -42,9 +42,6 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.json.JSONException; -import org.json.JSONObject; - import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonProperty; @@ -168,14 +165,12 @@ public class GetRowsCommand extends Command { RowWritingVisitor rwv = new RowWritingVisitor(start, limit); SortingConfig sortingConfig = null; - try{ - String json = request.getParameter("sorting"); - JSONObject sortingJson = (json == null) ? null : - ParsingUtilities.evaluateJsonStringToObject(json); + try { + String sortingJson = request.getParameter("sorting"); if (sortingJson != null) { sortingConfig = SortingConfig.reconstruct(sortingJson); } - } catch (JSONException e) { + } catch (IOException e) { } if (engine.getMode() == Mode.RowBased) { diff --git a/main/src/com/google/refine/commands/row/ReorderRowsCommand.java b/main/src/com/google/refine/commands/row/ReorderRowsCommand.java index 244b40c3f..fdb9ed798 100644 --- a/main/src/com/google/refine/commands/row/ReorderRowsCommand.java +++ b/main/src/com/google/refine/commands/row/ReorderRowsCommand.java @@ -33,10 +33,9 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. package com.google.refine.commands.row; -import javax.servlet.http.HttpServletRequest; +import java.io.IOException; -import org.json.JSONException; -import org.json.JSONObject; +import javax.servlet.http.HttpServletRequest; import com.google.refine.browsing.Engine; import com.google.refine.browsing.EngineConfig; @@ -45,7 +44,6 @@ import com.google.refine.model.AbstractOperation; import com.google.refine.model.Project; import com.google.refine.operations.row.RowReorderOperation; import com.google.refine.sorting.SortingConfig; -import com.google.refine.util.ParsingUtilities; public class ReorderRowsCommand extends EngineDependentCommand { @@ -59,9 +57,8 @@ public class ReorderRowsCommand extends EngineDependentCommand { try{ String json = request.getParameter("sorting"); - JSONObject sortingJson = (json == null) ? null : ParsingUtilities.evaluateJsonStringToObject(json); - sorting = (sortingJson == null) ? null : SortingConfig.reconstruct(sortingJson); - } catch (JSONException e) { + sorting = (json == null) ? null : SortingConfig.reconstruct(json); + } catch (IOException e) { // ignore } diff --git a/main/src/com/google/refine/exporters/TemplatingExporter.java b/main/src/com/google/refine/exporters/TemplatingExporter.java index a18a62b57..92cf230c7 100644 --- a/main/src/com/google/refine/exporters/TemplatingExporter.java +++ b/main/src/com/google/refine/exporters/TemplatingExporter.java @@ -38,7 +38,6 @@ import java.io.Writer; import java.util.Properties; import org.json.JSONException; -import org.json.JSONObject; import com.fasterxml.jackson.annotation.JsonProperty; import com.google.refine.browsing.Engine; @@ -88,13 +87,7 @@ public class TemplatingExporter implements WriterExporter { String limitString = options.getProperty("limit"); int limit = limitString != null ? Integer.parseInt(limitString) : -1; - JSONObject sortingJson = null; - try{ - String json = options.getProperty("sorting"); - sortingJson = (json == null) ? null : - ParsingUtilities.evaluateJsonStringToObject(json); - } catch (JSONException e) { - } + String sortingJson = options.getProperty("sorting"); String templateString = options.getProperty("template"); String prefixString = options.getProperty("prefix"); diff --git a/main/src/com/google/refine/sorting/SortingConfig.java b/main/src/com/google/refine/sorting/SortingConfig.java index cda207815..5d5f961df 100644 --- a/main/src/com/google/refine/sorting/SortingConfig.java +++ b/main/src/com/google/refine/sorting/SortingConfig.java @@ -2,8 +2,6 @@ package com.google.refine.sorting; import java.io.IOException; -import org.json.JSONObject; - import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.google.refine.util.ParsingUtilities; @@ -30,7 +28,7 @@ public final class SortingConfig { return _criteria; } - public static SortingConfig reconstruct(JSONObject obj) throws IOException { - return ParsingUtilities.mapper.readValue(obj.toString(), SortingConfig.class); + public static SortingConfig reconstruct(String obj) throws IOException { + return ParsingUtilities.mapper.readValue(obj, SortingConfig.class); } } \ No newline at end of file diff --git a/main/tests/server/src/com/google/refine/tests/operations/row/RowReorderOperationTests.java b/main/tests/server/src/com/google/refine/tests/operations/row/RowReorderOperationTests.java index adb1160bd..a21a83bb9 100644 --- a/main/tests/server/src/com/google/refine/tests/operations/row/RowReorderOperationTests.java +++ b/main/tests/server/src/com/google/refine/tests/operations/row/RowReorderOperationTests.java @@ -1,11 +1,8 @@ package com.google.refine.tests.operations.row; -import static org.mockito.Mockito.mock; - import java.util.Properties; import org.json.JSONException; -import org.json.JSONObject; import org.testng.Assert; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; @@ -52,7 +49,7 @@ public class RowReorderOperationTests extends RefineTest { @Test public void testSortEmptyString() throws Exception { String sortingJson = "{\"criteria\":[{\"column\":\"key\",\"valueType\":\"number\",\"reverse\":false,\"blankPosition\":2,\"errorPosition\":1}]}"; - SortingConfig sortingConfig = SortingConfig.reconstruct(new JSONObject(sortingJson)); + SortingConfig sortingConfig = SortingConfig.reconstruct(sortingJson); project.rows.get(1).cells.set(0, new Cell("", null)); AbstractOperation op = new RowReorderOperation( Mode.RowBased, sortingConfig @@ -69,7 +66,6 @@ public class RowReorderOperationTests extends RefineTest { @Test public void serializeRowReorderOperation() throws JSONException, Exception { - Project project = mock(Project.class); String json = " {\n" + " \"op\": \"core/row-reorder\",\n" + " \"description\": \"Reorder rows\",\n" + diff --git a/main/tests/server/src/com/google/refine/tests/sorting/SortingConfigTests.java b/main/tests/server/src/com/google/refine/tests/sorting/SortingConfigTests.java index 75903a1e0..837469313 100644 --- a/main/tests/server/src/com/google/refine/tests/sorting/SortingConfigTests.java +++ b/main/tests/server/src/com/google/refine/tests/sorting/SortingConfigTests.java @@ -2,8 +2,6 @@ package com.google.refine.tests.sorting; import java.io.IOException; -import org.json.JSONException; -import org.json.JSONObject; import org.testng.annotations.Test; import com.google.refine.sorting.SortingConfig; @@ -11,7 +9,7 @@ import com.google.refine.tests.util.TestUtils; public class SortingConfigTests { @Test - public void serializeSortingConfig() throws JSONException, IOException { + public void serializeSortingConfig() throws IOException { String json = "{\n" + " \"criteria\": [\n" + " {\n" + @@ -23,6 +21,6 @@ public class SortingConfigTests { " }\n" + " ]\n" + " }"; - TestUtils.isSerializedTo(SortingConfig.reconstruct(new JSONObject(json)), json); + TestUtils.isSerializedTo(SortingConfig.reconstruct(json), json); } }