Skip to content

Instantly share code, notes, and snippets.

@kdkanishka
Created October 2, 2020 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kdkanishka/eabef7c9c6a853e59f025f91a95a5e0f to your computer and use it in GitHub Desktop.
Save kdkanishka/eabef7c9c6a853e59f025f91a95a5e0f to your computer and use it in GitHub Desktop.
spray json example
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)
}
@kdkanishka
Copy link
Author

kdkanishka commented Oct 2, 2020

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 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment