Jackson deserialization for MassEditOperation

This commit is contained in:
Antonin Delpeuch 2018-10-22 13:25:28 +01:00
parent 50ee6ecc61
commit d493bf0720
3 changed files with 41 additions and 55 deletions

View File

@ -33,13 +33,18 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package com.google.refine.commands.cell;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.refine.browsing.EngineConfig;
import com.google.refine.commands.EngineDependentCommand;
import com.google.refine.model.AbstractOperation;
import com.google.refine.model.Project;
import com.google.refine.operations.cell.MassEditOperation;
import com.google.refine.operations.cell.MassEditOperation.Edit;
import com.google.refine.util.ParsingUtilities;
public class MassEditCommand extends EngineDependentCommand {
@ -55,7 +60,7 @@ public class MassEditCommand extends EngineDependentCommand {
engineConfig,
columnName,
expression,
MassEditOperation.reconstructEdits(ParsingUtilities.evaluateJsonStringToArray(editsString))
ParsingUtilities.mapper.readValue(ParsingUtilities.evaluateJsonStringToArray(editsString).toString(), MassEditOperation.new TypeReference<List<Edit>>() {})
);
}
}

View File

@ -40,9 +40,9 @@ import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.json.JSONArray;
import org.json.JSONObject;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.refine.browsing.EngineConfig;
@ -74,61 +74,40 @@ public class MassEditOperation extends EngineDependentMassCellOperation {
@JsonProperty("to")
final public Serializable to;
public Edit(List<String> from, boolean fromBlank, boolean fromError, Serializable to) {
public Edit(
List<String> from,
boolean fromBlank,
boolean fromError,
Serializable to) {
this.from = from;
this.fromBlank = fromBlank;
this.fromError = fromError;
this.to = to;
}
@JsonCreator
public static Edit deserialize(
@JsonProperty("from")
List<String> from,
@JsonProperty("fromBlank")
boolean fromBlank,
@JsonProperty("fromError")
boolean fromError,
@JsonProperty("to")
Object to,
@JsonProperty("type")
String type) {
Serializable serializable = (Serializable)to;
if ("date".equals(type)) {
serializable = ParsingUtilities.stringToDate((String) to);
}
return new Edit(from == null ? new ArrayList<>() : from,
fromBlank, fromError, serializable);
}
}
static public AbstractOperation reconstruct(Project project, JSONObject obj) throws Exception {
JSONObject engineConfig = obj.has("engineConfig") && !obj.isNull("engineConfig") ?
obj.getJSONObject("engineConfig") : null;
return new MassEditOperation(
EngineConfig.reconstruct(engineConfig),
obj.getString("columnName"),
obj.getString("expression"),
reconstructEdits(obj.getJSONArray("edits"))
);
}
static public List<Edit> reconstructEdits(JSONArray editsA) throws Exception {
int editCount = editsA.length();
List<Edit> edits = new ArrayList<Edit>(editCount);
for (int i = 0; i < editCount; i++) {
JSONObject editO = editsA.getJSONObject(i);
List<String> from = null;
if (editO.has("from") && !editO.isNull("from")) {
JSONArray fromA = editO.getJSONArray("from");
int fromCount = fromA.length();
from = new ArrayList<String>(fromCount);
for (int j = 0; j < fromCount; j++) {
from.add(fromA.get(j).toString());
}
} else {
from = new ArrayList<String>();
}
boolean fromBlank = (editO.has("fromBlank") && editO.getBoolean("fromBlank") || from.get(0).length() == 0 && from.size() == 1);
boolean fromError = editO.has("fromError") && editO.getBoolean("fromError");
Serializable to = (Serializable) editO.get("to");
if (editO.has("type")) {
String type = editO.getString("type");
if ("date".equals(type)) {
to = ParsingUtilities.stringToDate((String) to);
}
}
edits.add(new Edit(from, fromBlank, fromError, to));
}
return edits;
return ParsingUtilities.mapper.readValue(obj.toString(), MassEditOperation.class);
}
public MassEditOperation(EngineConfig engineConfig, String columnName, String expression, List<Edit> edits) {

View File

@ -9,6 +9,8 @@ import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import static org.mockito.Mockito.mock;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.refine.model.Project;
import com.google.refine.operations.OperationRegistry;
import com.google.refine.operations.cell.MassEditOperation;
@ -42,7 +44,7 @@ public class MassOperationTests extends RefineTest {
public void testReconstructEditString() throws Exception {
editsString = "[{\"from\":[\"String\"],\"to\":\"newString\",\"type\":\"text\"}]";
editList = MassEditOperation.reconstructEdits(ParsingUtilities.evaluateJsonStringToArray(editsString));
editList = ParsingUtilities.mapper.readValue(ParsingUtilities.evaluateJsonStringToArray(editsString).toString(), new TypeReference<List<Edit>>() {});
Assert.assertEquals(editList.get(0).from.size(), 1);
Assert.assertEquals(editList.get(0).from.get(0), "String");
@ -55,7 +57,7 @@ public class MassOperationTests extends RefineTest {
public void testReconstructEditMultiString() throws Exception {
editsString = "[{\"from\":[\"String1\",\"String2\"],\"to\":\"newString\",\"type\":\"text\"}]";
editList = MassEditOperation.reconstructEdits(ParsingUtilities.evaluateJsonStringToArray(editsString));
editList = ParsingUtilities.mapper.readValue(ParsingUtilities.evaluateJsonStringToArray(editsString).toString(), new TypeReference<List<Edit>>() {});
Assert.assertEquals(editList.get(0).from.size(), 2);
Assert.assertEquals(editList.get(0).from.get(0), "String1");
@ -69,7 +71,7 @@ public class MassOperationTests extends RefineTest {
public void testReconstructEditBoolean() throws Exception {
editsString = "[{\"from\":[true],\"to\":\"newString\",\"type\":\"text\"}]";
editList = MassEditOperation.reconstructEdits(ParsingUtilities.evaluateJsonStringToArray(editsString));
editList = ParsingUtilities.mapper.readValue(ParsingUtilities.evaluateJsonStringToArray(editsString).toString(), new TypeReference<List<Edit>>() {});
Assert.assertEquals(editList.get(0).from.size(), 1);
Assert.assertEquals(editList.get(0).from.get(0), "true");
@ -82,7 +84,7 @@ public class MassOperationTests extends RefineTest {
public void testReconstructEditNumber() throws Exception {
editsString = "[{\"from\":[1],\"to\":\"newString\",\"type\":\"text\"}]";
editList = MassEditOperation.reconstructEdits(ParsingUtilities.evaluateJsonStringToArray(editsString));
editList = ParsingUtilities.mapper.readValue(ParsingUtilities.evaluateJsonStringToArray(editsString).toString(), new TypeReference<List<Edit>>() {});
Assert.assertEquals(editList.get(0).from.size(), 1);
Assert.assertEquals(editList.get(0).from.get(0), "1");
@ -95,7 +97,7 @@ public class MassOperationTests extends RefineTest {
public void testReconstructEditDate() throws Exception {
editsString = "[{\"from\":[\"2018-10-04T00:00:00Z\"],\"to\":\"newString\",\"type\":\"text\"}]";
editList = MassEditOperation.reconstructEdits(ParsingUtilities.evaluateJsonStringToArray(editsString));
editList = ParsingUtilities.mapper.readValue(ParsingUtilities.evaluateJsonStringToArray(editsString).toString(), new TypeReference<List<Edit>>() {});
Assert.assertEquals(editList.get(0).from.get(0), "2018-10-04T00:00:00Z");
Assert.assertEquals(editList.get(0).to,"newString" );
@ -107,7 +109,7 @@ public class MassOperationTests extends RefineTest {
public void testReconstructEditEmpty() throws Exception {
editsString = "[{\"from\":[\"\"],\"to\":\"newString\",\"type\":\"text\"}]";
editList = MassEditOperation.reconstructEdits(ParsingUtilities.evaluateJsonStringToArray(editsString));
editList = ParsingUtilities.mapper.readValue(ParsingUtilities.evaluateJsonStringToArray(editsString).toString(), new TypeReference<List<Edit>>() {});
Assert.assertEquals(editList.get(0).from.size(), 1);
Assert.assertEquals(editList.get(0).from.get(0), "");