diff --git a/src/main/java/com/moandjiezana/toml/ObjectSerializer.java b/src/main/java/com/moandjiezana/toml/ObjectSerializer.java index 07da595..0540c38 100644 --- a/src/main/java/com/moandjiezana/toml/ObjectSerializer.java +++ b/src/main/java/com/moandjiezana/toml/ObjectSerializer.java @@ -7,69 +7,69 @@ import java.util.*; import static com.moandjiezana.toml.MapSerializer.MAP_SERIALIZER; class ObjectSerializer implements Serializer { - static final Serializer OBJECT_SERIALIZER = new ObjectSerializer(); + static final Serializer OBJECT_SERIALIZER = new ObjectSerializer(); - @Override - public boolean canSerialize(Object value) { - return true; + @Override + public boolean canSerialize(Object value) { + return true; + } + + @Override + public void serialize(Object value, SerializerContext context) { + Map to = new LinkedHashMap(); + Set fields = getFieldsForClass(value.getClass()); + for (Field field : fields) { + to.put(field.getName(), getFieldValue(field, value)); } - @Override - public void serialize(Object value, SerializerContext context) { - Map to = new LinkedHashMap(); - Set fields = getFieldsForClass(value.getClass()); - for (Field field : fields) { - to.put(field.getName(), getFieldValue(field, value)); - } + MAP_SERIALIZER.serialize(to, context); + } - MAP_SERIALIZER.serialize(to, context); + @Override + public boolean isPrimitiveType() { + return false; + } + + @Override + public boolean isTable() { + return true; + } + + static private Set getFieldsForClass(Class cls) { + Set fields = new LinkedHashSet(Arrays.asList(cls.getDeclaredFields())); + + getSuperClassFields(cls.getSuperclass(), fields); + + // Skip final fields + Set prunedFields = new LinkedHashSet(); + for (Field field : fields) { + if (!Modifier.isFinal(field.getModifiers())) { + prunedFields.add(field); + } } - @Override - public boolean isPrimitiveType() { - return false; + return prunedFields; + } + + static private void getSuperClassFields(Class cls, Set fields) { + if (cls == Object.class) { + return; } - @Override - public boolean isTable() { - return true; + fields.addAll(Arrays.asList(cls.getDeclaredFields())); + getSuperClassFields(cls.getSuperclass(), fields); + } + + static private Object getFieldValue(Field field, Object o) { + boolean isAccessible = field.isAccessible(); + field.setAccessible(true); + Object value = null; + try { + value = field.get(o); + } catch (IllegalAccessException ignored) { } + field.setAccessible(isAccessible); - static private Set getFieldsForClass(Class cls) { - Set fields = new LinkedHashSet(Arrays.asList(cls.getDeclaredFields())); - - getSuperClassFields(cls.getSuperclass(), fields); - - // Skip final fields - Set prunedFields = new LinkedHashSet(); - for (Field field : fields) { - if (!Modifier.isFinal(field.getModifiers())) { - prunedFields.add(field); - } - } - - return prunedFields; - } - - static private void getSuperClassFields(Class cls, Set fields) { - if (cls == Object.class) { - return; - } - - fields.addAll(Arrays.asList(cls.getDeclaredFields())); - getSuperClassFields(cls.getSuperclass(), fields); - } - - static private Object getFieldValue(Field field, Object o) { - boolean isAccessible = field.isAccessible(); - field.setAccessible(true); - Object value = null; - try { - value = field.get(o); - } catch (IllegalAccessException ignored) { - } - field.setAccessible(isAccessible); - - return value; - } + return value; + } }