Created
October 2, 2020 06:45
-
-
Save kdkanishka/eabef7c9c6a853e59f025f91a95a5e0f to your computer and use it in GitHub Desktop.
spray json example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pagero.services.emailapi | |
import spray.json._ | |
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport | |
import spray.json.DefaultJsonProtocol | |
case class Customer (id :Option[Int],name: Option[String]) | |
object Test extends App with DefaultJsonProtocol with SprayJsonSupport{ | |
implicit val CompanyFormat = jsonFormat2(Customer) | |
val cust = Customer(Some(1),Some("Cool")) | |
val json = cust.toJson.prettyPrint | |
println(json) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
result.
{ "id": 1, "name": "Cool" }
When an attribute of the Customer model is None, The spray json serializer will skip that attribute.
ie. Customer(Some(1),None)
{ "id": 1 }