Enumeration Encoding

59 views
Skip to first unread message

dmat...@sprypoint.com

unread,
Jan 15, 2015, 3:03:18 PM1/15/15
to activate-p...@googlegroups.com
Is it possible to change the Enumeration encoding so that Enums can be represented like the following

object Color extends Enumeration {
  val RED = Value("red")
  val BLUE = Value("blue")
  val GREEN = Value("green")
}

The supported format is causing issues with our Json4s serializers.

Flavio W. Brasil

unread,
Jan 16, 2015, 3:00:55 AM1/16/15
to dmat...@sprypoint.com, activate-p...@googlegroups.com
Hi,

It isn’t possible to use plain enumeration types because they don’t provide byte code information to allow the deserialization of values.

An alternative is use a sealed class + case objects + custom encoder. There is more code to write, but maybe it is compatible with Json4s.

sealed case class Color(name: String)
case object RED extends Color(“red”)
case object BLUE extends Color(“blue”)
case object GREEN extends Color(“green”)

import myContext._
class ColorEncoder
        extends Encoder[Color, String] {
    def encode(value: Color) = color.name
    def decode(name: String) = 
name match {
case “red” => RED
case “blue” => BLUE
case “green” => GREEN
}
}

Regards,

-- 
Flavio W. Brasil

--
You received this message because you are subscribed to the Google Groups "Activate Persistence Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to activate-persist...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

rpr...@sprypoint.com

unread,
Jan 16, 2015, 8:08:39 AM1/16/15
to activate-p...@googlegroups.com, dmat...@sprypoint.com
I'm not sure if creating even more convoluted enums is the right solution.  What class handles the Enumeration de/serialization?  We may just have to create a custom Json4S enum serializer that mimics the same pattern.
To unsubscribe from this group and stop receiving emails from it, send an email to activate-persistence+unsub...@googlegroups.com.

rpr...@sprypoint.com

unread,
Jan 16, 2015, 8:12:09 AM1/16/15
to activate-p...@googlegroups.com, dmat...@sprypoint.com
Also if the end result storage value a string, why is a method like Json4S EnumNameSerializer not a valid method?


On Friday, January 16, 2015 at 4:00:55 AM UTC-4, Flávio Brasil wrote:
To unsubscribe from this group and stop receiving emails from it, send an email to activate-persistence+unsub...@googlegroups.com.

dmat...@sprypoint.com

unread,
Jan 16, 2015, 8:53:51 AM1/16/15
to activate-p...@googlegroups.com, dmat...@sprypoint.com
We're also running into an issue where the enumeration will sometimes fail equivalence comparisons.  It looks like the #Val doesn't always get constructed with the proper index.

Flavio W. Brasil

unread,
Jan 16, 2015, 12:51:12 PM1/16/15
to dmat...@sprypoint.com, activate-p...@googlegroups.com
I don’t use Json2s, but it looks like the EnumNameSerializer needs to be provided by the application. Activate needs to be able to deserialize the string to the enum value without having to specify a serializer.

This is the deserialization code:


Best,

-- 
Flavio W. Brasil

To unsubscribe from this group and stop receiving emails from it, send an email to activate-persist...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages