Skip to content

Commit 4f6ee3b

Browse files
author
AdrianTodt
authored
Merge pull request #9 from rethinkdb/fix/use-plain-jackson
Use plain Jackson to get effectively the same deserialization results.
2 parents e202ff4 + 4560eb7 commit 4f6ee3b

File tree

2 files changed

+15
-41
lines changed

2 files changed

+15
-41
lines changed

src/main/java/com/rethinkdb/RethinkDB.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.rethinkdb;
22

3+
import com.fasterxml.jackson.databind.DeserializationFeature;
34
import com.fasterxml.jackson.databind.ObjectMapper;
45
import com.rethinkdb.gen.model.TopLevel;
56
import com.rethinkdb.net.Connection;
@@ -10,7 +11,7 @@ public class RethinkDB extends TopLevel {
1011
* The Singleton to use to begin interacting with RethinkDB Driver
1112
*/
1213
public static final RethinkDB r = new RethinkDB();
13-
private static ObjectMapper mapper = new ObjectMapper();
14+
private static ObjectMapper mapper = new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
1415
public Connection.Builder connection() {
1516
return Connection.build();
1617
}

src/main/java/com/rethinkdb/net/Util.java

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,19 @@ public static JSONObject toJSON(ByteBuffer buf) {
4545
}
4646

4747
public static <T, P> T convertToPojo(Object value, Optional<Class<P>> pojoClass) {
48-
return !pojoClass.isPresent() || !(value instanceof Map)
49-
? (T) value
50-
: (T) toPojo(pojoClass.get(), (Map<String, Object>) value);
48+
if (pojoClass.isPresent()) {
49+
if (pojoClass.get().isEnum()) {
50+
Enum<?>[] enumConstants = ((Class<Enum<?>>) pojoClass.get()).getEnumConstants();
51+
for (Enum<?> enumConst : enumConstants) {
52+
if (enumConst.name().equals(value)) {
53+
return (T) enumConst;
54+
}
55+
}
56+
} else if (value instanceof Map) {
57+
return (T) RethinkDB.getObjectMapper().convertValue(value, pojoClass.get());
58+
}
59+
}
60+
return (T) value;
5161
}
5262

5363
public static byte[] toUTF8(String s) {
@@ -57,43 +67,6 @@ public static byte[] toUTF8(String s) {
5767
public static String fromUTF8(byte[] ba) {
5868
return new String(ba, StandardCharsets.UTF_8);
5969
}
60-
61-
/**
62-
* Converts a String-to-Object map to a POJO using bean introspection.<br>
63-
* The POJO's class must be public and satisfy one of the following conditions:<br>
64-
* 1. Should have a public parameterless constructor and public setters for all properties
65-
* in the map. Properties with no corresponding entries in the map would have default values<br>
66-
* 2. Should have a public constructor with parameters matching the contents of the map
67-
* either by names and value types. Names of parameters are only available since Java 8
68-
* and only in case <code>javac</code> is run with <code>-parameters</code> argument.<br>
69-
* If the POJO's class doesn't satisfy the conditions, a ReqlDriverError is thrown.
70-
* @param <T> POJO's type
71-
* @param pojoClass POJO's class to be instantiated
72-
* @param map Map to be converted
73-
* @return Instantiated POJO
74-
*/
75-
@SuppressWarnings("unchecked")
76-
private static <T> T toPojo(Class<T> pojoClass, Map<String, Object> map) {
77-
// Jackson will throw an error if the POJO is not annotated with an ignore
78-
// annotation and the server gives a value that is not a field within the POJO To
79-
// prevent this, we get a list of all the field names from the class, and iterate
80-
// through the map. If the map contains a key that the does not correlate to a
81-
// field name, then that entry from the map is removed and we log an error.
82-
List<String> nameFields = new ArrayList<>();
83-
Arrays.asList(pojoClass.getDeclaredFields()).forEach(field -> nameFields.add(field.getName()));
84-
List<String> toRemove = new ArrayList<>();
85-
86-
map.keySet().forEach(s -> {
87-
if (!nameFields.contains(s))
88-
{
89-
log.error("Got JSON field [" + s + "] from server. POJO does not contain field, removing from map!");
90-
toRemove.add(s);
91-
}
92-
});
93-
toRemove.forEach(map::remove);
94-
95-
return RethinkDB.getObjectMapper().convertValue(map, pojoClass);
96-
}
9770
}
9871

9972

0 commit comments

Comments
 (0)