Issue with json conversions for scalax graph library for scala - json

We are using graph-json 1.11.0 and graph-core-1.11.5 with in a play 2.5.x application .
http://www.scala-graph.org/
The user guide examples for toJson and fromJson for a graph , do not wok with the current stable 1.11.0 release .
Ref : http://www.scala-graph.org/guides/json.html
We make use of a simple string graph in our application
Graph[String,DiEdge] .
We managed to write the graph to json conversion part , but unable to identify the exact fromJson syntax for the new stable version .
Below is a sample code used in our application . Can some one help us on how to convert json to graph.
import play.api.libs.json.{JsValue, Json}
import scalax.collection.Graph
import scalax.collection.GraphEdge.DiEdge
import scalax.collection.io.json.JsonGraph
import scalax.collection.io.json.descriptor.Descriptor
import scalax.collection.io.json.descriptor.predefined.Di
import scalax.collection.io.json.descriptor.{Descriptor, StringNodeDescriptor}
object HierarchyGraph {
val descriptor = new Descriptor(StringNodeDescriptor,Di.descriptor[String]())
def toJson(graph : Graph[String, DiEdge]) : JsValue = {
val jsText = JsonGraph(graph).toJson(descriptor)
try {
Json.parse(jsText)
} catch {
case e : Exception => Json.toJson(Json.obj())
}
}
def fromJson(graphAsJsValue : JsValue) : Graph[String,DiEdge] = {
// json to graph conversion code here
}
}

Issue addressed by peter-empen on github .
https://github.com/scala-graph/scala-graph/issues/71

Related

Spark | Could not create FileClient | read json | scala

I am trying to read a json file in windows local machine using spark and scala. I have tried like below :
object JsonTry extends App{
System.setProperty("hadoop.home.dir", "C:\\winutils")
val sparkSession = SparkSession.builder()
.master("local[*]")
.config("some-config", "some-value")
.appName("App Name")
.getOrCreate();
val res = sparkSession.read.json("./src/main/resources/test.json")
res.printSchema()
}
Json file which is under resource folder looks like below :
{"name":"Some name"}
But i am getting an exception when I run this main class :
Exception in thread "main" java.io.IOException: Could not create
FileClient
Screenshot attached :
To my surprise this piece of code is working, but i am looking to read json from file directly.
val res = sparkSession.read.option("multiline", true).json(sparkSession.sparkContext.parallelize(Seq("{\"name\":\"name\"}")))
Please let me know what is causing this issue, as I am not getting any solution .
I tried to read a json file in a similar way but I didnt face any problem,
You may try this too .
object myTest extends App {
val spark : SparkSession = SparkSession.builder()
.appName("MyTest")
.master("local[*]")
.getOrCreate()
import spark.implicits._
val jsonDataDF = spark.read.option("multiline","true").json("/Users/gp/Desktop/temp/test.json")
jsonDataDF.show()
}
Output -
I/P Data (JSOn) ->
Do let me know if I understood your question properly or not ?

Convert scala Map to Json in play framework

I am trying to convert a Map to Json in play framwork 2.6.x.
The code is like
def toJson(x:Map[String, Object]) = {
import play.api.libs.json._
Json.toJson(x)
}
I got error No Json serializer found for type Map[String,Object]. Try to implement an implicit Writes or Format for this type.
The I add writes and code becomes:
def toJson(x:Map[String, Object]) = {
import play.api.libs.json._
implicit val writes = Json.writes[Map[String, Object]]
Json.toJson(x)
}
I hit error. No apply function found for scala.collection.immutable.Map

Scala Play Json implicit writes type mismatch

I am new to Scala and Play, and I ask for help with this simple example. I tried to search for solution by myself, but I did not succeed.
I am trying to do the example from from Mastering Play Framework for Scala book, the one about extending Json parser (Pages 29-30).
The environment I use is:
Scala: 2.11.7
Play: 2.5.8
Activator: 1.3.10
The code is:
case class Subscription(emailId: String, interval: Long)
In controller:
import play.api.libs.json.Json
import play.api.libs.json.JsValue
import play.api.libs.json.Writes
.....
val parseAsSubscription = parse.using {
request =>
parse.json.map {
body =>
val emailId:String = (body \ "emailId").as[String]
val fromDate:Long = (body \ "fromDate").as[Long]
Subscription(emailId, fromDate)
}
}
implicit val subWrites:Writes[Subscription] = Json.writes[Subscription]
def getSub = Action(parseAsSubscription) {
request =>
val subscription: Subscription = request.body
Ok(Json.toJson(Subscription))
}
The line: Ok(Json.toJson(Subscription)) gives an error
No Json serializer found for type models.Subscription.type. Try to
implement an implicit Writes or Format for this type.
This is odd, because Writes object is defined one row above. Thus, I tried to pass it to toJson method explicitly:
Ok(Json.toJson(Subscription)(subWrites))
It gave me a different error, which partially explained why existing Writes object did not suit:
type mismatch;
found:
play.api.libs.json.Writes[models.Subscription]
required:
play.api.libs.json.Writes[models.Subscription.type]
However, I don't understand the nature of this error and what models.Subscription.type is .
I used to do a similar thing in a different example, and it worked just fine.
Any help will be appreciated.
You're trying to serialize the type Subscription, rather than the request body, which you stored as the value subscription. Try replacing the last line with Ok(Json.toJson(subscription)).

