Extracting Hash from JSON, saved into session, cannot be converted to Map

395 zobrazení
Preskočiť na prvú neprečítanú správu

Sajjad Lateef

neprečítané,
20. 4. 2016, 1:19:0220. 4. 2016
komu: gatling

Using Gatling 2.2.0 (pre-release version)


There an API that contains locations which is Hash/Map of String to Int. I have saved this into a session attribute. I am trying to extract the keys from this Hash for building out other API calls. But, I am getting stuck extracting the value from the session attribute. 


JSON:

  "locations": {
        "/href/10592": 373,
        "/href/10663": 6032,
...   }


Using

.check(jsonPath("$.locations").find.saveAs("locs"))


Using println, I can see that this  Session attribute looks like:

locs -> {"/href/10592":373,"/href/10663":6032, ...}


To me, this locs looks like a Map[String, Int] . 

But, when I get the session attribute back, and try to convert it into a Map[String, Int], it looks like it being stored as a String, which cannot be cast into a Map. 

var locMap = session("locs").as[Map[String,Int]]  // ------> class cast exception

[ERROR] [04/19/2016 21:18:20.947] [GatlingSystem-akka.actor.default-dispatcher-6] [akka://GatlingSystem/user/sessionHook-2] java.lang.String cannot be cast to scala.collection.immutable.Map
java.lang.ClassCastException: java.lang.String cannot be cast to scala.collection.immutable.Map
at com.company.IllBFS$$anonfun$1.apply(IlBFS.scala:56)
at com.company.IllBFS$$anonfun$1.apply(IlBFS.scala:55)
at io.gatling.core.action.SessionHook.executeOrFail(SessionHook.scala:46)
at io.gatling.core.action.Failable$class.execute(Actions.scala:71)

I think that the json element (of type String) has to be evaluated into an Map object before passing it into saveAs(). For that, I guess I need to use a transform before the saveAs to correctly convert the JSON-string into a Hash. Is this correct reasoning?

Thanks in advance for any advice.

Regards
Sajjad

--
Sajjad Lateef  sajjadlateef (a) acm.org
slateef.gc (a) gmail.com

Sajjad Lateef

neprečítané,
20. 4. 2016, 14:30:2120. 4. 2016
komu: gatling
Solved this by using a session function to parse the string into a JSON object. 

                      val labels = session("locs").as[String]
                      val json:Option[Any] = JSON.parseFull(labels)
                      val labelsVector: Vector[String] = json.get.asInstanceOf[Map[String, Int]].keys.toVector
                      println(labelsVector.toString)

slan...@gatling.io

neprečítané,
20. 4. 2016, 14:33:0220. 4. 2016
komu: gat...@googlegroups.com
Bad idea. Perf killer. Use ofType[]. Can't explain more, I'm on mobile. Please search the doc.
--
You received this message because you are subscribed to the Google Groups "Gatling User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gatling+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Odpovedať všetkým
Odpovedať autorovi
Poslať ďalej
0 nových správ