Skip to content

Commit 832a9e4

Browse files
Do not restrict custom deserializer use to JObject
closes #3
1 parent c9191f6 commit 832a9e4

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/main/scala/net/liftmodules/jsonextractorng/ExtractionNg.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ object Extraction {
9090
val customDeserializer = formats.customDeserializer(formats)
9191

9292
root match {
93-
case obj @ JObject(_) if formats.customSerializers.nonEmpty &&
94-
customDeserializer.isDefinedAt(ctor.typeInfo, obj) =>
93+
case obj if formats.customSerializers.nonEmpty &&
94+
customDeserializer.isDefinedAt(ctor.typeInfo, obj) =>
9595
customDeserializer(ctor.typeInfo, obj)
9696

9797
case obj @ JObject(fields) =>

src/test/scala/net/liftmodules/jsonextractorng/ExtractionNgSpec.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,27 @@ class ExtractionNgSpec extends FlatSpec with Matchers {
121121

122122
output should equal(Baconizer2[String]("Testy McTestface", 3.14))
123123
}
124+
125+
it should "properly handle a custom deserializer for a non JObject" in {
126+
val customDeserializer = new Serializer[Baconizer] {
127+
val clazz = classOf[Baconizer]
128+
override def serialize(implicit formats: Formats) = ???
129+
130+
override def deserialize(implicit formats: Formats) = {
131+
case (TypeInfo(`clazz`, None), json) =>
132+
json match {
133+
case JString(s) => Baconizer("Testy McTestface", 3.14)
134+
case x => throw new RuntimeException("Can't convert " + x + " to SomethingWithACustom")
135+
}
136+
}
137+
}
138+
139+
implicit val formats = DefaultFormats + customDeserializer
140+
val input: JString = JString("Testy McTestface")
141+
val output = input.extractNg[Baconizer]
142+
143+
output should equal(Baconizer("Testy McTestface", 3.14))
144+
}
124145
}
125146

126147
case class SimpleCaseClass(name: String, age: Int)

0 commit comments

Comments
 (0)