json - How do I ensure SerializeJSON keeps trailing/leading zeroes? -
edit 3 problem below exists coldfusion 9.0, updating 9.0.1 indeed fix this
i have application using serializejson encode query results:
#serializejson('ok works fine')#
unfortunately trims trailing zeroes numbers:
#serializejson(12345.50)#
manually if make same value string, same thing occurs
#serializejson('12345.50')#
how can prevent happening?
edit - scenario specifics
database (oracle) has these example values stored on row
- benefactor_id : 0000729789 varchar2(10)
- life_gift_credit_amt : 12345.50 number(14,2)
when query using coldfusion 9.0.1 (cfscript if matters) , here rc dump, notice id string retains leading zeroes, number column has removed trailing zero. while interesting, doesnt matter original issue can create query manually retain trailing 0 below, still gets lost in serializejson
i take query results, , encode values using serializejson. json consumed jquery datatables ajax. notice id string has become number, , has added '.0' miguel-f mentioned
<cfscript> ... rc.sql = q.setsql; rc.qresult = q.execute().getresult(); savecontent variable="rc.aadata" { (i=1; <= rc.qresult.recordcount; i++) { writeoutput('{'); (col=1; col <= icolumnslen; col++) { // following line contains conditional specific example writeoutput('"#acolumns[col]#":#serializejson(rc.qresult[acolumns[col]][i])#'); //former statement, discarded due not being able handle apostrophe's ... writeoutput('"#jsstringformat(rc.qresult[acolumns[col]][i])#"'); writeoutput((col neq icolumnslen) ? ',' : ''); } writeoutput('}'); writeoutput((i neq rc.qresult.recordcount) ? ',' : ''); } }; </cfscript>
i oringially using jsstringformat instead of serializejson, return invalid json due comments text area containing apostrophe's ect
{ "secho": 1, "itotalrecords": 65970, "itotaldisplayrecords": 7657, "aadata": [ { "nd_event_id": 525, "benefactor_id": 729789.0, "seq_number": 182163, "life_gift_credit_amt": 12345.5, "qty_requested": 2, "b_a_comment": "#swap", "pref_mail_name": "jay p. rizzi" } ] }
edit 2
a quick sidenote, if change serialization line
writeoutput('"#acolumns[col]#": "#serializejson(rc.qresult[acolumns[col]][i])#"');
then result set changes placing records in double quoting , double double quotes strings, while still removing trailing zero; leads me believe serializejson casting value type?
"aadata": [ { "nd_event_id": "525", "benefactor_id": "729789.0", "seq_number": "182163", "life_gift_credit_amt": "12345.5", "qty_requested": "2", "b_a_comment": ""#swap"", "pref_mail_name": ""jayp.rizzi"" },
taken comments
the original poster (op) of question reported having issue coldfusion 9.0.1. turned out running coldfusion 9.0.0. significant because adobe had made changes how serializejson()
function treats numbers in version 9.0.1. when server upgraded version 9.0.1 these issues resolved.
this blog post raymond camden discusses changes made in 9.0.1 - not happy cf901 json changes?
in blog post references bug 83638 had been entered , fixed in hotfix 1 version 9.0.1 - cumulative hotfix 1 (chf1) coldfusion 9.0.1
if search bugbase json under version 9.0.1 there several reporting same issue op.
those reported bugs mentioned issue op had not reported, .0
being appended integers well. later in discussion op confirmed seeing behavior. lead them verify coldfusion version being utilized , found not 9.0.1.
Comments
Post a Comment