Following this article https://github.com/FasterXML/jackson-module-scala/wiki/Enumerations
The enumeration declaration is as
object UserStatus extends Enumeration {
type UserStatus = Value
val Active, Paused = Value
}
class UserStatusType extends TypeReference[UserStatus.type]
case class UserStatusHolder(#JsonScalaEnumeration(classOf[UserStatusType]) enum: UserStatus.UserStatus)
The DTO is declared as
class UserInfo(val emailAddress: String, val userStatus:UserStatusHolder) {
}
and the serialization code is
val mapper = new ObjectMapper()
mapper.registerModule(DefaultScalaModule)
def serialize(value: Any): String = {
import java.io.StringWriter
val writer = new StringWriter()
mapper.writeValue(writer, value)
writer.toString
}
The resulting JSON serialization is
{
"emailAddress":"user1#test.com",
"userStatus":{"enum":"Active"}
}
Is it possible to get it the following form ?
{
"emailAddress":"user1#test.com",
"userStatus":"Active"
}
Have you tried:
case class UserInfo(
emailAddress: String,
#JsonScalaEnumeration(classOf[UserStatusType]) userStatus: UserStatus.UserStatus
)
The jackson wiki's example is a little misleading. You don't need the holder class. Its just an example of a thing that has that element. The thing you need is the annotation
Related
We have the following example and it seems that extractors do not work while converting Json to case class.
import play.api.libs.json.Reads._
import play.api.libs.json._
import play.api.libs.json.Format.GenericFormat
val json: JsValue = Json.parse("""
{
"firstName" : "John",
"lastName" : "Doe"
}
""")
trait BasePublicForm {
def firstName: String
def lastName: String
}
case class CustomerPublicForm(firstName: String, lastName: String) extends BasePublicForm
case class LeadPublicForm(firstName: String, lastName: String ) extends BasePublicForm
object CustomerPublicForm {
implicit val writesPublicLeadFormRequest: Writes[CustomerPublicForm] = Json.writes[CustomerPublicForm]
implicit val readsPublicLeadFormRequest: Reads[CustomerPublicForm] = Json.reads[CustomerPublicForm]
def apply(firstName: String, lastName: String): CustomerPublicForm = {
if(firstName.equalsIgnoreCase("John")) {
throw new Exception("John Exception")
}
new CustomerPublicForm(firstName, lastName)
}
}
object LeadPublicForm {
def apply(firstName: String, lastName: String): LeadPublicForm = {
new LeadPublicForm(firstName, lastName)
}
}
val s = json.validate[CustomerPublicForm] match {
case JsSuccess(form, _) => {
form
// do something with place
}
case e: JsError => {
// error handling flow
throw new Exception("Error")
}
}
s
Link -> https://scastie.scala-lang.org/eZrHTOVkQvSUJmoAJMTXfQ
Any ideas why it does not return Exception as expected?
The code doesn't throw an Exception (which is probably not a good idea anyway), as the json input is valid and so .validate returns CustomerPublicForm(John,Doe)
The Json.reads[CustomerPublicForm] seems to be circumventing your apply method.
I'm not sure of the exact details of Play's macro Reads generation, but it's probably just going straight for your class constructor rather than the apply method. Since the apply method you wrote has the same signature as the existing constructor, you'd be better off if you moved the validation (exception throwing) logic into the constructor and just removing the custom apply.
Alternatively you could make a custom apply with a different signature (not that it makes sense in this case, but I'm speaking generally here), and then using non-macro code to implement the Reads for that class.
I have an Enumeration in Scala
object Status extends Enumeration {
type Status = Value
val Success = Value
val Error = Value
}
This is used in the below -
case class Response(
status: Status,
errorMessage: String
)
I want to store Response in a file. So, I am using Jackson object mapper (com.fasterxml.jackson.databind.ObjectMapper ) to serialize it.
writeOutputToFile(filePath: Path , objectMapper.writeValueAsString(response))
However, object mapper writes an empty json to the file. I know object mapper requires a getter method to serialize. Is that why this is failing? Would I need a custom object mapper?
You can define generic serializer and deserializer for all enums and then for each of them register corresponding pairs of instances:
class EnumSerializer[T <: scala.Enumeration](e: T) extends JsonSerializer[T#Value] {
override def serialize(x: T#Value, jg: JsonGenerator, spro: SerializerProvider): Unit =
jg.writeString(x.toString)
}
class EnumDeserializer[T <: scala.Enumeration](e: T) extends JsonDeserializer[T#Value] {
private[this] val ec = new ConcurrentHashMap[String, T#Value]
override def deserialize(jp: JsonParser, ctxt: DeserializationContext): T#Value = Try {
val s = jp.getValueAsString
var x = ec.get(s)
if (x eq null) {
x = e.values.iterator.find(_.toString == s).get
ec.put(s, x)
}
x
}.getOrElse(ctxt.handleUnexpectedToken(classOf[T#Value], jp).asInstanceOf[T#Value])
}
val objectMapper: ObjectMapper with ScalaObjectMapper = {
val jsonFactory = new JsonFactoryBuilder()
.configure(...)
.build()
new ObjectMapper(jsonFactory) with ScalaObjectMapper {
registerModule(DefaultScalaModule)
registerModule(new SimpleModule()
.addSerializer(classOf[Status], new EnumSerializer(Status))
.addDeserializer(classOf[Status] new EnumDeserializer(Status))
...
)
}
}
The proposed solution is much safe and more efficient in runtime than just using of Status.withName(s) for each deserialization.
It will work even for the dynamic definition of enum values, like:
object Status extends Enumeration {
type Status = Value
val Success = Value
val Error = Value
def extra(name: String): Status = Value(nextId, name)
}
Status.extra("Unknown")
Full sources are here.
I have a trait and two case class:
#JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "type")
#JsonSubTypes(Array(
new Type(value = classOf[ScreenablePlainTextAddressValue], name = "ScreenablePlainTextAddressValue"),
new Type(value = classOf[ScreenableAddressValue], name = "ScreenableAddressValue")))
trait ScreenableAddress {
def screeningAddressType: String
def addressFormat: String
}
#JsonTypeName(value = "ScreenablePlainTextAddressValue")
case class ScreenablePlainTextAddressValue(addressFormat: String, screeningAddressType: String,
mailingAddress: MailingAddress) extends ScreenableAddress{
//code
}
#JsonTypeName(value = "ScreenableAddressValue")
case class ScreenableAddressValue(addressFormat: String, screeningAddressType: String, value: String)
extends ScreenableAddress{
//code
}
I am trying to deserialize the a json using jackson mapper for the above classes and I get a error as below:
missing property 'type' that is to contain type id (for class com.amazon.isp.execution.model.ScreenableAddress)
My Json look like this:
"{\"schemaVersion\":\"1.0\",\"screeningEntities\":[{\"screeningRecordList\":[{\"address\":{}},{\"address\":{\"addressFormat\":\"ADDRESS_ID\",\"value\":\"SK36SAV4WD4BSGDLGZQEG05RMMA0261533395I3IZ4AMMRVPXTQ2EIA2OXZFEHLF\",\"screeningAddressType\":\"OTHER_ADDRESS\"}},{\"name\":{}},{\"name\":{\"screeningNameType\":\"OTHER_NAME\",\"nameFormat\":\"KEYMASTER_SEALED_FULL_NAME\",\"value\":\"AAAAAAAAAADYzV1Zr7RxcB5y53pylNH4KgAAAAAAAAD3vya/1QtdPxVBZM+alrMIZZvaz9DHm+w+qby7z/c8YutbxeoX6so+mGY=\"}},{\"name\":{\"screeningNameType\":\"LEGAL_NAME\",\"nameFormat\":\"KEYMASTER_SEALED_FULL_NAME\",\"value\":\"AAAAAAAAAADYzV1Zr7RxcB5y53pylNH4KgAAAAAAAACRUkz7uKJGOQCB0pZWXRXTFrk9Enpucj33hV+/yHRM/dQKo2yWGxGcjB8=\"}}]}],\"screeningMetaData\":{\"customerId\":\"" + CustomerID + "\"}}"
Any help will be really appreciated.
I still get the same error, I have defined the marshaller (and imported it); it appears that the case class entry is not in context when the function is polymorphic. and this throws a Cannot find JsonWriter or JsonFormat type class for Case Class. Is there a reason why spray-json can not find the implicit marshaller for the case class, (even when defined) is this case class in context? Link to marshaller
import spray.json._
import queue.MedusaJsonProtocol._
object MysqlDb {
...
}
case class UserDbEntry(
id: Int,
username: String,
countryId: Int,
created: LocalDateTime
)
trait MysqlDb {
implicit lazy val pool = MysqlDb.pool
}
trait HydraMapperT extends MysqlDb {
val FetchAllSql: String
def fetchAll(currentDate: String): Future[List[HydraDbRow]]
def getJson[T](row: T): String
}
object UserHydraDbMapper extends HydraMapperT {
override val FetchAllSql = "SELECT * FROM user WHERE created >= ?"
override def fetchAll(currentDate: String): Future[List[UserDbEntry]] = {
pool.sendPreparedStatement(FetchAllSql, Array(currentDate)).map { queryResult =>
queryResult.rows match {
case Some(rows) =>
rows.toList map (x => rowToModel(x))
case None => List()
}
}
}
override def getJson[UserDbEntry](row: UserDbEntry): String = {
HydraQueueMessage(
tableType = HydraTableName.UserTable,
payload = row.toJson.toString()
).toJson.toString()
}
private def rowToModel(row: RowData): UserDbEntry = {
UserDbEntry (
id = row("id").asInstanceOf[Int],
username = row("username").asInstanceOf[String],
countryId = row("country_id").asInstanceOf[Int],
created = row("created").asInstanceOf[LocalDateTime]
)
}
}
payload = row.toJson.toString() Can't find marshaller for UserDbEntry
You have defined UserDbEntry locally and there is no JSON marshaller for that type. Add the following:
implicit val userDbEntryFormat = Json.format[UserDbEntry]
I'm not sure how you can call row.toJson given UserDbEntry is a local case class. There must be a macro in there somewhere, but it's fairly clear that it's not in scope for the local UserDbEntry.
Edit
Now that I see your Gist, it looks like you have a package dependency problem. As designed, it'll be circular. You have defined the JSON marshaller in package com.at.medusa.core.queue, which imports UserDbEntry, which depends on package com.at.medusa.core.queue for marshalling.
I'm using the play 2.1 framework for scala and the MongoDB Salat plugin.
When I update an Enumeration.Value I got an exception:
java.lang.IllegalArgumentException: can't serialize class scala.Enumeration$Val
at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:270) ~[mongo-java-driver-2.11.1.jar:na]
at org.bson.BasicBSONEncoder.putIterable(BasicBSONEncoder.java:295) ~[mongo-java-driver-2.11.1.jar:na]
at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:234) ~[mongo-java-driver-2.11.1.jar:na]
at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:174) ~[mongo-java-driver-2.11.1.jar:na]
at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:120) ~[mongo-java-driver-2.11.1.jar:na]
at com.mongodb.DefaultDBEncoder.writeObject(DefaultDBEncoder.java:27) ~[mongo-java-driver-2.11.1.jar:na]
Inserting the Enumeration.Value works fine. My case class looks like:
case class User(
#Key("_id") id: ObjectId = new ObjectId,
username: String,
email: String,
#EnumAs language: Language.Value = Language.DE,
balance: Double,
added: Date = new Date)
and my update code:
object UserDAO extends ModelCompanion[User, ObjectId] {
val dao = new SalatDAO[User, ObjectId](collection = mongoCollection("users")) {}
def update(): WriteResult = {
UserDAO.dao.update(q = MongoDBObject("_id" -> new ObjectId(id)), o = MongoDBObject("$set" -> MongoDBObject("language" -> Language.EN))))
}
}
Any ideas how to get that working?
EDIT:
workaround: it works if I cast the Enumeration.Value toString, but that's not how it should be...
UserDAO.dao.update(q = MongoDBObject("_id" -> new ObjectId(id)), o = MongoDBObject("$set" -> MongoDBObject("language" -> Language.EN.toString))))
It is possible to add a BSON encoding for Enumeration. So, the conversion is done in a transparent manner.
Here is the code
RegisterConversionHelpers()
custom()
def custom() {
val transformer = new Transformer {
def transform(o: AnyRef): AnyRef = o match {
case e: Enumeration$Val => e.toString
case _ => o
}
}
BSON.addEncodingHook(classOf[Enumeration$Val], transformer)
}
}
At the time of writing mongoDB doesn't place nice with scala enums, I use a decorator method as a work around.
Say you have this enum:
object EmployeeType extends Enumeration {
type EmployeeType = Value
val Manager, Worker = Value
}
and this mongodb record:
import EmployeeType._
case class Employee(
id: ObjectId = new ObjectId
)
In your mongoDB, store the integer index of the enum instead of the enum itself:
case class Employee(
id: ObjectId = new ObjectId,
employeeTypeIndex: Integer = 0
){
def employeeType = EmployeeType(employeeTypeIndex); /* getter */
def employeeType_=(v : EmployeeType ) = { employeeTypeIndex= v.id} /* setter */
}
The extra methods implement getters and setters for the employee type enum.
Salat only does its work when you serialize to and from your model object with the grater, not when you do queries with MongoDB-objects yourself. The mongo driver api knows nothing about the annotation #EnumAs. (In addition to that even if you could use salat for that, how would it be able to know that you are referring to User.language in a generic key->value MongoDBObject?)
So you have to do like you describe in your workaround. Provide the "value" of the enum yourself when you want to do queries.