1

I have a this object:

case class UserStateSummaryByHourPayload (
        ownerid: String,
        userid: String,
        team: String,
        profile: String,
        day: String,
        hour: Int,
        sortdate: String,
        month: String,
        durationpayload: Map[String, Long],
        maxdurationpayload: Map[String, Long]
    )

I would like to turn the two Map attributes into JSON or a string (writing to mysql column) such as this

case class JunkTest (
        ownerid: String,
        userid: String,
        team: String,
        profile: String,
        day: String,
        hour: Int,
        sortdate: String,
        month: String,
        durationpayload: String,
        maxdurationpayload: String
    )

I tried to do something like this following some other peoples guides, but its not working:

import com.fasterxml.jackson.module.scala.DefaultScalaModule;
            import com.fasterxml.jackson.databind.ObjectMapper;

            @transient val mapper = new ObjectMapper();
            mapper.registerModule(DefaultScalaModule);

            var junktest = ussapayloads.map(a => new JunkTest(
                a.ownerid, 
                a.userid,
                a.team, 
                a.profile, 
                a.day, 
                a.hour, 
                a.sortdate, 
                a.month, 
                mapper.writeValueAsString(a.durationpayload),
                mapper.writeValueAsString(a.maxdurationpayload)
            ));

How to I take my Map[String, Long] and turn it into JSON or a JSON string?

Map("Online" -> 100,"Ready" -> 200)
{ "Online": 100, "Ready": 200 }

2 Answers 2

2

Here you go!

import spray.json._
import DefaultJsonProtocol._
object Demo1 extends App {
val keyValue = Map("Online" -> 100, "Ready" -> 200)
    
println(keyValue.toJson) //output {"Online":100,"Ready":200}
}
Sign up to request clarification or add additional context in comments.

Comments

1

Using play-json it is really easy:

import play.api.libs.json.Json
Json.stringify(Json.toJsObject(Map("Online" -> 100, "Ready" -> 200)))

Code run can be found at Scastie;

BTW, the code you attached works completely fine. You can find it in Scastie.

In order to install play-json please add the following to your build.sbt:

resolvers += "play-json" at "https://mvnrepository.com/artifact/com.typesafe.play/play-json"
libraryDependencies += "com.typesafe.play" %% "play-json" % "2.9.1"

8 Comments

I get exceptions with my above code: Caused by: java.io.NotSerializableException: com.fasterxml.jackson.module.paranamer.shaded.CachingParanamer
@phattyD Are you sure? Do you have redundant imports? Can you reproduce this failure in Scastie?
Yes I am sure, it works in Scastie, but does not work in my lab environment. I am trying implementations for the play-json and spray json now. Just trying to find the statements for adding to my sbt build to import the packages
@phattyD, I added what you need in your build.sbt
Thanks Tomer! I ended up with errors: Caused by: java.lang.ClassNotFoundException: play.api.libs.json.Writes I have tried importing play.api.libs.json._ and that didn't help. I had the same issues with the spray.json above. Is there a jar I need to add to classpath?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.