Help: Deserialize Json to class that takes Map as param to its constructor

26 views
Skip to first unread message

Noor Rayni

unread,
Apr 23, 2016, 9:56:17 PM4/23/16
to Play framework dev

Hi guys,

I would appreciate if someone could help me fix the "Module [modules.StoreModule] cannot be instantiated." that Play keeps raising. 

StoreModule extends AbstractModule and I am using it to inject Stores (see implementation below) to my ApplicationController.

Stores constructor takes as argument a Map[String, Store]. I have enabled the module in application.conf (play.modules.enabled += "modules.StoreModule") but it is just not working. Please help me.

/************* JSON (stores.json) ***************/

{

"clothing": {

"title": "clothing",

"subtitle": "",

"coverImg": "",

"msg": "",

"products": [],

"sections": [

{

"title": "WOMEN",

"bullets": [ "Dresses", "Blouses & Shirts", "Swimwear" ]

},

{

"title": "WOMEN",

"bullets": [ "Dresses", "Blouses & Shirts", "Swimwear" ]

},

{

"title": "FAMILY",

"bullets": [ "Dresses", "Blouses & Shirts", "Swimwear" ]

}

]

},

"other": {

"title": "clothing",

"subtitle": "",

"coverImg": "",

"msg": "",

"products": [],

"sections": [

{

"title": "WOMEN",

"bullets": [ "Dresses", "Blouses & Shirts", "Swimwear" ]

},

{

"title": "WOMEN",

"bullets": [ "Dresses", "Blouses & Shirts", "Swimwear" ]

},

{

"title": "FAMILY",

"bullets": [ "Dresses", "Blouses & Shirts", "Swimwear" ]

}

]

},

}

/*************  DATA MODEL ****************/

case class Product(title: String, subtitle: String, price: String, img: String)
case class Section(title: String, bullets: Seq[String])
case class Store(title: String, subtitle: String, coverImg: String, msg: String, products: Seq[Product], sections: Seq[Section])
case class Stores(stores: collection.immutable.Map[String, Store])

/*****************  MODULE  **************/

@Singleton

class StoreProvider @Inject() (environment: Environment) extends Provider[Stores] {
private lazy val stores: Stores = {
    val rsOpt
= environment.resourceAsStream("stores.json")
    val
is = rsOpt match {
     
case Some(value) => value
     
case None => throw new RuntimeException("")
   
}
   
Stores(JsonUtil.toMap[Store](IOUtils.toString(is, "UTF-8"))) // JsonUtils is Util I wrote
 
}
 
override def get(): Stores = stores
}


class StoreModule extends AbstractModule {

override def configure(): Unit = {

    bind
(classOf[Stores]).toProvider(classOf[StoreProvider])

 
}
}


Reply all
Reply to author
Forward
0 new messages