Skip to content

Commit 1f3f30b

Browse files
committed
SpotifySerializer now serializes all super class fields
1 parent eb97b23 commit 1f3f30b

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

src/main/java/com/spotify/objects/SpotifySerializer.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,28 @@
99
import java.lang.reflect.Constructor;
1010
import java.lang.reflect.Field;
1111
import java.lang.reflect.InvocationTargetException;
12+
import java.util.ArrayList;
13+
import java.util.Arrays;
14+
import java.util.Collections;
15+
import java.util.List;
1216

1317
public abstract class SpotifySerializer {
1418

19+
20+
/**
21+
* Recursively gets all fields from current class to highest in hierarchy
22+
*/
23+
private List<Field> getAllFields(Class<?> cls){
24+
Class<?> supperCls = cls.getSuperclass();
25+
if (supperCls == null) return new ArrayList<>();
26+
27+
List<Field> fields = this.getAllFields(supperCls);
28+
fields.addAll(Arrays.asList(cls.getDeclaredFields()));
29+
30+
return fields;
31+
}
32+
33+
1534
@SuppressWarnings({"unchecked", "rawtypes"})
1635
private <E extends Serializable> E serializer(Class<E> cls, JSONObject json) {
1736
try {
@@ -25,7 +44,9 @@ private <E extends Serializable> E serializer(Class<E> cls, JSONObject json) {
2544
boolean clsRequired = notRequiredCls != null;
2645

2746

28-
for (Field field : cls.getDeclaredFields()) {
47+
List<Field> allFields = this.getAllFields(cls);
48+
for (Field field : allFields) {
49+
2950
SpotifyField spotifyField = field.getAnnotation(SpotifyField.class);
3051
// if not field not annotated ignore
3152
if (spotifyField == null) continue;

0 commit comments

Comments
 (0)