[2.0.3] Case problem in JSon conversion

218 views
Skip to first unread message

Xavier NOPRE

unread,
Oct 8, 2012, 11:17:56 AM10/8/12
to play-framework
Hi,

I have an object with the attribut "uForWin". 

In my controller, I have this code to return the object as JSon object for JavaScript use :

play.libs.Json.toJson(object)

In client slide, in JavaScript, the JSON object has 2 properties with the orinigal value of uForWin : uForWin and uforWin.

My object has other attribute and I don't have this problem for them. If I rename this attribute, I never have the problem. Seems to be linked to the single lowercase character at the beginning of the Java attribute.

Any idea or explanation ?

Thanks,

Xavier

James Roper

unread,
Oct 8, 2012, 11:39:18 PM10/8/12
to play-fr...@googlegroups.com
This has everything to do with the complexities of the Java bean naming conventions, and Jackson trying to guess what you want.  Play is simply using Jackson, so just annotate your property with @JsonProperty("uForWin") to explicitly map it.

Xavier NOPRE

unread,
Oct 9, 2012, 3:38:50 AM10/9/12
to play-fr...@googlegroups.com
Hi James,

Thank you for your answer and solution, but it doesn't solve my problem, and I don't understand why ... 
In unit test, with data from YAML file, I don't have the problem, only for object from database and only for this property ...

Thanks,

Xavier


2012/10/9 James Roper <jro...@gmail.com>
--
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/-/ej89mjmNCxEJ.
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.

James Roper

unread,
Oct 10, 2012, 12:15:00 AM10/10/12
to play-fr...@googlegroups.com
Ah, so this is a database object?  Play automatically generates getters and setters for database objects, and it seems this generation is clashing with Jacksons naming strategies.  To solve, write your own @JsonIgnored getter:

@JsonIgnore
getUForWin()

James Roper

unread,
Oct 10, 2012, 12:15:04 AM10/10/12
to play-fr...@googlegroups.com
Ah, so this is a database object?  Play automatically generates getters and setters for database objects, and it seems this generation is clashing with Jacksons naming strategies.  To solve, write your own @JsonIgnored getter:

@JsonIgnore
getUForWin()


On Tuesday, 9 October 2012 18:38:57 UTC+11, Xavier wrote:

Xavier NOPRE

unread,
Oct 10, 2012, 2:22:47 AM10/10/12
to play-fr...@googlegroups.com
Hi J

2012/10/10 James Roper <jro...@gmail.com>

Ah, so this is a database object?  Play automatically generates getters and setters for database objects, and it seems this generation is clashing with Jacksons naming strategies.  To solve, write your own @JsonIgnored getter:

@JsonIgnore
getUForWin()

My attribut is "public", I don't have any getter or setter .... :-(

Thanks,

Xavier

Yann Simon

unread,
Oct 10, 2012, 2:42:02 AM10/10/12
to play-fr...@googlegroups.com
2012/10/10 Xavier NOPRE <xno...@gmail.com>:
Play generates getters & setters for public attributes.
See http://www.playframework.org/documentation/2.0.4/JavaEbean for
more information, especially the following paragraph:

"Play has been designed to generate getter/setter automatically, to
ensure compatibility with libraries that expect them to be available
at runtime (ORM, Databinder, JSON Binder, etc). If Play detects any
user-written getter/setter in the Model, it will not generate
getter/setter in order to avoid any conflict."

Xavier NOPRE

unread,
Oct 10, 2012, 4:49:48 AM10/10/12
to play-fr...@googlegroups.com
Hi James,

Yes I known this generation by play for setter and getter.
But you suggested me to add "@JsonIgnore" to my object, thinking the naming problem comes from getter, but I don't have any getter ...

What the problem can be ?

Do you think I have to add getter + @JsonIgnore ?

Thanks,

Xavier



2012/10/10 Yann Simon <yann.s...@gmail.com>
--
You received this message because you are subscribed to the Google Groups "play-framework" group.

James Roper

unread,
Oct 11, 2012, 8:28:36 AM10/11/12
to play-fr...@googlegroups.com

Do you think I have to add getter + @JsonIgnore ?


Yes.  Play automatically generates a getter for you, it adds this to the compiled byte code, so you never actually see it, but it's there.  See:


And this is what Jackson is seeing, and this is what is adding that extra property, because Jackson is seeing both the Play generated getter, and your public field, and in Jackson's opinion they have slightly different names due to slightly different capitalisation strategies, and hence it puts both in the generated JSON.  What you want to do is to tell Jackson to ignore this getter, so what you can do is implement it yourself, and annotate it, then Play won't generate it.
Reply all
Reply to author
Forward
0 new messages