I am in a situation where I need to serialize to JSON class without case.
The presence of a class like:
class MyClass(val name: String) { def SaySomething() : String = { return "Saying something... " } }
I created JsonProtocol for this class:
object MyClassJsonProtocol extends DefaultJsonProtocol { implicit object MyClassJsonFormat extends JsonWriter[MyClass] { override def write(obj: MyClass): JsValue = JsObject( "name" -> JsString(obj.name) ) } }
Later in the code I import the protocol.
val aListOfMyClasses = List[MyClass]() ... // lets assume that has items and not an empty list import spray.json._ import MyClassJsonProtocol._ val json = aListOfMyClasses.toJson
When I try to create a project, I get the following error:
Cannot find JsonWriter or JsonFormat for List class class [MyClass]
spray-json already has a format for the general list, and I am providing a format for my class, what is the problem?
Thanks in advance...!!!
source share