Merge pull request #2070 from OpenRefine/issue-2068-duplicate-json-key

Remove duplicate JSON keys.
This commit is contained in:
Antonin Delpeuch 2019-07-02 10:19:16 +02:00 committed by GitHub
commit cde59a0dca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 24 additions and 9 deletions

View File

@ -26,7 +26,7 @@
******************************************************************************/ ******************************************************************************/
package com.google.refine.browsing.facets; package com.google.refine.browsing.facets;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonSubTypes; import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type; import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.JsonTypeInfo;
@ -63,6 +63,6 @@ public interface FacetConfig {
/** /**
* The facet type as stored in json. * The facet type as stored in json.
*/ */
@JsonProperty("type") @JsonIgnore // already included by @JsonTypeInfo
public String getJsonType(); public String getJsonType();
} }

View File

@ -26,6 +26,7 @@
******************************************************************************/ ******************************************************************************/
package com.google.refine.clustering; package com.google.refine.clustering;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes; import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type; import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
@ -70,6 +71,6 @@ public abstract class ClustererConfig {
/** /**
* Type string used in Json serialization * Type string used in Json serialization
*/ */
@JsonProperty("type") @JsonIgnore // already added by @JsonTypeInfo
public abstract String getType(); public abstract String getType();
} }

View File

@ -35,6 +35,7 @@ package com.google.refine.model;
import java.util.Properties; import java.util.Properties;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver; import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver;
@ -72,7 +73,7 @@ abstract public class AbstractOperation {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@JsonProperty("op") @JsonIgnore // the operation id is already added as "op" by the JsonTypeInfo annotation
public String getOperationId() { public String getOperationId() {
return OperationRegistry.s_opClassToName.get(this.getClass()); return OperationRegistry.s_opClassToName.get(this.getClass());
} }

View File

@ -26,8 +26,8 @@
******************************************************************************/ ******************************************************************************/
package com.google.refine.preference; package com.google.refine.preference;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonProperty;
/** /**
* Interface to be extended by all objects stored * Interface to be extended by all objects stored
@ -40,7 +40,7 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo;
@JsonTypeInfo( @JsonTypeInfo(
use=JsonTypeInfo.Id.CLASS, use=JsonTypeInfo.Id.CLASS,
include=JsonTypeInfo.As.PROPERTY, include=JsonTypeInfo.As.EXISTING_PROPERTY,
property="class") property="class")
public interface PreferenceValue { public interface PreferenceValue {

View File

@ -70,7 +70,7 @@ abstract public class Criterion {
@JsonProperty("reverse") @JsonProperty("reverse")
public boolean reverse = false; public boolean reverse = false;
@JsonProperty("valueType") @JsonIgnore // already added by @JsonTypeInfo
public abstract String getValueType(); public abstract String getValueType();
// Returns a cached cell index // Returns a cached cell index

View File

@ -27,11 +27,13 @@
package com.google.refine.tests.preference; package com.google.refine.tests.preference;
import java.io.IOException; import java.io.IOException;
import java.util.Collections;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonMappingException;
import com.google.refine.preference.PreferenceValue;
import com.google.refine.preference.TopList; import com.google.refine.preference.TopList;
import com.google.refine.tests.util.TestUtils; import com.google.refine.tests.util.TestUtils;
import com.google.refine.util.ParsingUtilities; import com.google.refine.util.ParsingUtilities;
@ -50,8 +52,12 @@ public class TopListTests {
+ " \"grel:value.parseJson()[\\\"employment-summary\\\"].join('###')\"," + " \"grel:value.parseJson()[\\\"employment-summary\\\"].join('###')\","
+ " \"grel:\\\"https://pub.orcid.org/\\\"+value+\\\"/employments\\\"\"" + " \"grel:\\\"https://pub.orcid.org/\\\"+value+\\\"/employments\\\"\""
+ "]}"; + "]}";
PreferenceValue prefValue = ParsingUtilities.mapper.readValue(json, PreferenceValue.class);
TestUtils.isSerializedTo( TestUtils.isSerializedTo(
ParsingUtilities.mapper.readValue(json, TopList.class), prefValue,
json); json);
String mapJson = "{\"key\":"+json+"}";
TestUtils.isSerializedTo(Collections.singletonMap("key",prefValue), mapJson);
} }
} }

View File

@ -35,8 +35,11 @@ import java.io.LineNumberReader;
import java.io.StringReader; import java.io.StringReader;
import java.io.StringWriter; import java.io.StringWriter;
import com.fasterxml.jackson.core.JsonFactory.Feature;
import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.MapperFeature; import com.fasterxml.jackson.databind.MapperFeature;
@ -50,7 +53,11 @@ public class TestUtils {
static ObjectMapper mapper = new ObjectMapper(); static ObjectMapper mapper = new ObjectMapper();
static { static {
mapper = mapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true); mapper = mapper.enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS);
// Reject duplicate fields because the previous implementation with org.json
// does not accept them. It is also cleaner and more compact.
mapper = mapper.enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION);
mapper = mapper.enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY);
} }
/** /**