Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Ignore non-parameter fields when serializing case classes.

  • Loading branch information...
commit 207bfeeb8ec5ac977bf96bb96e30b62e70b69d82 1 parent 4787fe7
yenne authored
View
2  src/main/scala/com/codahale/jerkson/ScalaModule.scala
@@ -12,6 +12,6 @@ class ScalaModule(classLoader: ClassLoader) extends Module {
def setupModule(context: SetupContext) {
context.addDeserializers(new ScalaDeserializers(classLoader))
- context.addSerializers(new ScalaSerializers)
+ context.addSerializers(new ScalaSerializers(classLoader))
}
}
View
11 src/main/scala/com/codahale/jerkson/ser/CaseClassSerializer.scala
@@ -1,17 +1,22 @@
package com.codahale.jerkson.ser
import org.codehaus.jackson.JsonGenerator
-import org.codehaus.jackson.map.{SerializerProvider, JsonSerializer}
import org.codehaus.jackson.annotate.JsonIgnore
import org.codehaus.jackson.map.annotate.JsonCachable
import java.lang.reflect.Modifier
+import com.codahale.jerkson.util.CaseClassSigParser
+import org.codehaus.jackson.map.{SerializationConfig, SerializerProvider, JsonSerializer}
@JsonCachable
-class CaseClassSerializer[A <: Product](klass: Class[_]) extends JsonSerializer[A] {
+class CaseClassSerializer[A <: Product](config: SerializationConfig,
+ klass: Class[_],
+ classLoader: ClassLoader) extends JsonSerializer[A] {
+ private val params = CaseClassSigParser.parse(klass, config.getTypeFactory, classLoader).map(_._1).toArray
private val nonIgnoredFields = klass.getDeclaredFields.filterNot { f =>
f.getAnnotation(classOf[JsonIgnore]) != null ||
(f.getModifiers & Modifier.TRANSIENT) != 0 ||
- f.getName.contains("$")
+ f.getName.contains("$") ||
+ !params.contains(f.getName)
}
private val methods = klass.getDeclaredMethods
View
4 src/main/scala/com/codahale/jerkson/ser/ScalaSerializers.scala
@@ -4,7 +4,7 @@ import org.codehaus.jackson.`type`.JavaType
import com.codahale.jerkson.AST.JValue
import org.codehaus.jackson.map._
-class ScalaSerializers extends Serializers.None {
+class ScalaSerializers(classLoader: ClassLoader) extends Serializers.None {
override def findSerializer(config: SerializationConfig, javaType: JavaType, beanDesc: BeanDescription, beanProp: BeanProperty) = {
val ser: Object = if (classOf[Option[_]].isAssignableFrom(beanDesc.getBeanClass)) {
new OptionSerializer
@@ -23,7 +23,7 @@ class ScalaSerializers extends Serializers.None {
} else if (classOf[Either[_,_]].isAssignableFrom(beanDesc.getBeanClass)) {
new EitherSerializer
} else if (classOf[Product].isAssignableFrom(beanDesc.getBeanClass)) {
- new CaseClassSerializer(beanDesc.getBeanClass)
+ new CaseClassSerializer(config, beanDesc.getBeanClass, classLoader)
} else {
null
}
View
4 src/test/scala/com/codahale/jerkson/tests/CaseClassSupportSpec.scala
@@ -28,8 +28,8 @@ class CaseClassSupportSpec extends Spec {
}
class `A case class with lazy fields` {
- @test def `generates a JSON object with those fields evaluated` = {
- generate(CaseClassWithLazyVal(1)).must(be("""{"id":1,"woo":"yeah"}"""))
+ @test def `generates a JSON object with those fields ignored` = {
+ generate(CaseClassWithLazyVal(1)).must(be("""{"id":1}"""))
}
@test def `is parsable from a JSON object without those fields` = {
Please sign in to comment.
Something went wrong with that request. Please try again.