From 57f6af3a95508c3a3661861e1d8dc5c82e047d13 Mon Sep 17 00:00:00 2001 From: Kevin Scaldeferri Date: Tue, 17 Apr 2012 16:30:04 -0700 Subject: [PATCH] Simplify fieldName logic It's impossible for method.getName and field.getName to be different. Simplify the code so people don't think there's some obscure corner case being dealt with. --- .../com/codahale/jerkson/ser/CaseClassSerializer.scala | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/scala/com/codahale/jerkson/ser/CaseClassSerializer.scala b/src/main/scala/com/codahale/jerkson/ser/CaseClassSerializer.scala index 6e1309b..341795a 100644 --- a/src/main/scala/com/codahale/jerkson/ser/CaseClassSerializer.scala +++ b/src/main/scala/com/codahale/jerkson/ser/CaseClassSerializer.scala @@ -14,7 +14,7 @@ class CaseClassSerializer[A <: Product](klass: Class[_]) extends JsonSerializer[ private val ignoredFields = if (klass.isAnnotationPresent(classOf[JsonIgnoreProperties])) { klass.getAnnotation(classOf[JsonIgnoreProperties]).value().toSet } else Set.empty[String] - + private val nonIgnoredFields = klass.getDeclaredFields.filterNot { f => f.getAnnotation(classOf[JsonIgnore]) != null || ignoredFields(f.getName) || @@ -25,14 +25,14 @@ class CaseClassSerializer[A <: Product](klass: Class[_]) extends JsonSerializer[ private val methods = klass.getDeclaredMethods .filter { _.getParameterTypes.isEmpty } .map { m => m.getName -> m }.toMap - + def serialize(value: A, json: JsonGenerator, provider: SerializerProvider) { json.writeStartObject() for (field <- nonIgnoredFields) { - val methodOpt = methods.get(field.getName) + val fieldName = field.getName + val methodOpt = methods.get(fieldName) val fieldValue: Object = methodOpt.map { _.invoke(value) }.getOrElse(field.get(value)) if (fieldValue != None) { - val fieldName = methodOpt.map { _.getName }.getOrElse(field.getName) provider.defaultSerializeField(if (isSnakeCase) snakeCase(fieldName) else fieldName, fieldValue, json) } }