tracker issue : CF-4198326

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

[ANeff] ER for: DeserializeJSON() support for .toJSON()'s serializeQueryByColumns="struct"

| View in Tracker

Status/Resolution/Reason: To Fix//BugVerified

Reporter/Name(from Bugbase): Aaron Neff / Aaron Neff ()

Created: 02/08/2017

Components: Language, Serialization

Versions: 2016

Failure Type: Others

Found In Build/Fixed In Build: CF2016 HF3 /

Priority/Frequency: Normal /

Locale/System: /

Vote Count: 0

This ER is for DeserializeJSON(q, "query"), to support .toJSON()'s serializeQueryByColumns="struct"

Repro:

<cfscript>
  q = queryNew("col1,col2,col3", "varchar,double,integer", [[1,1.0,1],[2,2.0,2]]);
  j = q.toJSON("struct", false);//returns [{"COL1":"1","COL2":1.0,"COL3":1},{"COL1":"2","COL2":2.0,"COL3":2}]
  q2 = deserializeJSON(j, false);
  writeDump(q2);
</cfscript>

Actual Result: an array of structs (understandable.. but.. we need a way to force an array-of-structs to query..)

Suggestion:

<cfscript>
  q = queryNew("col1,col2,col3", "varchar,double,integer", [[1,1.0,1],[2,2.0,2]]);
  j = q.toJSON("struct", false);//returns [{"COL1":"1","COL2":1.0,"COL3":1},{"COL1":"2","COL2":2.0,"COL3":2}]
  q2 = deserializeJSON(j, "query");
  writeDump(q2);
</cfscript>

Actual Result: exception

Expected Result: If input is an array of structs, then return a query. Otherwise, throw an exception.

Attachments:

Comments:

To illustrate the ER, here is a wrapper around deserializeJSON() that adds support for .toJSON()'s serializeQueryByColumns='struct': <cfscript> function udfDeserializeJSON(required string JSONVar, string strictMapping=true, boolean useCustomSerializer=true) output=false hint="A wrapper around deserializeJSON() that adds support for .toJSON()'s serializeQueryByColumns='struct'" { switch(ARGUMENTS.strictMapping) { case "query": { if(isJSON(ARGUMENTS.JSONVar) && (ARGUMENTS.strictMapping is "query")) { var queryRows = deserializeJSON(ARGUMENTS.JSONVar); if(isArray(queryRows) && queryRows.len() && isStruct(queryRows[1])) { //Begin determining query column names var queryColumns = queryRows.reduce(function(queryColumns, queryRow) { if(isStruct(queryRow)) { var possibleColumns = ARGUMENTS.queryRow.keyArray(); var newColumns = possibleColumns.filter(function(possibleColumnName) { return !queryColumns.findNoCase(ARGUMENTS.possibleColumnName); }); return ARGUMENTS.queryColumns.append(newColumns, true); } else { throw("When strictMapping is 'query', the JSON must be an array of structs"); } }, []); //End determining query column names return queryNew(queryColumns.toList(), "", queryRows); } else { throw("When strictMapping is 'query', the JSON must be an array of structs"); } } break; } default: { return deserializeJSON(ARGUMENTS.JSONVar, ARGUMENTS.strictMapping, ARGUMENTS.useCustomSerializer); } } } jsonString = '[{"COL1":"1","COL2":1.0},{"COL1":"2","COL2":2.0,"COL3":2}]';//an array of structs, for building query writeDump(udfDeserializeJSON(jsonString, "query")); </cfscript> If you pass-in the result from .toJSON()'s serializeQueryByColumns="struct", and set the 2nd parameter to "query", then it will return a query. That functionality is what I am requesting in this ER. Thanks!, -Aaron
Comment by Aaron N.
1210 | February 09, 2017 02:42:35 AM GMT