[2.0] toJson infinite recursion

2,178 views
Skip to first unread message

drakkan

unread,
Apr 1, 2012, 6:50:59 AM4/1/12
to play-framework
Hi,

I have classical user and group models with a many to many relation.
I'm using ebean.

If I do something like this.

ObjectNode result = Json.newObject();
List<User> users =
User.finder.fetch("userGroups.group","id,name").findList();
result.put("Users",Json.toJson(users));

I get:

[RuntimeException: java.lang.IllegalArgumentException: Infinite
recursion (StackOverflowError) (through reference chain:
models.User["userGroups"]->com.avaje.ebean.common.BeanList[0]-
>models.UserGroup["user"]->models.User["userGroups"]-
>com.avaje.ebean.common.BeanList[0]->models.UserGroup["user"]-> ......

if instead I define a List< Map<String,Object> > and populate it
iterating over the Users, toJson works fine, is this the intended
behaviour or is there a bug in toJson?

thanks
drakkan

Guillaume Bort

unread,
Apr 2, 2012, 7:49:40 AM4/2/12
to play-fr...@googlegroups.com
You have cycle in your object graph (user point to group that point to
user). That's why it can't be automatically serialized to JSON.

> --
> You received this message because you are subscribed to the Google Groups "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>

--
Guillaume Bort

Marcos Pereira

unread,
Apr 2, 2012, 10:44:43 PM4/2/12
to play-fr...@googlegroups.com
As far as I know, XStream json mappers can handle cycles. So, maybe you can create your own json serializer to handle your object graph.


Ben McCann

unread,
Apr 4, 2012, 1:44:01 PM4/4/12
to play-fr...@googlegroups.com
Play uses Jackson 1.  Jackson 2 can handle cycles.  You might look at upgrading Play or using your own custom serialization that's based off of Play's.  The main difference between Jackson 1 and Jackson 2 is that the package name has changed, which is pretty easy to update.
> To post to this group, send email to play-framework@googlegroups.com.
> To unsubscribe from this group, send email to play-framework+unsubscribe@googlegroups.com.

> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>



--
Guillaume Bort

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-framework@googlegroups.com.
To unsubscribe from this group, send email to play-framework+unsubscribe@googlegroups.com.

gaurav

unread,
Jul 22, 2012, 1:48:02 AM7/22/12
to play-fr...@googlegroups.com
Sorry for being little dense. I am having exactly the same issue. How do I use Jackson 2 libraries instead Jackson 1. Just add the associated libs in the lib folder? Also any particular reason why play uses jackson 1 vs 2?

Ben McCann

unread,
Jul 22, 2012, 3:53:45 AM7/22/12
to play-fr...@googlegroups.com
I don't think it's as simple as that.  You'd probably have to submit a pull request to upgrade Play.


To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/NBw_PA3Vc4IJ.

To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.

Vincent Buzzano

unread,
Jan 7, 2013, 12:44:47 PM1/7/13
to Play! Framework
I try to use FlexJson to serialize a Model, but I got all public field twice... example:

@Entity
public class Person extend Model{
    public String name
    ...
}

in my controller I do

Person p = new Person();
p.name = "My name";

JSONSerializer serializer = new JSONSerializer()
  .include("name")
  .exclude("*")
  .prettyPrint(true);

serializer.serialize(p)

I get the result:

{
  "name": "My name",
  "name": "My name"
}
How do you deal with this ?


--
Vincent Buzzano
vincent...@gmail.com


2012/10/22 tru3d3v <tru3...@gmail.com>
Hello Guys, i have already solve this problem just like this.

List<ExtjsDevObjects> obj = Ebean.find(ExtjsDevObjects.class).select("application,version,instance").
findList();
JSONSerializer postDetailsSerializer = new JSONSerializer().include(
                "application",
                "version","instance.instance").exclude("*");

return ok(postDetailsSerializer.serialize(obj));

verry awesome, its so simple...i am using flexjson:

you can add jar file with add this code to file Build.scala 



val appDependencies = Seq(

      // Add your project dependencies here,

//      "mysql" % "mysql-connector-java" % "5.1.21",

        "net.sf.flexjson" % "flexjson" % "2.1"   )

Good Luck :)
--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/ThVyb9lvWjoJ.

Vincent Buzzano

unread,
Jan 7, 2013, 12:49:14 PM1/7/13
to Play! Framework
Sorry about the last mail.. I was testing Version 2.0 and it 's work with version 2.1

--
Vincent Buzzano
vincent...@gmail.com


2013/1/7 Vincent Buzzano <vincent...@gmail.com>

Rupanjan Baidya

unread,
Sep 10, 2014, 4:26:51 PM9/10/14
to play-fr...@googlegroups.com
tru3d3v,


I was facing the same problem, your fix worked for me also .. thanks alot.

Thanks,
Rupanjan

On Monday, October 22, 2012 11:22:22 AM UTC+5:30, tru3d3v wrote:
Hello Guys, i have already solve this problem just like this.

List<ExtjsDevObjects> obj = Ebean.find(ExtjsDevObjects.class).select("application,version,instance").
findList();
JSONSerializer postDetailsSerializer = new JSONSerializer().include(
                "application",
                "version","instance.instance").exclude("*");

return ok(postDetailsSerializer.serialize(obj));

verry awesome, its so simple...i am using flexjson:

you can add jar file with add this code to file Build.scala 



val appDependencies = Seq(

      // Add your project dependencies here,

//      "mysql" % "mysql-connector-java" % "5.1.21",

        "net.sf.flexjson" % "flexjson" % "2.1"   )

Good Luck :)

On Sunday, April 1, 2012 5:50:59 PM UTC+7, drakkan wrote:

tru3...@gmail.com

unread,
Sep 10, 2014, 9:43:59 PM9/10/14
to play-fr...@googlegroups.com
Y'r welcome Rupanjan :)
Sent from my BlackBerry®
powered by Sinyal Kuat INDOSAT

From: Rupanjan Baidya <baidya....@gmail.com>
Date: Wed, 10 Sep 2014 13:26:50 -0700 (PDT)
Subject: [play-framework] Re: [2.0] toJson infinite recursion
--
You received this message because you are subscribed to a topic in the Google Groups "play-framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/play-framework/_e-Dny5Wtzs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages