mirror of
https://github.com/plexusorg/toml4j.git
synced 2024-12-28 19:24:15 +00:00
4 -> 2 spaces
This commit is contained in:
parent
4ed3740732
commit
1216a009cb
1 changed files with 55 additions and 55 deletions
|
@ -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<String, Object> to = new LinkedHashMap<String, Object>();
|
||||
Set<Field> fields = getFieldsForClass(value.getClass());
|
||||
for (Field field : fields) {
|
||||
to.put(field.getName(), getFieldValue(field, value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(Object value, SerializerContext context) {
|
||||
Map<String, Object> to = new LinkedHashMap<String, Object>();
|
||||
Set<Field> 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<Field> getFieldsForClass(Class cls) {
|
||||
Set<Field> fields = new LinkedHashSet<Field>(Arrays.asList(cls.getDeclaredFields()));
|
||||
|
||||
getSuperClassFields(cls.getSuperclass(), fields);
|
||||
|
||||
// Skip final fields
|
||||
Set<Field> prunedFields = new LinkedHashSet<Field>();
|
||||
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<Field> 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<Field> getFieldsForClass(Class cls) {
|
||||
Set<Field> fields = new LinkedHashSet<Field>(Arrays.asList(cls.getDeclaredFields()));
|
||||
|
||||
getSuperClassFields(cls.getSuperclass(), fields);
|
||||
|
||||
// Skip final fields
|
||||
Set<Field> prunedFields = new LinkedHashSet<Field>();
|
||||
for (Field field : fields) {
|
||||
if (!Modifier.isFinal(field.getModifiers())) {
|
||||
prunedFields.add(field);
|
||||
}
|
||||
}
|
||||
|
||||
return prunedFields;
|
||||
}
|
||||
|
||||
static private void getSuperClassFields(Class cls, Set<Field> 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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue