Permalink
Browse files

[OPENENGSB-3579] fixed missing OpenEngSBModelEntry type conversion in…

… the JSON deserializing process
  • Loading branch information...
1 parent e084712 commit ed89d70a0156107e94fbd96f9c2cac4c409ae7dd Felix Mayerhuber committed Mar 27, 2013
@@ -69,6 +69,7 @@ public void tryConvertJSONIntoModelWithoutAdditionalSerializer_shouldThrowExcept
@Test
public void tryConvertJSONIntoModelWithAdditionalSerializer_shouldThrowNoException() throws Exception {
NullModel model = createTestModel();
+ ModelUtils.addOpenEngSBModelEntry(model, new OpenEngSBModelEntry("number", 42, Integer.class));
String result = mapper.writeValueAsString(model);
NullModel other = JsonUtils.convertObject(result, NullModel.class);
@@ -80,8 +81,9 @@ public void tryConvertJSONIntoModelWithAdditionalSerializer_shouldThrowNoExcepti
assertThat(model.getSubs().get(0).getName(), is(other.getSubs().get(0).getName()));
assertThat(model.getSubs().get(1).getId(), is(other.getSubs().get(1).getId()));
assertThat(model.getSubs().get(1).getName(), is(other.getSubs().get(1).getName()));
- assertThat(entries.size(), is(2));
+ assertThat(entries.size(), is(3));
assertThat(entries.contains(new OpenEngSBModelEntry("test", "test", String.class)), is(true));
assertThat(entries.contains(new OpenEngSBModelEntry("test2", "test2", String.class)), is(true));
+ assertThat(entries.contains(new OpenEngSBModelEntry("number", 42, Integer.class)), is(true));
}
}
@@ -19,15 +19,18 @@
import java.io.IOException;
import java.lang.reflect.Array;
+import java.lang.reflect.Constructor;
import java.util.List;
+import org.apache.commons.lang.reflect.MethodUtils;
import org.openengsb.core.api.model.OpenEngSBModelEntry;
import org.openengsb.core.api.remote.MethodCall;
import org.openengsb.core.api.remote.MethodCallMessage;
import org.openengsb.core.api.remote.MethodResult;
import org.openengsb.core.api.remote.MethodResultMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.springframework.util.ClassUtils;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
@@ -143,6 +146,10 @@ public OpenEngSBModelEntry deserialize(JsonParser jp, DeserializationContext ctx
}
do {
if (token == JsonToken.END_OBJECT) {
+ Object value = createValueOfEntry(entry);
+ if (value != null) {
+ entry.setValue(value);
+ }
return entry;
} else {
if (jp.getCurrentName().equals("key")) {
@@ -162,5 +169,31 @@ public OpenEngSBModelEntry deserialize(JsonParser jp, DeserializationContext ctx
} while (token != null);
return null;
}
+
+ /**
+ * Converts the string located in the value property of the entry in the correct data format and returns it.
+ */
+ private Object createValueOfEntry(OpenEngSBModelEntry entry) {
+ if (entry.getType().equals(String.class)) {
+ return entry.getValue();
+ }
+ Object element = null;
+ if (entry.getType() == null) {
+ LOGGER.error("Unknown type for model entry with key {}", entry.getKey());
+ return element;
+ }
+ try {
+ Class<?> clazz = entry.getType();
+ Constructor<?> constr = ClassUtils.getConstructorIfAvailable(clazz, String.class);
+ if (constr != null) {
+ element = constr.newInstance(entry.getValue());
+ } else {
+ element = MethodUtils.invokeStaticMethod(clazz, "valueOf", entry.getValue());
+ }
+ } catch (Exception e) {
+ LOGGER.error("Unable to convert value with the key {} to correct type", entry.getKey());
+ }
+ return element;
+ }
}
}

0 comments on commit ed89d70

Please sign in to comment.