Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ trait ProductFormatsInstances { self: ProductFormats with StandardFormats =>
JsObject(fields: _*)
}
def read(value: JsValue) = {
val fields = getJsValueFields(value)
if (fields.size != 1) {
deserializationError("Number of fields in JSON does not equal to target class")
}
[#val p1V = fromField[P1](value, fieldName1)#
]
construct([#p1V#])
Expand Down
12 changes: 11 additions & 1 deletion src/main/scala/spray/json/ProductFormats.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ trait ProductFormats extends ProductFormatsInstances {
new RootJsonFormat[T] {
def write(p: T) = JsObject()
def read(value: JsValue) = value match {
case JsObject(_) => construct()
case JsObject(fields) =>
if (fields.size > 0) {
throw new DeserializationException(s"Redundant fields: ${fields.keys.mkString(",")}")
}
construct()
case _ => throw new DeserializationException("Object expected")
}
}
Expand Down Expand Up @@ -65,6 +69,12 @@ trait ProductFormats extends ProductFormatsInstances {
case _ => deserializationError("Object expected in field '" + fieldName + "'", fieldNames = fieldName :: Nil)
}

protected def getJsValueFields(value: JsValue) = value match {
case JsObject(fields) => fields
case _ => deserializationError("object expected!")

}

protected def extractFieldNames(tag: ClassTag[_]): Array[String] = {
val clazz = tag.runtimeClass
try {
Expand Down