spray-json: could not find implicit value for FromRequestUnmarshallar - json

I'm creating my first application with AKKA-http. I'm currently using spray-json to write object to json. When I created GET request everything is working fine, but I tried a POST request and the following error shows:
Error:(55, 26) could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[nl.quintor.model.Employee]
entity(as[Employee]) { request =>
This is my main class:
object Main extends App with Routes {
implicit val system = ActorSystem("system")
implicit val executor: ExecutionContext = system.dispatcher
implicit val materializer: ActorMaterializer = ActorMaterializer()
override val slickboard = createSlickboard
Http().bindAndHandle(routes, httpInterface, httpPort)
private def createSlickboard: ActorRef =
system.actorOf(Slickboard.props(system), applicationName)
}
}
I defined my routes in the following trait:
trait Routes extends CORSSupport with Protocols {
val slickboard: ActorRef
implicit val timeout = Timeout(5 seconds)
val routes =
corsHandler {
pathPrefix("employees") {
pathEnd {
get {
complete {
(slickboard ? GetAllEmployees).mapTo[HttpResponse]
}
}
}
} ~
pathPrefix("employee") {
path(IntNumber) { id =>
get {
complete {
(slickboard ? GetEmployeeById(id)).mapTo[HttpResponse]
}
} ~
post {
decodeRequest {
entity(as[Employee]) { request =>
complete {
request.toString()
}
}
}
}
}
}
}
}
I defined my protocol just like spray recommended:
trait Protocols extends DefaultJsonProtocol {
implicit val employeeFormat = jsonFormat8(Employee.apply)
}
The class I'm trying to convert to json is the following:
case class Employee(ID: Int, firstname: String, lastname: String, street: String, zipCode: String, city: String, phoneNumber: String, image: String)
I tried multiple solutions found in stack overflow, but none of them are actually working.
Could someone please help me with this problem?

I suspect you need
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._

Related

Receive Map in ktor server using kotlinx.serialization

