Option serializable?

119 views
Skip to first unread message

Roberto Leibman

unread,
May 18, 2012, 2:15:46 AM5/18/12
to Scala+GWT
Ok... still having the same problems with "ant s

But "ant devmode" seems to be working, so I can at least go on with
the things I'm trying to accomplish.
I'm getting stuff from a database using hibernate and java classes. I
then convert those java classes into scala clasess through implicit
converters (preeeeetty!!!)

So I have:
@Table
public class JavaFoo {
@Column
Integer myVar
}

public class ScalaFoo(var myVar:Int) {
}

implicit def JavaFoo2ScalaFoo(convertMe:JavaFoo):ScalaFoo {
new ScalaFoo(convertMe.myVar)
}

My first problem was that some of the java objects contained
java.lang.Integer data that was null (it's null in the database), this
causes a NullPointerException in Integer2int when trying to set it
into scala's fields.
So I did what seems to be the right thing, I declared those fields as
Option[Int]

public class ScalaFoo(var myVar:Option[Int]) {
}


and did:

implicit def JavaFoo2ScalaFoo(convertMe:JavaFoo):ScalaFoo {
new ScalaFoo(if (convertMe.getMyvar() == null) None else
Some(convertMe.getMyvar()))
}

The compiler likes it... but the problem is that Some (and I assume
None) is NOT Serializable! so gwt can't pass it to the client!

Very frustrating!

Any ideas?

Lex Spoon

unread,
May 18, 2012, 10:13:25 AM5/18/12
to scal...@googlegroups.com
On Fri, May 18, 2012 at 2:15 AM, Roberto Leibman <rob...@leibman.net> wrote:
>
> The compiler likes it... but the problem is that Some (and I assume
> None) is NOT Serializable! so gwt can't pass it to the client!
>
> Very frustrating!

Unfortunate.

Are you on any Scala lists that it would be good to raise that issue
on? Or perhaps post it as a bug report? I would think that Some and
None should be tagged as @serializable, which should in turn cause GWT
RPC to consider them serializable.

-Lex

Nate Bauernfeind

unread,
May 18, 2012, 10:21:08 AM5/18/12
to scal...@googlegroups.com
GWT uses an interface called "IsSerializable" which is GWT-only.

The best option I can recommend (pun intended ;)), is to roll your own. I know case classes can be serialized if all fields are labeled var and it extends IsSerializable. I'm not sure about case objects, but I can't think of any reasons why you would not be able to get it to serialize.

You can create implicit conversions from your SerializableOption to Option so that you don't have to support all of the awesome collection features like .foreach, .map, .flatMap, etc. in your own impl.

Scala-gwt is in a very nice place, in that the most frustrating thing is that the scala collection library is not serializable. Definitely frustrating, but it's waaayyy better than coding GWT in java =D.

Glen Marchesani

unread,
May 18, 2012, 10:54:26 AM5/18/12
to scal...@googlegroups.com
A bit off topic from how to get Option to serialize in gwt.  Unfortunately I don't have anything to directly help with that other than some wisdom/best practice on why we use "human readable serialization strategies and REST-like api/service structures instead...

We have always ended up migrating away from GWT's serialization and used our own json (or xml or yaml) based.  
  • having something human readable is very, very helpful for debugging
  • having something reachable via tools like curl/wget is very, very helpful for debugging
  • having something human readable AND reachable via tools like curl/wget is worth so much more than the sum of it's parts
    • have node 19 acting funny well you can get very fine grained into it's data structures with just curl and your own eyes
    • need to test out a service and the client isn't ready yet, you can plumb in a custom request with curl and a text editor
    • need to test out a client but the service isn't ready yet (then break out a text editor and create the response your service should see and stick it on a server as a static resource)
  • will you ever have to integrate the client/server with third party clients/servers?  We have other vendors/services wanting to plug into the various services and one can't hand a .net programmer any kind of reasonable layout on how to use the service (ie java and gwt serialization isn't an open standard).
  • Already have a rich service infrastructure and don't want to extend gwt's servlet's
I think you get the point.  I don't mean to go all hating on GWT serialization it has it's uses, in practice my team and I have found that for long lived apps we now just use json and REST from the get go.

What has me really excited is getting a library like lift's json library on the client.  Which makes accessing parts of a json document very simple and light (no need to create entire overlay types to get a single # deep in the graph). Note this covers the scala collections api nicely.  Case classes should compose nicely on the server and the client without getting into sticky spaghetti issues...

Anywho my 2 cents take it fwiw...

Eric Richardson

unread,
May 18, 2012, 12:35:04 PM5/18/12
to scal...@googlegroups.com
Hi All,

Our shop has be using SmartClient JS and more recently SmartGWT. We use the enterprise versions to serialize beans to JSON via SmartClients proprietary mechanism but I have to agree with Glen that going directly to rest services (JAX-RS if the backend is in Java) is a good solution. SmartGWT framework is a very nice Free API that is worth checking out versus using GWT's widgets. Their front end code has a very nice way to call REST and SOAP type web services with minimal fuss that updates the UI components with no client side data manipulation. SmartGWT is not as close to the metal as pure GWT but it really does allot for you out of the box and is LGPL licensed.

http://code.google.com/p/smartgwt/

I have not tried to use ScalaGWT with SmartGWT but I see not reason why it shouldn't work.

Eric

Roberto Leibman

unread,
May 18, 2012, 12:40:47 PM5/18/12
to Scala+GWT
Ok... I'm confused! I had said that Option wasn't serializable, but
upon further research, according to the scala documents, both Some and
Option are serializable:

http://www.scala-lang.org/api/current/scala/Some.html
http://www.scala-lang.org/api/current/scala/Option.html

And yet, when I try it out, I get the following exception

om.google.gwt.user.client.rpc.SerializationException: Type
'scala.Some' was not included in the set of types which can be
serialized by this SerializationPolicy or its Class object could not
be loaded. For security purposes, this type will not be serialized.:
instance = Some(10800)

I had originally interpreted to mean that Some wasn't serializable,
but apparently it is

Sorry for the confusion, I'm trying to learn too many things at the
same time and it all gets a bit jumbled.

So, what's this SerializationPolicy and what do we need to do to get
it to work?

Nate Bauernfeind

unread,
May 18, 2012, 12:44:35 PM5/18/12
to scal...@googlegroups.com
GWT looks for implementation of the IsSerializable interface, not java serializable.

Roberto Leibman

unread,
May 18, 2012, 1:18:53 PM5/18/12
to Scala+GWT
On May 18, 9:44 am, Nate Bauernfeind <nate.bauernfe...@gmail.com>
wrote:
> GWT looks for implementation of the IsSerializable interface, not java
> serializable.

Not according to this:

https://developers.google.com/web-toolkit/doc/latest/tutorial/RPC#serialize

Nate Bauernfeind

unread,
May 18, 2012, 2:21:12 PM5/18/12
to scal...@googlegroups.com
Ahh, you're right. Then the reason it fails is because the value of x in Some is final.

final case class Some[+A](x: A) extends Option[A] {
  def isEmpty = false
  def get = x

Clint Gilbert

unread,
May 18, 2012, 2:30:28 PM5/18/12
to scal...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

As I understand it, final fields are treated as transient by GWT-RPC's
serializer. Also note that for a class to be serializable, it also
needs a no-arg constructor. :( This constructor can be private, however.

On 05/18/2012 02:21 PM, Nate Bauernfeind wrote:
> Ahh, you're right. Then the reason it fails is because the value of
> x in Some is final.
>
> final case class Some[+A](x: A) extends Option[A] { def isEmpty =
> false def get = x }
>
> On Fri, May 18, 2012 at 12:18 PM, Roberto Leibman
> <rob...@leibman.net <mailto:rob...@leibman.net>> wrote:
>
> On May 18, 9:44 am, Nate Bauernfeind <nate.bauernfe...@gmail.com
> <mailto:nate.bauernfe...@gmail.com>> wrote:
>> GWT looks for implementation of the IsSerializable interface, not
>> java serializable.
>
> Not according to this:
>
> https://developers.google.com/web-toolkit/doc/latest/tutorial/RPC#serialize
>
>
>

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk+2lUQACgkQ5IyIbnMUeTuQuwCfe98ivOPN3LsRtFsitEQ6P09Q
MBYAniQe0NwfKfmlXjN0UCECaAJ6OPwt
=UiCq
-----END PGP SIGNATURE-----

Roberto Leibman

unread,
May 18, 2012, 2:42:07 PM5/18/12
to Scala+GWT
Super w00t!!!!!

I managed to make it work by adding the following class, I imagine
this could be added to the scala gwt library:

package com.google.gwt.user.client.rpc.core.scala

import com.google.gwt.user.client.rpc._

object Some_CustomFieldSerializer {
def hasCustomInstantiate() : Boolean = {true}
def deserialize[T<:Serializable](reader: SerializationStreamReader,
some: Some[T]): Unit = {
}
def serialize[T<:Serializable](writer: SerializationStreamWriter,
some: Some[T]): Unit = {
writer.writeObject(some)
}
def instantiate[T<:Serializable](reader:
SerializationStreamReader):Some[T] = {
reader.readObject().asInstanceOf[Some[T]]
}
}

class Some_CustomFieldSerializer[T<:Serializable] extends
CustomFieldSerializer[Some[T]] {
def deserializeInstance(reader: SerializationStreamReader, some:
Some[T]): Unit = {
}
def serializeInstance(writer: SerializationStreamWriter, some:
Some[T]): Unit = {
writer.writeObject(some)
}
override def instantiateInstance(reader:
SerializationStreamReader):Some[T] = {
reader.readObject().asInstanceOf[Some[T]]
}
override def hasCustomInstantiateInstance() : Boolean = {true}
}

Stephen Haberman

unread,
May 18, 2012, 2:42:51 PM5/18/12
to scal...@googlegroups.com, clint_...@hms.harvard.edu

> As I understand it, final fields are treated as transient by GWT-RPC's
> serializer. Also note that for a class to be serializable, it also
> needs a no-arg constructor. :( This constructor can be private,
> however.

True. There is a technically a pending-review patch to GWT that would
make final fields work, but you're still stuck with the no-arg
constructor.

I haven't been following this thread super closely, but I think your
best bet would be to write a custom field serialization. I haven't had
to write one of these myself yet, but it lets you side step the default
reflection-based serialization and instantiate the classes however you
need to.

There are quite a few examples of these in the GWT code base, typically
for the collection classes like java.util.ArrayList, HashMap, etc. E.g.
ArrayList_CustomFieldSerializer.

- Stephen

Lex Spoon

unread,
May 18, 2012, 4:02:17 PM5/18/12
to scal...@googlegroups.com
On Fri, May 18, 2012 at 12:44 PM, Nate Bauernfeind
<nate.bau...@gmail.com> wrote:
> GWT looks for implementation of the IsSerializable interface, not java
> serializable.

That's outdated. For several years now, GWT supports both the Java
Serializable interface and its own IsSerializable. Recommended
practice is to use the Java version.

Perhaps the annotation is not being read in by the Jribble reader?

Lex

Lex Spoon

unread,
May 18, 2012, 4:03:11 PM5/18/12
to scal...@googlegroups.com
I should have finished the thread before following up.

Yes, there are problems serializing case classes right now, which
would include Some and None...

Lex
Reply all
Reply to author
Forward
0 new messages