jerkson icon indicating copy to clipboard operation
jerkson copied to clipboard

Enumeration Support for Jerkson

Open gzoller opened this issue 13 years ago • 12 comments

It would be a great addition to Jerkson if it supported ser/deser of Scala Enumerations. Sample below:

object Day extends Enumeration { val Mon, Tues, Wed, Thurs, Fri, Sat, Sun = Value }

gzoller avatar May 06 '11 19:05 gzoller

+1

Also the following construct doesn't seem to work:

sealed abstract class Status case object ON extends Status case object OFF extends Status

if one tries to serialize/deserialize the object containing such type, it is serialized as {}

if we do something like: case object ON extends Status {val strValue:String="ON"} case object OFF extends Status {val strValue:String="OFF"}

then it does put these values in when serializing, but crashes and returns null when tries to deserialize.

Ashalynd avatar Nov 22 '11 10:11 Ashalynd

I would dearly love to be able to serdes arbitrary algebraic data types. I have a lot of pairs of Foo and FooDTO classes. :(

acruise avatar Dec 06 '11 22:12 acruise

+1

fooblahblah avatar Apr 10 '12 20:04 fooblahblah

+1

artgon avatar Jul 05 '12 21:07 artgon

+1

diegovar avatar Aug 03 '12 14:08 diegovar

+1

crumbpicker avatar Aug 03 '12 18:08 crumbpicker

I just tried hacking this, and I don't see any way to deserialize enumeration values:

scala> object Colors extends Enumeration { val Red, Blue, Green = Value }
defined module Colors

scala> Colors.Red
res61: Colors.Value = Red

scala> manifest[Colors.Value]
res62: Manifest[Colors.Value] = scala.Enumeration$Value

scala> manifest[Enumeration#Value]
res63: Manifest[Enumeration#Value] = scala.Enumeration#scala.Enumeration$Value

scala> manifest[Colors.Value] == manifest[Enumeration#Value]
res64: Boolean = true

Basically, if I say Json.parse[Colors.Value](string), then manifest[Colors.Value] doesn't appear to contain any more information identifying Colors or Colors.type than Enumeration#Value does (i.e. none), which I think is what we'd need to get at Colors.withName(string) to construct something like Colors.Red, Colors.Blue, etc.

jdanbrown avatar Sep 08 '12 04:09 jdanbrown

salat has _typeHint field for exactly that reason

imikushin avatar Sep 08 '12 11:09 imikushin

Ok, clever idea: _typeHint in salat

I think that's not so desirable in this case since we'd have to give up "red" in favor of something like {_typeHint: com.awesome.project.module.Colors, value: "red"}, which is both way more verbose and also exposes the implementation detail of which jvm class is used to model it, which becomes a burdensome leaky abstraction as soon as you want to interop with non-jvm systems.

jdanbrown avatar Sep 10 '12 17:09 jdanbrown

Actually, Salat has a concept of context, and type hinting is just one thing in a context. I believe, it can be done in a more elegant way, like detecting what type it is by the fieldset of the JSON document being deserialized. But again, some types might have the same fieldsets, so then the deserializer shall need some hint.

imikushin avatar Sep 12 '12 19:09 imikushin

+1

dircsem avatar Oct 29 '12 16:10 dircsem

:+1:

JoshZA avatar Jan 14 '13 01:01 JoshZA