Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add jsonReader and jsonWriter functions for constructing readers and writers for case classes #246

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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 @@ -24,18 +24,11 @@ trait ProductFormatsInstances { self: ProductFormats with StandardFormats =>
jsonFormat(construct, [#p1#])
}
def jsonFormat[[#P1 :JF#], T <: Product](construct: ([#P1#]) => T, [#fieldName1: String#]): RootJsonFormat[T] = new RootJsonFormat[T]{
def write(p: T) = {
val fields = new collection.mutable.ListBuffer[(String, JsValue)]
fields.sizeHint(1 * 2)
[#fields ++= productElement##2Field[P1](fieldName1, p, 0)#
]
JsObject(fields: _*)
}
def read(value: JsValue) = {
[#val p1V = fromField[P1](value, fieldName1)#
]
construct([#p1V#])
}
val writer = jsonWriter(construct, [#fieldName1: String#])
val reader = jsonReader(construct, [#fieldName1: String#])

def write(p: T) = writer.write(p)
def read(value: JsValue) = reader.read(value)
}#


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2011,2012 Mathias Doenitz, Johannes Rudolph
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package spray.json

trait ProductReadersInstances { self: ProductFormats with StandardFormats =>
[# // Case classes with 1 parameters

def jsonReader1[[#P1 :JsonReader#], T <: Product :ClassManifest](construct: ([#P1#]) => T): RootJsonReader[T] = {
val Array([#p1#]) = extractFieldNames(classManifest[T])
jsonReader(construct, [#p1#])
}
def jsonReader[[#P1 :JsonReader#], T <: Product](construct: ([#P1#]) => T, [#fieldName1: String#]): RootJsonReader[T] = new RootJsonReader[T]{
def read(value: JsValue) = {
[#val p1V = fromField[P1](value, fieldName1)#
]
construct([#p1V#])
}
}#


]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2011,2012 Mathias Doenitz, Johannes Rudolph
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package spray.json

trait ProductWritersInstances { self: ProductFormats with StandardFormats =>
[# // Case classes with 1 parameters

def jsonWriter1[[#P1 :JsonWriter#], T <: Product :ClassManifest](construct: ([#P1#]) => T): RootJsonWriter[T] = {
val Array([#p1#]) = extractFieldNames(classManifest[T])
jsonWriter(construct, [#p1#])
}
def jsonWriter[[#P1 :JsonWriter#], T <: Product](construct: ([#P1#]) => T, [#fieldName1: String#]): RootJsonWriter[T] = new RootJsonWriter[T]{
def write(p: T) = {
val fields = new collection.mutable.ListBuffer[(String, JsValue)]
fields.sizeHint(1 * 2)
[#fields ++= productElement##2Field[P1](fieldName1, p, 0)#
]
JsObject(fields: _*)
}
}#


]
}
2 changes: 1 addition & 1 deletion src/main/scala/spray/json/ProductFormats.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import scala.util.control.NonFatal
* Provides the helpers for constructing custom JsonFormat implementations for types implementing the Product trait
* (especially case classes)
*/
trait ProductFormats extends ProductFormatsInstances {
trait ProductFormats extends ProductFormatsInstances with ProductWritersInstances with ProductReadersInstances {
this: StandardFormats =>

def jsonFormat0[T](construct: () => T): RootJsonFormat[T] =
Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/spray/json/AdditionalFormatsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class AdditionalFormatsSpec extends Specification {
implicit def containerReader[T :JsonFormat] = lift {
new JsonReader[Container[T]] {
def read(value: JsValue) = value match {
case JsObject(fields) if fields.contains("content") => Container(Some(jsonReader[T].read(fields("content"))))
case JsObject(fields) if fields.contains("content") => Container(Some(spray.json.jsonReader[T].read(fields("content"))))
case _ => deserializationError("Unexpected format: " + value.toString)
}
}
Expand Down