Hi CF users and members of the community. I am rea...
# adobe
s
Hi CF users and members of the community. I am reaching to you for some help in identifying some real world use cases for serializeJSON in CF. I am intending to revamp the page, make it more user friendly, and replace the existing sampes with some solid, real-world samples. As a developer with deep CF expertise, how do you use serializeJSON? What are its typical use cases?
z
As Lucee is open source, you can see all out test cases, some of which arise from feature development and others are in response to bugs raised by users at the bottom of every page in lucee docs is a link to search our test cases on github https://docs.lucee.org/reference/functions/serializejson.html
s
Thank you @zackster I'll take a look. Thanks!
a
@saghosh most important to that site's examples is demonstrated in this example code for SerializeJSON:
Copy code
<!--- 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...)
there's a mishmash of example-by-narrative, and example-by-code in that doc, and seemingly no clear reason as to why one approach is taken over the other. It's a mess.
s
I agree Adam. Hence the effort to revamp this.
a
You don't need or want "real-world" examples. Demonstrate the variants of the behaviour
(oh yeah and to be clear, I'm not having a go at you)
But make the code as clear and as succinct as possible.
s
What I meant by real-world is that the examples should be clear enough for CF devs, but not too simplistic or trivial.
Thanks a ton Adam.
a
Copy code
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.
👍 2
then use the variant leveraging
queryFormat
, and explain what that does. But keep the code as similar as poss between the two, to amplify the differences in the outputs.
If you show me how a null is serialized, and a query is serialized and a struct is serialized... I will understand what to expect in my real-world scenario which has a struct containing a query which might have null values in it.
(how key order is [not] handled when serializing a struct would be something to demonstrate)
👍 2