Github user tweise commented on a diff in the pull request:
https://github.com/apache/incubator-apex-core/pull/250#discussion_r57507058
--- Diff: api/src/main/java/com/datatorrent/api/StringCodec.java ---
@@ -377,4 +383,43 @@ public String toString(Class<? extends T> clazz)
private static final long serialVersionUID = 201312082053L;
}
+ public class JsonStringCodec<T> implements StringCodec<T>, Serializable
+ {
+ private static final long serialVersionUID = 2513932518264776006L;
+ Class<?> clazz;
+
+ public JsonStringCodec(Class<T> clazz)
+ {
+ this.clazz = clazz;
+ }
+
+ @Override
+ public T fromString(String string)
+ {
+ try {
+ ObjectMapper mapper = new ObjectMapper();
+
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,
false);
+ ObjectReader reader = mapper.reader(clazz);
+ return reader.readValue(string);
+ } catch (IOException e) {
+ DTThrowable.wrapIfChecked(e);
+ }
+ return null;
+ }
+
+ @Override
+ public String toString(T pojo)
+ {
+ try {
+ ObjectMapper mapper = new ObjectMapper();
+
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,
false);
+ ObjectWriter writer = mapper.writer();
+ return writer.writeValueAsString(pojo);
+ } catch (IOException e) {
+ DTThrowable.wrapIfChecked(e);
--- End diff --
This is not specific to this PR but in general, should we consider using
Throwables.propagate() instead?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---