tracker issue : CF-4194160

select a category, or use search below
(searches all categories and all time range)
Title:

SerializeJSON does not preserver datatype

| View in Tracker

Status/Resolution/Reason: Closed/Fixed/

Reporter/Name(from Bugbase): John Whish / John Whish (John Whish)

Created: 09/29/2016

Components: Functions

Versions: 2016

Failure Type: Data Corruption

Found In Build/Fixed In Build: CF2016_Update1 /

Priority/Frequency: Critical / Some users will encounter

Locale/System: English / Mac 10.9 64 bit

Vote Count: 0

Problem Description:

<cfscript>
x = [
    {"userid":"123"},
    {"userid":"ABC"}
];
</cfscript>

writeDump(SerializeJSON(x));

returns:

[{"userid":123},{"userid":"ABC"}]

Note that the first userid is numeric, the second is a string. Expected:

[{"userid":"123"},{"userid":"ABC"}]

This also happens when iterating a query.

For example:

<cfscript>
function queryToArrayOfStructs(q) {
    var result = [];
    for (row in q) {
        var data = {};
        for (var key in row) {
            data[lcase(key)] = "" & row[key];
        }
        result.append(data);
    };
    return result;
}

// simulate a query
myQuery = queryNew("UserID,UserName,FirstName","varchar,varchar,varchar", [
    ["100", "Fred", "Bloggs"],
    ["ABC", "Jo", "Smith"]
]);


foo = queryToArrayOfStructs(myQuery);
writeDump(myQuery);
writeDump(serializeJSON(foo));
</cfscript>

Here it does not respect the original query column type of `varchar`. It also does not respect that the attempt to coerce it back to a string in the loop.

Any Workarounds:

None found.

----------------------------- Additional Watson Details -----------------------------

Watson Bug ID:	4194160

External Customer Info:
External Company:  
External Customer Name: John Whish
External Customer Email:  
External Test Config: My Hardware and Environment details:

Attachments:

Comments:

We have fixed this in ColdFusion 2016 HF2 as a part of the bug no CF-3337394(https://bugbase.adobe.com/index.cfm?event=bug&id=CF-3337394). Code with the relevant changes to fix the issue would be: <cfscript> x = [ {"userid":"123"}, {"userid":"ABC"} ]; metadata={ items:[ { userid:'string' } ] }; x.setmetadata(metadata); writeDump(SerializeJSON(x)); </cfscript> <cfscript> function queryToArrayOfStructs(q) { var result = []; for (row in q) { var data = {}; data.setmetadata({ userid:'string',username:'string',firstname:'string' }); for (var key in row) { data[lcase(key)] = "" & row[key]; } result.append(data); }; return result; } // simulate a query myQuery = queryNew("UserID,UserName,FirstName","varchar,varchar,varchar", [ ["100", "Fred", "Bloggs"], ["ABC", "Jo", "Smith"] ]); foo = queryToArrayOfStructs(myQuery); writeDump(myQuery); writeDump(serializeJSON(foo)); </cfscript>
Comment by Suchika S.
1716 | September 30, 2016 07:23:49 AM GMT
I'd say the fix you mention is a workaround rather than a fix, but good to know there is a solution :)
Comment by External U.
1717 | November 01, 2016 11:05:58 AM GMT