Skip to content

Commit c43db81

Browse files
committed
Merge pull request #26 from wjobin/fix_pojo
Thanks.
2 parents 5e47b52 + bfb4fa5 commit c43db81

File tree

1 file changed

+28
-25
lines changed

1 file changed

+28
-25
lines changed

src/com/microsoft/azure/documentdb/JsonSerializable.java

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -302,16 +302,8 @@ public <T extends Object> T getObject(String propertyName, Class<T> c) {
302302
"Failed to instantiate class object.", e);
303303
}
304304
} else {
305-
// POJO.
306-
if (c.isAnonymousClass() || c.isLocalClass()) {
307-
throw new IllegalArgumentException(
308-
String.format("%s can't be an anonymous or local class.", c.getName()));
309-
}
310-
311-
if (c.isMemberClass() && !Modifier.isStatic(c.getModifiers())) {
312-
throw new IllegalArgumentException(
313-
String.format("%s must be static if it's a member class.", c.getName()));
314-
}
305+
// POJO
306+
checkForValidPOJO(c);
315307

316308
try {
317309
return new ObjectMapper().readValue(jsonObj.toString(), c);
@@ -325,6 +317,16 @@ public <T extends Object> T getObject(String propertyName, Class<T> c) {
325317
return null;
326318
}
327319

320+
private static void checkForValidPOJO(Class<?> c){
321+
if (c.isAnonymousClass() || c.isLocalClass()) {
322+
throw new IllegalArgumentException(
323+
String.format("%s can't be an anonymous or local class.", c.getName()));
324+
}
325+
if (c.isMemberClass() && !Modifier.isStatic(c.getModifiers())) {
326+
throw new IllegalArgumentException(
327+
String.format("%s must be static if it's a member class.", c.getName()));
328+
}
329+
}
328330
/**
329331
* Gets an object collection.
330332
*
@@ -339,13 +341,25 @@ public <T extends Object> Collection<T> getCollection(String propertyName, Class
339341
JSONArray jsonArray = this.propertyBag.getJSONArray(propertyName);
340342
Collection<T> result = new ArrayList<T>();
341343
ObjectMapper mapper = null;
344+
boolean isBaseClass = false;
345+
boolean isJsonSerializable = false;
346+
347+
// Check once.
348+
if (Number.class.isAssignableFrom(c) || String.class.isAssignableFrom(c) ||
349+
Boolean.class.isAssignableFrom(c)) {
350+
isBaseClass = true;
351+
} else if (JsonSerializable.class.isAssignableFrom(c)) {
352+
isJsonSerializable = true;
353+
} else {
354+
checkForValidPOJO(c);
355+
mapper = new ObjectMapper();
356+
}
342357

343358
for (int i = 0; i < jsonArray.length(); i++) {
344-
if (Number.class.isAssignableFrom(c) || String.class.isAssignableFrom(c) ||
345-
Boolean.class.isAssignableFrom(c)) {
359+
if (isBaseClass) {
346360
// Number, String, Boolean
347361
result.add(c.cast(jsonArray.get(i)));
348-
} else if (JsonSerializable.class.isAssignableFrom(c)) {
362+
} else if (isJsonSerializable) {
349363
JSONObject jsonObject = jsonArray.getJSONObject(i);
350364
// JsonSerializable
351365
try {
@@ -358,14 +372,6 @@ public <T extends Object> Collection<T> getCollection(String propertyName, Class
358372
} else {
359373
JSONObject jsonObject = jsonArray.getJSONObject(i);
360374
// POJO
361-
if (mapper == null) {
362-
mapper = new ObjectMapper();
363-
// Checks once.
364-
if (!c.isMemberClass() || !Modifier.isStatic(c.getModifiers())) {
365-
throw new IllegalArgumentException(
366-
"c must be a member (not an anonymous or local) and static class.");
367-
}
368-
}
369375
try {
370376
result.add(mapper.readValue(jsonObject.toString(), c));
371377
} catch (IOException e) {
@@ -440,10 +446,7 @@ public <T extends Object> T toObject(Class<T> c) {
440446
return c.cast(this.propertyBag);
441447
} else {
442448
// POJO
443-
if (!c.isMemberClass() || !Modifier.isStatic(c.getModifiers())) {
444-
throw new IllegalArgumentException(
445-
"c must be a member (not an anonymous or local) and static class.");
446-
}
449+
checkForValidPOJO(c);
447450
try {
448451
return new ObjectMapper().readValue(this.toString(), c);
449452
} catch (IOException e) {

0 commit comments

Comments
 (0)