saghosh
10/02/2023, 10:06 AMzackster
10/02/2023, 1:42 PMsaghosh
10/02/2023, 2:53 PMAdam Cameron
<!--- Generate a clean feed by suppressing white space and debugging
information. --->
<cfprocessingdirective suppresswhitespace="yes">
<cfsetting showdebugoutput="no">
<!--- Generate the JSON feed as a JavaScript function. --->
<cfcontent type="application/x-javascript">
<cfscript>
// Construct a weather query with information on cities.
// To simplify the code, we use the same weather for all cities and days.
// Normally this information would come from a data source.
weatherQuery = QueryNew("City, Temp, Forecasts");
QueryAddRow(weatherQuery, 2);
theWeather=StructNew();
theWeather.High=73;
theWeather.Low=53;
theWeather.Weather="Partly Cloudy";
weatherArray=ArrayNew(1);
for (i=1; i<=5; i++) weatherArray[i]=theWeather;
querySetCell(weatherQuery, "City", "Newton", 1);
querySetCell(weatherQuery, "Temp", "65", 1);
querySetCell(weatherQuery, "ForeCasts", weatherArray, 1);
querySetCell(weatherQuery, "City", "San Jose", 2);
querySetCell(weatherQuery, "Temp", 75, 2);
querySetCell(weatherQuery, "ForeCasts", weatherArray, 2);
// Convert the query to JSON.
// The SerializeJSON function serializes a ColdFusion query into a JSON
// structure.
theJSON = SerializeJSON(weatherQuery);
// Wrap the JSON object in a JavaScript function call.
// This makes it easy to use it directly in JavaScript.
writeOutput("onLoad( "&theJSON&" )");
</cfscript>
</cfprocessingdirective>
Only line 30 of all that mess is relevant to serializeJson.
Show these variants:
• serialise a struct. Point out the key-case issue.
• serialize a query (but create the query with a single queryNew call!!), and show how the columns / rows are handled, for both variants of queryFormat
• serialize a datetime, and show the resultant format
• serialize null.
• demonstrate that beyond the value of a Long, a numeric is serialized as a string (TIL...)Adam Cameron
saghosh
10/02/2023, 4:01 PMAdam Cameron
Adam Cameron
Adam Cameron
saghosh
10/02/2023, 4:02 PMsaghosh
10/02/2023, 4:02 PMAdam Cameron
q = queryNew("id,value", "integer,varchar", [
[1, "one"],
[2, "two"],
[3, "three"]
])
writeOutput(serializeJson(q))
Demonstrating serializing a query. And follow up with the output and explain what they're looking at.Adam Cameron
queryFormat, and explain what that does. But keep the code as similar as poss between the two, to amplify the differences in the outputs.Adam Cameron
Adam Cameron