Patching unmarshall to support old recordType key
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-publishing/document-store-lib@154347 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
76baf9de84
commit
c216adc12e
|
@ -17,6 +17,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
|
|||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JavaType;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
/**
|
||||
|
@ -168,15 +169,10 @@ public class DSMapper {
|
|||
mapper.registerSubtypes(classes);
|
||||
}
|
||||
|
||||
public static JsonNode asJsonNode(String jsonString) throws JsonProcessingException, IOException {
|
||||
ObjectMapper mapperJson = new ObjectMapper();
|
||||
return mapperJson.readTree(jsonString);
|
||||
}
|
||||
|
||||
public static boolean isJSONValid(String jsonInString ) {
|
||||
try {
|
||||
final ObjectMapper mapperJson = new ObjectMapper();
|
||||
mapperJson.readTree(jsonInString);
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -16,6 +16,8 @@ import org.gcube.documentstore.exception.InvalidValueException;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
|
@ -214,6 +216,8 @@ public class RecordUtility {
|
|||
|
||||
protected static final String INVALID = "invalid";
|
||||
|
||||
private static final String USAGE_RECORD_TYPE = "usageRecordType";
|
||||
|
||||
/**
|
||||
* Create a Record from a Map serialized using toString()
|
||||
* @param serializedMapOrValidJSON the String representation of Map
|
||||
|
@ -221,16 +225,20 @@ public class RecordUtility {
|
|||
* @throws Exception if deserialization fails
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <R extends Record> R getRecord(String json) throws Exception {
|
||||
public static <R extends Record> R getRecord(String jsonString) throws Exception {
|
||||
|
||||
// verify if serializedMap is a json (new serializable or old method)
|
||||
if (DSMapper.isJSONValid(json)){
|
||||
logger.trace("Unmarshal record with jackson");
|
||||
return (R) DSMapper.unmarshal(Record.class, json);
|
||||
} else {
|
||||
try {
|
||||
JsonNode jsonNode = DSMapper.asJsonNode(jsonString);
|
||||
// Patch for old records using usageRecordType instead of recordType
|
||||
if(jsonNode.has(USAGE_RECORD_TYPE)){
|
||||
jsonString = jsonString.replace(USAGE_RECORD_TYPE, Record.RECORD_TYPE);
|
||||
}
|
||||
logger.trace("Going to unmarshall {} using jackson", jsonString);
|
||||
return (R) DSMapper.unmarshal(Record.class, jsonString);
|
||||
}catch (Exception ex) {
|
||||
//old method
|
||||
logger.trace("Unmarshal record serialized as Map");
|
||||
Map<String,? extends Serializable> map = getMapFromString(json);
|
||||
logger.trace("Going to unmarshall {} as serialized Map", jsonString);
|
||||
Map<String,? extends Serializable> map = getMapFromString(jsonString);
|
||||
if (map!=null){
|
||||
Record record = getRecord(map);
|
||||
try {
|
||||
|
@ -244,7 +252,7 @@ public class RecordUtility {
|
|||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue