Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Scala renderJSON
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  14 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Vincent Buzzano  
View profile  
 More options Jul 15 2010, 11:52 am
From: Vincent Buzzano <vincent.buzz...@gmail.com>
Date: Thu, 15 Jul 2010 08:52:36 -0700 (PDT)
Local: Thurs, Jul 15 2010 11:52 am
Subject: Scala renderJSON
Hello,

I get a problem using Scala and trying to do a renderJSON on a list

I get all contacts and then try to render the list as JSON

> var contacts = asScala[Contact].findAll
> renderJSON(contacts)

I get this exception

Execution exception (In /app/controllers/Contacts.scala around line
48)
UnsupportedOperationException occured : Expecting parameterized type,
got class scala.collection.immutable.$colon$colon.  Are you missing
the use of TypeToken idiom?  See
http://sites.google.com/site/gson/gson-user-guide#TOC-Serializing-and...

play.exceptions.JavaExecutionException: Expecting parameterized type,
got class scala.collection.immutable.$colon$colon.
 Are you missing the use of TypeToken idiom?
 See http://sites.google.com/site/gson/gson-user-guide#TOC-Serializing-and...
        at play.mvc.ActionInvoker.invoke(ActionInvoker.java:259)
        at Invocation.HTTP Request(Play!)
Caused by: java.lang.UnsupportedOperationException: Expecting
parameterized type, got class scala.collection.immutable.$colon$colon.
 Are you missing the use of TypeToken idiom?
 See http://sites.google.com/site/gson/gson-user-guide#TOC-Serializing-and...
        at com.google.gson.TypeInfoFactory.getActualType(TypeInfoFactory.java:
97)
        at
com.google.gson.TypeInfoFactory.extractRealTypes(TypeInfoFactory.java:
116)
        at com.google.gson.TypeInfoFactory.getActualType(TypeInfoFactory.java:
65)
        at
com.google.gson.TypeInfoFactory.getTypeInfoForField(TypeInfoFactory.java:
54)
        at
com.google.gson.ObjectNavigator.navigateClassFields(ObjectNavigator.java:
148)
        at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:123)
        at
com.google.gson.JsonSerializationContextDefault.serialize(JsonSerialization ContextDefault.java:
56)
        at com.google.gson.Gson.toJsonTree(Gson.java:230)
        at com.google.gson.Gson.toJson(Gson.java:315)
        at com.google.gson.Gson.toJson(Gson.java:270)
        at com.google.gson.Gson.toJson(Gson.java:250)
        at play.mvc.results.RenderJson.<init>(RenderJson.java:20)
        at play.mvc.Controller.renderJSON(Controller.java:280)
        at play.mvc.ControllerDelegate.renderJSON(ControllerDelegate.java:58)
        at controllers.Contacts$.getContacts(/app/controllers/Contacts.scala:
48)
        at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:
374)
        at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:
357)
        at play.mvc.ActionInvoker.invoke(ActionInvoker.java:162)
        ... 1 more

it's work when i render only one contact

> var contacts = asScala[Contact].findAll
> renderJSON(contacts(0))

Do you have any idea ?

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Liu Yongjian  
View profile  
 More options Jul 15 2010, 12:08 pm
From: Liu Yongjian <ggd...@gmail.com>
Date: Fri, 16 Jul 2010 00:08:17 +0800
Local: Thurs, Jul 15 2010 12:08 pm
Subject: Re: [play-framework] Scala renderJSON

I have the same question about that .

According to the gson document , you need to define a TypeToken  if the
object that your are serializing/deserializing is a ParameterizedType (i.e.
contains at least one type parameter and may be an array) then you must use
the toJson(Object,
Type)<file:///E:/libary/google-gson-1.4-release/google-gson-1.4/gson-1.4-ja vadoc/com/google/gson/Gson.html#toJson(java.lang.Object,
java.lang.reflect.Type)> or fromJson(String,
Type)<file:///E:/libary/google-gson-1.4-release/google-gson-1.4/gson-1.4-ja vadoc/com/google/gson/Gson.html#fromJson(java.lang.String,
java.lang.reflect.Type)> method.

Here is an example for serializing and deserialing a ParameterizedType
picked from gson docs:

 Type listType = new TypeToken<List<String>>() {}.getType();
 List<String> target = new LinkedList<String>();
 target.add("blah");

 Gson gson = new Gson();
 String json = gson.toJson(target, listType);
 List<String> target2 = gson.fromJson(json, listType);

But it does not work properly in scala.collection.immutable.List, so
you can only use array instead - it seams the array in Scala is the
same as the one in Java

On Thu, Jul 15, 2010 at 11:52 PM, Vincent Buzzano <vincent.buzz...@gmail.com


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
dirk  
View profile  
 More options Dec 6 2010, 2:29 pm
From: dirk <australiandevelo...@gmail.com>
Date: Mon, 6 Dec 2010 16:29:21 -0300
Local: Mon, Dec 6 2010 2:29 pm
Subject: Re: [play-framework] Scala renderJSON

> UnsupportedOperationException occured : Expecting parameterized type,
> got class scala.collection.immutable.$colon$colon. Are you missing
> the use of TypeToken idiom?

Here's a solution to the problem using a custom serializer:

// Your action
def search(query: String) = Json(MySerializer.toJson(User.find("name like
?", query)))

object MySerializer {
    val builder = new GsonBuilder()

builder.registerTypeAdapter(classOf[scala.collection.immutable.$colon$colon [_]],
new CollectionSerializer)
    val gson = builder.create

    def toJson(obj: Any) = gson.toJson(obj)

}

// Custom serializer
//
http://sites.google.com/site/gson/gson-user-guide#TOC-Custom-Serializ...
class CollectionSerializer extends
JsonSerializer[scala.collection.immutable.$colon$colon[_]] {
    override def serialize(items:
scala.collection.immutable.$colon$colon[_], objType: Type, context:
JsonSerializationContext): JsonElement = {
        val json = new JsonArray()
        items.foreach(item => json.add(context.serialize(item)))
        return json
    }


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Loic  
View profile  
 More options Apr 12 2011, 9:09 am
From: Loic <loic.desco...@gmail.com>
Date: Tue, 12 Apr 2011 06:09:22 -0700 (PDT)
Local: Tues, Apr 12 2011 9:09 am
Subject: Re: [play-framework] Scala renderJSON

I have this  problem too, maybe we should open a bug


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dirk  
View profile  
 More options Apr 12 2011, 10:40 am
From: Dirk <dirkm...@gmail.com>
Date: Tue, 12 Apr 2011 10:40:08 -0400
Local: Tues, Apr 12 2011 10:40 am
Subject: Re: [play-framework] Scala renderJSON

I'm currently working on getting lift-json working with play/scala:
http://groups.google.com/group/play-framework/browse_thread/thread/dd...
http://groups.google.com/group/liftweb/browse_thread/thread/b6d5a0060...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Loic  
View profile  
 More options Apr 13 2011, 3:49 am
From: Loic <loic.desco...@gmail.com>
Date: Wed, 13 Apr 2011 00:49:23 -0700 (PDT)
Local: Wed, Apr 13 2011 3:49 am
Subject: Re: [play-framework] Scala renderJSON

There is a method for that in the play scala API, no need to use lift-json I
think.

According to the Scala module documentation, I guess this should work
without error :

def listByApi = Json(Contacts.findAll())

But it reponds an error :

{
    type:   'play.exceptions.JavaExecutionException',
    message: 'Expecting parameterized type, got class scala.collection.immutable.$colon$colon.
 Are you missing the use of TypeToken idiom?
 See http://sites.google.com/site/gson/gson-user-guide#TOC-Serializing-and...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Guillaume Bort  
View profile  
 More options Apr 13 2011, 3:54 am
From: Guillaume Bort <guillaume.b...@gmail.com>
Date: Wed, 13 Apr 2011 09:54:24 +0200
Local: Wed, Apr 13 2011 3:54 am
Subject: Re: [play-framework] Scala renderJSON

That why we need Lift-JSON. GSON is not able to serialize correctly scala
types.

--
Guillaume Bort, http://guillaume.bort.fr

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
v6ak  
View profile  
 More options Apr 13 2011, 4:23 pm
From: v6ak <v...@volny.cz>
Date: Wed, 13 Apr 2011 13:23:57 -0700 (PDT)
Local: Wed, Apr 13 2011 4:23 pm
Subject: Re: Scala renderJSON
It's simple:

import net.liftweb.json.ParameterNameReader
import java.lang.reflect.Constructor
import play.classloading.enhancers.LocalvariablesNamesEnhancer
import scala.collection.JavaConversions._

object PlayParameterReader extends ParameterNameReader{
        def lookupParameterNames(constructor: Constructor[_]) =
LocalvariablesNamesEnhancer.lookupParameterNames(constructor)

}

Then you need to use this paranamer instead of the default one in
Formats. There is one way that allows it:
https://gist.github.com/891235

Regards
Vít Šesták 'v6ak'

On Apr 12, 4:40 pm, Dirk <dirkm...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Guillaume Bort  
View profile  
 More options Apr 14 2011, 4:44 am
From: Guillaume Bort <guillaume.b...@gmail.com>
Date: Thu, 14 Apr 2011 10:44:50 +0200
Local: Thurs, Apr 14 2011 4:44 am
Subject: Re: [play-framework] Re: Scala renderJSON

We really need Lift-JSON working with Play Scala as GSON won't work for most
Scala specific structure. If you have a working version, please send us a
pull request.

--
Guillaume Bort, http://guillaume.bort.fr

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dirk  
View profile  
 More options Apr 15 2011, 12:33 pm
From: Dirk <dirkm...@gmail.com>
Date: Fri, 15 Apr 2011 13:33:10 -0300
Local: Fri, Apr 15 2011 12:33 pm
Subject: Re: [play-framework] Re: Scala renderJSON

I'm almost there, just trying to iron out a few remaining issues with
lift-json. For example it wasn't able to correctly serialize a model object
until yesterday:
http://groups.google.com/group/liftweb/browse_thread/thread/96d3bd870...

On Thu, Apr 14, 2011 at 5:44 AM, Guillaume Bort <guillaume.b...@gmail.com>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
v6ak  
View profile  
 More options Apr 16 2011, 12:49 pm
From: v6ak <v...@volny.cz>
Date: Sat, 16 Apr 2011 09:49:06 -0700 (PDT)
Local: Sat, Apr 16 2011 12:49 pm
Subject: Re: [play-framework] Re: Scala renderJSON

In fact, I don't have integrated it well. I just have solved the issue with
loading parameter names according to a dicsussion thread. The problem was
probably that Lift JSON expects serialized classes to be on classpath
(unless you provide a custom ParameterNameReader), but Play! uses a
different classloading mechanism. (This is possibly not true in the
production mode.)

I'm not sure about the right Play integration. Like any advanced
(de)serializer, Lift suports some configuration. (In fact, you can handle
polymorphism, of you configure it.) The configuration is described in an
instance of net.liftweb.json.Formats.

I've some ideas: https://gist.github.com/923233 (simple by default, complex
and robust on demand)

* Constructor parameter vs. method: Constructor parameter seems to be a
cleaner way for me.
* It is possible to use constructor overloading for convenience. So,
Controller can have constructors that accepts both of variants (either
net.liftweb.json.Formats or something like play.json.JsonAdapter).
* Combining availbility of constructor parameter and protected method seems
to be odd for me. Parameters could be overriden in such case by a trait. A
similar API design fail have been done in java.lang.Thread, which implements
java.lang.Runnable. Although there is a different issue (you can accidentaly
call run() instead of start()), both of these designs are crazy.
* Should Play! "defensively" force an instance of PlayParameterReader? There
are some advantages and disadvantages on both sides:
* * If Play! does not force the PlayParameterReader (or
PlayParameterNameReader), it can lead to some serialization issues (the
result of the serialization is "{}") if the user speciffies some non-default
Formats and does not choose to use PlayParameterReader.
* * If Play! forces the PlayParameterReader, it can lead to similar iussues
if user uses a specific ParameterNameReader on purpose. Fortunately, it is
an edge case. However, this seems to be a magic for me.
* * It is also possible to use one of these ParameterNameReader-s by default
and the other as a fallback, but there is still some magic.
* * It is also possible to allow user to switch overriding
ParameterNameReader off, which can be the best way.

What do you think about it?

Regards
Vít Šesták 'v6ak'


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dirk  
View profile  
 More options Apr 16 2011, 9:15 pm
From: Dirk <dirkm...@gmail.com>
Date: Sat, 16 Apr 2011 22:15:30 -0300
Local: Sat, Apr 16 2011 9:15 pm
Subject: Re: [play-framework] Re: Scala renderJSON

The reason they added the configuration option to use another parameter name
reader is because I asked for it so that lift-json could be used with play:
http://groups.google.com/group/liftweb/browse_thread/thread/b6d5a0060...
So we simply define a play ParameterNameReader that uses play's classloading
mechanism. I'll do this within the scala plugin itself so that play users
don't need to worry about it.
I'm currently working on it, but as I mentioned in my last post I'm just
waiting for them iron out a couple of problems with the serialization:
http://groups.google.com/group/liftweb/browse_thread/thread/96d3bd870...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
v6ak  
View profile  
 More options Apr 17 2011, 8:06 am
From: v6ak <v...@volny.cz>
Date: Sun, 17 Apr 2011 05:06:59 -0700 (PDT)
Local: Sun, Apr 17 2011 8:06 am
Subject: Re: [play-framework] Re: Scala renderJSON

RI understand that Play users don't need to worry about it in simple cases. However, one may need a custom Formats (for typeHinth etc.). In such case, the behavior is a tradeoff between simplicity and magic.

Regards,
Vít Šesták 'v6ak'


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dirk  
View profile  
 More options Apr 18 2011, 11:13 am
From: Dirk <dirkm...@gmail.com>
Date: Mon, 18 Apr 2011 12:13:00 -0300
Subject: Re: [play-framework] Re: Scala renderJSON

Yes you're right the developer should be allowed to override the default
behaviour. I will make the Play ParameterNameReader object public so that it
can be re-used. In the case that the developer wants to use a completely
different library, he can produce the required json as a string and pass
that to Json()


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »