would be nice to force 'types'

39 views
Skip to first unread message

eugene

unread,
Sep 29, 2009, 7:46:33 PM9/29/09
to json-taglib

Sorry if this is the wrong place to request features, but one annoying
thing about the taglib is that it outputs numbers as 'strings'.
Sometimes I really want the output to be a json number data type, not
a quote delimited string.

So instead of:
<json:property name="price">${product.price}</json:property> -->
"price" : "392.12"

I want:
<json:property name="price" type="number">${product.price}</
json:property> --> "price" : 392.12

Anyone else?

James Wiltshire

unread,
Oct 19, 2009, 12:41:36 PM10/19/09
to json-taglib
Hi
You should be able to achieve that if you use the value="" attribute
rather than setting the value within the body of the <json:property>
tag.
So instead of
<json:property name="price">${product.price}</json:property>
try
<json:property name="price" value="${product.price}"/>

When you specify the value like this then the type returned by the EL
expression should be a Java numeric type (int, float) which in turn
will be treated as a JavaScript numeric value when rendering the JSON.
When specifying the value within the body of the tag any value is
always treated as a String.

Here is an example from the taglib test suite...

<%-- Primitive values test --%>
<json:object>
<json:property name="boolT" value="${true}"/>
<json:property name="boolF" value="${false}"/>
<json:property name="string1" value="true"/>
<json:property name="string2" value="false"/>
<json:property name="numeric1" value="${1+2}"/>
<json:property name="numeric2" value="${-500}"/>
<json:property name="numeric3" value="${123.456}"/>
</json:object>

<%-- Expected
{
"boolT":true,
"boolF":false,
"string1":"true",
"string2":"false",
"numeric1":3,
"numeric2":-500
"numeric3":123.456
}
--%>

Regards
James Wiltshire

eugene

unread,
Oct 20, 2009, 6:41:02 AM10/20/09
to json-taglib

Terrific -- thanks for the response.

On Oct 19, 9:41 am, James Wiltshire <jameswiltshir...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages