diff options
author | Sam Halliday <sam.halliday@Gmail.com> | 2015-01-27 09:16:25 +0000 |
---|---|---|
committer | Sam Halliday <sam.halliday@Gmail.com> | 2015-01-27 09:16:25 +0000 |
commit | 13bb8082f681a8fb55c4de1651caa461f7f6c38b (patch) | |
tree | f7dfdf717f56e09cdc2a6e19947aa1976cf0e783 /src | |
parent | a53a5a895e462a4a83f88be06a5eb36a21f2efb0 (diff) | |
download | spray-json-13bb8082f681a8fb55c4de1651caa461f7f6c38b.tar.gz spray-json-13bb8082f681a8fb55c4de1651caa461f7f6c38b.tar.bz2 spray-json-13bb8082f681a8fb55c4de1651caa461f7f6c38b.zip |
close #132
Diffstat (limited to 'src')
-rw-r--r-- | src/main/scala/spray/json/ProductFormats.scala | 29 |
1 files changed, 13 insertions, 16 deletions
diff --git a/src/main/scala/spray/json/ProductFormats.scala b/src/main/scala/spray/json/ProductFormats.scala index 39d5300..9914b55 100644 --- a/src/main/scala/spray/json/ProductFormats.scala +++ b/src/main/scala/spray/json/ProductFormats.scala @@ -46,22 +46,19 @@ trait ProductFormats extends ProductFormatsInstances { } } - protected def fromField[T](value: JsValue, fieldName: String)(implicit reader: JsonReader[T]) = { - value match { - case x: JsObject => - var fieldFound = false - try { - val fieldValue = x.fields(fieldName) - fieldFound = true - reader.read(fieldValue) - } - catch { - case e: NoSuchElementException if !fieldFound => - if (reader.isInstanceOf[OptionFormat[_]]) None.asInstanceOf[T] - else deserializationError("Object is missing required member '" + fieldName + "'", e) - } - case _ => deserializationError("Object expected in field '" + fieldName + "'") - } + protected def fromField[T](value: JsValue, fieldName: String) + (implicit reader: JsonReader[T]) = value match { + case x: JsObject if + (reader.isInstanceOf[OptionFormat[_]] & + !x.fields.contains(fieldName)) => + None.asInstanceOf[T] + case x: JsObject => + try reader.read(x.fields(fieldName)) + catch { + case e: NoSuchElementException => + deserializationError("Object is missing required member '" + fieldName + "'", e) + } + case _ => deserializationError("Object expected in field '" + fieldName + "'") } protected def extractFieldNames(classManifest: ClassManifest[_]): Array[String] = { |