I have a Ktor server running with kotlinx.serialization as it's json (de)serializer
I send my Ktor a message like this one:
{
"Brick(partNumber=004229, partName=Sticker Sheet for Set 295-1, dataSource=Rebrickable, brickImage=null, categoryID=58)": 5
}
which is a pair of an Int and this class:
import kotlinx.serialization.Serializable
#Serializable
data class Brick(
val partNumber: String,
val partName: String,
val dataSource: String,
val brickImage: String?,
val categoryID: Int?
)
But I get this error
kotlinx.serialization.json.JsonDecodingException: Invalid JSON at 0: Expected '[, kind: MAP'
at kotlinx.serialization.json.internal.JsonReader.fail(JsonReader.kt:293)
Which to me means that kotlinx.serialization expects a different syntax for the map class. Which is odd to me. When I change the type into List> it throws the same exception but with LIST instead of MAP.
EDIT: Upon further inspection, it expects a [ instead of a { at the start of the line.
My (partial) application implementation
fun Application.module(testing: Boolean = false) {
install(ContentNegotiation) { serialization() }
routing {
route("user") {
route("brick") {
post {
call.request.queryParameters["userName"]
?.let { userRepository.login(it) } // Someone else is still working login nvm this
?.let { user ->
val bricks = call.receive<Map<Brick, Int>>() // This throws an error
userRepository.addBricks(user, bricks)
call.respond(HttpStatusCode.OK)
}
?: call.respond(HttpStatusCode.Unauthorized)
}
}
}
}
}
The android retrofit function that sends the class (using GSON):
#POST("/user/brick")
suspend fun setBricksAmounts(
#Query("userName")
userName: String,
#Body
brickAmounts: Map<Brick, Int>
)
I think using a class as a key does not work in kotlinx serialization
and it looks like that class is just serialized into a string to use it as key
Instead you can receive it as Map<String, Int>
and afterwards run
bricks.mapKeys { (jsonString, number) ->
Json(JsonConfiguration.Stable).parse(Brick.Serializer, jsonString)
}
or the equivalent jackson code if you want

Kotlin Json Parser

I have this JSON in Kotlin, and I'm not able to fetch and parse. Any quick help. Please.
[{platform: {name: "mena-web",publishingRegion: "mena",platformGroup:"web",id: 2,countryCode: "AE",locales: {locale:["en_US","ar_AE"]}}}]
Here are my data classes:
data class Locales(var locale: ArrayList<String>) {}
data class Platform(var name: String, var publishingRegion: String, var platformGroup: String, var id: Int, var countryCode: String, var locales: Locales) {}
data class Json(var platform: Platform) {}
Here is my JSON API interface:
interface Api {
#GET("/me.json2")
fun getGeo(callback: Callback<List<Json>>): Call<List<Json>>
}
Here is my RestAPI:
class RestAPI(val api: Api) {
fun getNews(callback: Callback<List<Json>>) {
val call = api.getGeo(callback)
call.enqueue(callback)
}
}
Here is my RestAPI call:
try {
val api: RestAPI
val retrofit = Retrofit.Builder()
.baseUrl(PLATEFORM_URL)
.addConverterFactory(MoshiConverterFactory.create())
.build()
api = retrofit.create(RestApi::class.java)
val callback = object : Callback<List<Json>> {
override fun onResponse(call: Call<List<Json>>?, response: retrofit2.Response<List<Json>>?) {
response?.isSuccessful.let {
this#MainActivity.photos = response?.body()
}
}
override fun onFailure(call: Call<List<Json>>?, t: Throwable?) {
Log.e("MainActivity", "Problems calling API", t)
}
}
api.getGeo(callback)
// Log.e("Message", test.getNews().toList().toString())
} catch(e:Exception){
Log.e("Message", e.message)
}
Thanks Guys!
I found the answer, everything stat working after changing the parser
MoshiConverterFactory to GsonConverterFactory.
respones is the string which will be your jsonResponse
try {
val rootArray = JSONArray(respones)
val mainObject=rootArray.getJSONObject(0)
val platformObject=mainObject.getJSONObject("platform")
val name=platformObject.getString("name")
val publishingRegion=platformObject.getString("publishingRegion")
val platformGroup=platformObject.getString("platformGroup")
val id=platformObject.getInt("id")
val countryCode=platformObject.getString("countryCode")
val localesObj=platformObject.getJSONObject("locales")
val localeArray=locales.getJSONArray("locale")
val stringOne=localeArray.getString(0)
val stringTwo=localeArray.getString(1)
} catch (e: JSONException){}

How to edit existing JSON object with sprayJSON

I am using akka with spray json support for which I need to edit value in the recieved json.
import akka.http.scaladsl.server.Directives
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import spray.json._
final case class Item(name: String, id: Long)
final case class Order(items: List[Item],orderTag:String)
trait JsonSupport extends SprayJsonSupport with DefaultJsonProtocol {
implicit val itemFormat = jsonFormat2(Item)
implicit val orderFormat = jsonFormat2(Order)
}
In my use case I recieve the json with orderTag value as null, all I need to do is edit the orderTag value with and then use it as entity value.Is it possible to write/edit jsonObject and How to do that ?
class MyJsonService extends Directives with JsonSupport {
// format: OFF
val route =
get {
pathSingleSlash {
complete(Item("thing", 42)) // will render as JSON
}
} ~
post {
entity(as[Order]) { order => // will unmarshal JSON to Order
val itemsCount = order.items.size
val itemNames = order.items.map(_.name).mkString(", ")
complete(s"Ordered $itemsCount items: $itemNames")
}
}
}
You can just edit the json AST like ..
val json = """{"orderTag":null}"""
val jsVal = json.parseJson
val updatedJs = if (jsObj.fields.get("orderTag") == Some(JsNull)) {
JsObject(jsObj.fields + ("orderTag" -> JsString("new tag")))
} else {
jsObj
}
updatedJs.compactPrint
res26: String = """
{"orderTag":"new tag"}
"""

Handle JSON data in POST request using json4s

I'm trying to map JSON i/p to my case class CacheRequest.
request is POST.
I'm new to Scala and Akka.
import org.json4s.{DefaultFormats, Formats}
implicit val formats: Formats = DefaultFormats
val route: Route = traceContextAwareRoute {
pathPrefix(system.name) {
post {
path("sample") {
entity(as[CacheRequest]) { x => {
val cacheRequest: CacheRequest = CacheRequest(x.a, x.b, x.c, x.d, x.e, x.f)
onComplete(getSystemStock(cacheRequest)) {
(response: Try[Option[CacheResponse]]) => complete(processResponse(response))
}
}
}
}
}
}
My case class is like this.
case class CacheRequest(a: String,
b: String,
c: Int,
d: Int,
e: Int,
f: Int)
Getting an Error Like
could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[mcc.movie.common.model.CacheRequest]
not enough arguments for method as: (implicit um: akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[mcc.movie.common.model.CacheRequest])akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[mcc.movie.common.model.CacheRequest]
I'supposed to do this using json4s.
Any help regarding this is fine for me.
You can use this lib: https://github.com/hseeberger/akka-http-json
code may something like this
import org.json4s.{DefaultFormats, Formats, jackson}
import de.heikoseeberger.akkahttpjson4s.Json4sSupport
class Route {
import Json4sSupport._
implicit val formats: Formats = DefaultFormats
implicit val serialization = jackson.Serialization
val route: Route = traceContextAwareRoute {
pathPrefix(system.name) {
post {
path("sample") {
entity(as[CacheRequest]) { x => {
val cacheRequest: CacheRequest = CacheRequest(x.a, x.b, x.c, x.d, x.e, x.f)
onComplete(getSystemStock(cacheRequest)) {
(response: Try[Option[CacheResponse]]) => complete(processResponse(response))
}
}
}
}
}
}
}

Play Writes with generic type parameters

I have a trait Processor that looks like this:
trait Processor[A] {
def process(in: Seq[Byte]): Result[A]
}
trait Result[A]{
val ok: Boolean
val errorMessage: Option[String]
val data: Option[A]
}
A concrete implementation:
class StringProc extends Processor[String] {
def process(in: Seq[Byte]): StrResult
}
case class StrResult(...) extends Result[String]
object StrResult {
implicit val writes = Json.writes[StrResult]
}
When using a StringProc instance as type Processor[String], the return type of process unsurprisingly is Result[String], not StrResult. Unfortunately, the Writes[StrResult] seems to be useless in this case:
No Json serializer found for type Result[String]
How could I handle this situation?
You can try
object Result {
implicit def resWrites[T](implicit nested: Writes[T]): Writes[Result[T]] = OWrites[Result[T]] { res =>
Json.obj("ok" -> res.ok, "errorMessage" -> res.errorMessage,
"data" -> nested.writes(res.data))
}
}