-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added recordedByID into hdfs query builder and etc.
- Loading branch information
Showing
8 changed files
with
146 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
occurrence-common/src/main/java/org/gbif/occurrence/common/json/ListStringSerDeserUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package org.gbif.occurrence.common.json; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import org.codehaus.jackson.map.DeserializationConfig; | ||
import org.codehaus.jackson.map.ObjectMapper; | ||
import org.codehaus.jackson.map.SerializationConfig; | ||
import org.codehaus.jackson.map.annotate.JsonSerialize; | ||
import org.codehaus.jackson.map.type.CollectionType; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.google.common.base.Throwables; | ||
|
||
/** | ||
* Utility class to serialize and deserialize Strings instances from/to JSON. | ||
*/ | ||
public class ListStringSerDeserUtils { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(ListStringSerDeserUtils.class); | ||
private static final String SER_ERROR_MSG = "Unable to serialize list of string objects to JSON"; | ||
private static final String DESER_ERROR_MSG = "Unable to deserialize String into list of string objects"; | ||
|
||
private static final ObjectMapper MAPPER = new ObjectMapper(); | ||
|
||
static { | ||
// Don't change this section, methods used here guarantee backwards compatibility with Jackson 1.8.8 | ||
MAPPER.configure(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true); | ||
MAPPER.configure(SerializationConfig.Feature.INDENT_OUTPUT, true); | ||
MAPPER.getSerializationConfig().setSerializationInclusion(JsonSerialize.Inclusion.ALWAYS); | ||
} | ||
|
||
private static final CollectionType LIST_STRINGS_TYPE = | ||
MAPPER.getTypeFactory().constructCollectionType(List.class, String.class); | ||
|
||
|
||
private ListStringSerDeserUtils() { | ||
// private constructor | ||
} | ||
|
||
/** | ||
* Converts the list of string objects into a JSON string. | ||
*/ | ||
public static String toJson(List<String> strings) { | ||
try { | ||
if (strings != null && !strings.isEmpty()) { | ||
return MAPPER.writeValueAsString(strings); | ||
} | ||
} catch (IOException e) { | ||
logAndRethrow(SER_ERROR_MSG, e); | ||
} | ||
return ""; | ||
} | ||
|
||
/** | ||
* Converts a Json string into a list of string objects. | ||
*/ | ||
public static List<String> fromJson(String strings) { | ||
try { | ||
return MAPPER.readValue(strings, LIST_STRINGS_TYPE); | ||
} catch (IOException e) { | ||
logAndRethrow(DESER_ERROR_MSG, e); | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Logs an error and re-throws the exception. | ||
*/ | ||
private static void logAndRethrow(String message, Throwable throwable) { | ||
LOG.error(message, throwable); | ||
Throwables.propagate(throwable); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters