EL-friendly

65 views
Skip to first unread message

Per Lindberg

unread,
Apr 3, 2012, 5:20:06 AM4/3/12
to google-gson
I have been using the old simple JSONobject package where an object
extends HashMap and an array extends ArrayList. This is highly
convenient in JSP because elements can be accessed directly using EL,
e.g. ${car.numberOfDoors} or ${carModels["Volvo_V70"].numberOfDoors}
or ${soldCars[4711]} . The JSP code is easy to write and read.

Now I want to switch to gson. Unfortunately, this is not possible with
gson objects, as far as I can tell.

What would be The Right Thing to fix this? It would be great if gson
JsonElement could be made EL-friendly!

Inderjeet Singh

unread,
Apr 3, 2012, 1:13:09 PM4/3/12
to google-gson
Hi Per,

Can you tell us in a bit more detail what it would take to make Gson
EL-friendly?
JsonObject already provides a few getters, those are not good enough?

Inder

Per Lindberg

unread,
Apr 11, 2012, 10:09:47 AM4/11/12
to google-gson
Nope, the getters are not enough. See the relevant JSRs for EL.

In short (i'm not an expert), I think that the gson JsonObject has to
imeplement Map, and JsonArray has to implement List (or perhaps
ListArray). But I'm guessing. If you have a copy of 'Head First
Servlets and JSP', turn to page 368-370. And see the relevant JSRs.

Create a JSP, add a scriptlet where you make a gson JsonObject
structure, and then try the EL access examples in my original post.

Per Lindberg

unread,
Jan 15, 2013, 10:57:09 AM1/15/13
to googl...@googlegroups.com
Well. Since my suggestion hasn't been implemented, I'm now throwing out Gson.
It has some nice features, but this deficiency renders it useless in a JSP/JSTL environment, so any nice features become irrelevant.


Maaartin

unread,
Jan 15, 2013, 7:41:16 PM1/15/13
to googl...@googlegroups.com
On Tuesday, January 15, 2013 4:57:09 PM UTC+1, Per Lindberg wrote:
Well. Since my suggestion hasn't been implemented, I'm now throwing out Gson.
It has some nice features, but this deficiency renders it useless in a JSP/JSTL environment, so any nice features become irrelevant.

You could
1. Create a wrapper  implementing `List` / `Map`. Not nice, but easy.
2. Create a wrapper accessing `JsonArray.elements` / `JsonObject.members` via reflection. Even less nice (could break one day), but even easier.
3. Patch gson. This is more work as both mentioned classes already extend `JsonElement`, so you can't use `AbstractMap` / `AbstractList`.

I wonder if there's a good reason for JsonArray/JsonObject not to implement the interfaces.

Reply all
Reply to author
Forward
0 new messages