Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON deserialization using reflection

Hi I am trying to extract a JSON using reflection

import net.liftweb.json._
case class Bike(make: String, price: Int) {
   def this(price: Int) = this("Trek", price)
}

val cls = Class.forName("Bike")
val manifest = Manifest.classType(cls)
val parsedData =net.liftweb.json.JsonParser.parse(json)

JsonParser.parse(""" {"price":350} """).extract[manifest]

however I am getting this error:

not found: type manifest
  JsonParser.parse(""" {"price":350} """).extract[manifest]
                                                   ^

although manifest is from type Manifest

like image 884
igx Avatar asked Jun 08 '26 21:06

igx


1 Answers

You can extract directly into a case class

val json = "the json";

val bike = parse(json).extract[Bike];

JSON parsing is done through reflection.

If the class is a runtime construct, create a TypeInfo instance and pass that to the extract method.

like image 155
flavian Avatar answered Jun 11 '26 09:06

flavian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!