Skip to content

Commit

Permalink
Merge pull request #60 from hill0826/patch-1
Browse files Browse the repository at this point in the history
Remove redundant recursion for serialization (#59)
  • Loading branch information
monitorjbl committed Jul 13, 2018
2 parents cdef1cd + 2a3c478 commit 39181f1
Showing 1 changed file with 26 additions and 30 deletions.
56 changes: 26 additions & 30 deletions json-view/src/main/java/com/monitorjbl/json/JsonViewSerializer.java
Expand Up @@ -317,44 +317,40 @@ boolean writeMap(Object obj) throws IOException {
void writeObject(Object obj) throws IOException {
jgen.writeStartObject();

Class cls = obj.getClass();
while(!cls.equals(Object.class)) {
List<AccessibleProperty> fields = getAccessibleProperties(cls);
List<AccessibleProperty> fields = getAccessibleProperties(obj.getClass());

for(AccessibleProperty property : fields) {
try {
//if the field has a serializer annotation on it, serialize with it
if(fieldAllowed(property, obj.getClass())) {
Object val = readField(obj, property);
if(!valueAllowed(property, val, obj.getClass())) {
continue;
}

for(AccessibleProperty property : fields) {
try {
//if the field has a serializer annotation on it, serialize with it
if(fieldAllowed(property, obj.getClass())) {
Object val = readField(obj, property);
if(!valueAllowed(property, val, obj.getClass())) {
continue;
}
String name = getFieldName(property);
jgen.writeFieldName(name);

String name = getFieldName(property);
jgen.writeFieldName(name);

JsonSerializer fieldSerializer = annotatedWithJsonSerialize(property);
if(fieldSerializer != null) {
fieldSerializer.serialize(val, jgen, serializerProvider);
} else if(customSerializersMap != null && val != null) {
JsonSerializer<Object> serializer = customSerializersMap.get(val.getClass());
if(serializer != null) {
serializer.serialize(val, jgen, serializerProvider);
} else {
new JsonWriter(jgen, result, currentMatch, currentPath, path, property, serializerProvider).write(name, val);
}
} else if(val instanceof JsonNode) {
// Let Jackson deal with these, they're special
serializerProvider.defaultSerializeValue(val, jgen);
JsonSerializer fieldSerializer = annotatedWithJsonSerialize(property);
if(fieldSerializer != null) {
fieldSerializer.serialize(val, jgen, serializerProvider);
} else if(customSerializersMap != null && val != null) {
JsonSerializer<Object> serializer = customSerializersMap.get(val.getClass());
if(serializer != null) {
serializer.serialize(val, jgen, serializerProvider);
} else {
new JsonWriter(jgen, result, currentMatch, currentPath, path, property, serializerProvider).write(name, val);
}
} else if(val instanceof JsonNode) {
// Let Jackson deal with these, they're special
serializerProvider.defaultSerializeValue(val, jgen);
} else {
new JsonWriter(jgen, result, currentMatch, currentPath, path, property, serializerProvider).write(name, val);
}
} catch(IllegalArgumentException | IllegalAccessException e) {
throw new RuntimeException(e);
}
} catch(IllegalArgumentException | IllegalAccessException e) {
throw new RuntimeException(e);
}
cls = cls.getSuperclass();
}

jgen.writeEndObject();
Expand Down

0 comments on commit 39181f1

Please sign in to comment.