Gatling :- Compare web service Json response using jsonFileFeeder

I'm using JSON feeder to compare JSON output by web services as follows,
val jsonFileFeeder = jsonFile("test_data.json")
val strategy = (value: Option[String], session: Session) => value.map { jsonFileFeeder =>
val result = JSONCompare.compareJSON("expectedStr", "actualStr", JSONCompareMode.STRICT)
if (result.failed) Failure(result.getMessage)
else Success(value)
}.getOrElse(Failure("Missing body"))
val login = exec(http("Login")
.get("/login"))
.pause(1)
.feed(feeder)
.exec(http("authorization")
.post("/auth")
.headers(headers_10)
.queryParam("""email""", "${email}")
.queryParam("""password""", "${password}")
.check(status.is(200))
.check(bodyString.matchWith(strategy)))
.pause(1)
But it throws error
value matchWith is not a member of io.gatling.core.check.DefaultFindChe
ckBuilder[io.gatling.http.check.HttpCheck,io.gatling.http.response.Response,String,String]
15:10:01.963 [ERROR] i.g.a.ZincCompiler$ - .check(bodyString.matchWith(jsonFileFeeder)))
s\lib\Login.scala:18: not found: value JSONCompare
15:10:05.224 [ERROR] i.g.a.ZincCompiler$ - val result = JSONCompare.compareJSON(jsonFileFeeder, j
sonFileFeeder, JSONCompareMode.STRICT)
^
15:10:05.631 [ERROR] i.g.a.ZincCompiler$ - two errors found
Compilation failed
Here's a sample script that semantically compares a JSON response with expected output:
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.core.json.Jackson
import java.nio.charset.StandardCharsets.UTF_8
import scala.concurrent.duration._
class BasicSimulation extends Simulation {
lazy val expectedJson = Jackson.parse(
getClass.getResourceAsStream("/output.json"),
UTF_8
)
val scn = scenario("Scenario Name")
.exec(http("request_1")
.get("http://localhost:8000/output.json")
.check(bodyString.transform(Jackson.parse).is(expectedJson))
)
setUp(scn.inject(atOnceUsers(1)))
}
It assumes there is a file output.json in the resources directory (the directory that also contains your data and request-bodies).
However, I think you should carefully consider whether this solution is right for your needs. It won't scale as well as JSONPath or regex checks (especially for large JSON files), it's inflexible, and it seems more like a functional testing task than a performance task. I suspect that if you're trying to compare JSON files in this way, then you're probably trying to solve the wrong problem.
Note that it doesn't use jsonFile, as jsonFile is designed for use as a feeder, whereas I suspect you want to compare a single request with a hard-coded response. However, jsonFile may prove useful if you will be making a number of different requests with different parameters and expect different (known) responses. Here's an example script that takes this approach:
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.core.json.Jackson
import scala.concurrent.duration._
class BasicSimulation extends Simulation {
val myFeed = jsonFile("json_data.json").circular
val scn = scenario("Scenario Name")
.feed(myFeed)
.exec(http("request_1")
.get("${targetUrl}")
.check(bodyString.transform(Jackson.parse).is("${expectedResponse}"))
)
setUp(scn.inject(atOnceUsers(2)))
}
It assumes there is a json resource in data/json_data.json, that looks something like the following:
[
{
"targetUrl":"http://localhost:8000/failure.json",
"expectedResponse":
{
"success": false,
"message": "Request Failed"
}
},
{
"targetUrl":"http://localhost:8000/success.json",
"expectedResponse":
{
"success": true,
"message": "Request Succeeded"
}
}
]
The expectedResponse should be the exact JSON you expect to get back from the server. And of course you don't just have to parameterise targetUrl, you can parameterise whatever you want in this way.
As an aside, you may also be interested to know that Gatling 2.1 is expected to allow comparing an response with a file without using hacks like these (although the current development version only supports comparing byte-for-byte, not comparing-as-json).

Output parsed JSON to template in lift

Using Lift's json parser, how can I output parsed json objects into a template?
The datatypes that net.liftweb.json.JsonParser provides are not
standard lists.
package rem.lift_client
package snippet
import net.liftweb._
import util._
import Helpers._
import net.liftweb.json.JsonParser._
class SearchResults {
def render() = {
val json_raw = "[ {\"userName\":\"John\"}, {\"userName\":\"Michael\"} ]"
val json_parsed = parse(input)
"li *" #> json_parsed.toString <---- NOT CORRECT
}
}
In the above example, I wanted to output a list of users as:
John
Michael
How do I interpret the parsed object? Any ideas are welcome, thanks.
NOTE: In addition to the accepted answer, lift-json has an excellent documentation on this subject.
One way is to extract the data with case classes.
implicit val formats = DefaultFormats
case class User(userName: String)
json_parsed.extract[List[User]]