简体   繁体   中英

parse json string to a scala object

I use play framework version 2.2.1 with scala.

I have a large json-string that i wish to construct an object from.
I have written the formatters for the classes.

My question is what are the steps i am supposed to cast that string to concrete class.

eg of code that i've tried:

val something = scala.util.parsing.json.JSON.parseFull(jsonContent).asInstanceOf[HsmTenant] //This has stucked my debug session and operation never completes

Or

val concreteClass = Json.parse(jsonContent).asInstanceOf[T] //message: "Error processing request - Failed to migrate, Exception=play.api.libs.json.JsObject cannot be cast to migration.hsm.HsmTenant"

When trying:

Json.parse(jsonContent).as[T]

I get the following:

 Multiple markers at this line
- No Json deserializer found for type migration.hsm.HsmTenant. Try to implement an implicit Reads or Format for this type.
- not enough arguments for method as: (implicit fjs: play.api.libs.json.Reads[migration.hsm.HsmTenant])migration.hsm.HsmTenant. Unspecified value 
 parameter fjs.
- No Json deserializer found for type migration.hsm.HsmTenant. Try to implement an implicit Reads or Format for this type.
- not enough arguments for method as: (implicit fjs: play.api.libs.json.Reads[migration.hsm.HsmTenant])migration.hsm.HsmTenant. Unspecified value 
 parameter fjs.
- Line breakpoint:HsmParser [line: 16] - parse(jsonContent: String): migration.hsm.HsmTenant

My read and writes looks like: and they compile:

trait HsmFormats {

implicit val hsmRetailerFormat = Json.format[Retailer]
implicit val hsmproductFormat = new Format[HsmProduct]
{
def writes(item: HsmProduct): JsValue = 
{
  val retailers = item.r.getOrElse(List[Retailer]())
  val images = item.images.getOrElse(List[String]())
  Json.obj(
    "u" -> item.u,
    "s" -> item.s,
    "z" -> item.z,
    "n" -> item.n,
    "v" -> item.v,
    "vu" -> item.vu,
    "t" -> item.t,
    "r" -> retailers,
    "images" -> images
  )
}

def reads(json: JsValue): JsResult[HsmProduct] = 
{
  val retailers = (json \ "r").as[Option[List[Retailer]]]
  var retaliersReal:Option[List[Retailer]] = None
  if (retailers.isDefined)
  {
      retaliersReal = Some(retailers.get)
  }

  val images = (json \ "images").as[Option[List[String]]]
  var imagesReal:Option[List[String]] = None
  if (images.isDefined)
  {
      imagesReal = Some(images.get)
  }      
JsSuccess(new HsmProduct(
  (json \ "u").as[String],
  (json \ "s").as[Int],
  (json \ "z").as[Int],
  (json \ "n").as[String],
  (json \ "v").as[String],
  (json \ "vu").as[String],
  (json \ "t").as[String],
  retailers,
  imagesReal
))
}

}

implicit val hsmTenantFormat = new Format[HsmTenant]
{
def writes(tenant: HsmTenant): JsValue = 
{
  val items = tenant.items.getOrElse(List[HsmProduct]())
  Json.obj(
    "items" -> tenant.items,
    "prefixAndroid" -> tenant.prefixAndroid,
    "prefixIOS" -> tenant.prefixIOS,
    "er" -> tenant.er,
    "erMessage" -> tenant.erMessage

  )
}

def reads(json: JsValue): JsResult[HsmTenant] = 
{
  val items = (json \ "items").as[Option[List[HsmProduct]]]
  var itemsReal:Option[List[HsmProduct]] = None
  if (items.isDefined)
  {
      itemsReal = Some(items.get)
  }  
JsSuccess(new HsmTenant(
  itemsReal,
  (json \ "prefixAndroid").as[String],
  (json \ "prefixIOS").as[String],
  (json \ "er").as[Int], 
  (json \ "erMessage").as[String]
))
}

}

This should work if your Format s are correct:

import play.api.libs.json.Json

Json.parse(jsonContent).as[T]

What was the problem ?
It was not issue if import the Formats class, my format is a trait , so i must extend that trait in classes i use it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM