SerializeJSON and datetime

Not that I know of with serializeJson() specifically, though you could always write your own custom serializer (see serialize Code Examples and CFML Documentation) and achieve what you’re after, but that would still require some rewrite.

The other option is to modify the Lucee source code for serializeJson() to have it format dates in a manner more to your liking.

All that said… any one of those formats will pass an isDate() check and would work as dates for further processing by ColdFusion thanks to data coercion. You didn’t identify the end point of the data, but assuming it is further internal processing then that format doesn’t pose a problem.

And, lastly, you mention passing queries through serializeJSON() which produces absolutely horrid results that are difficult to work with. Making another assumption here that you’re using those results… in which case best practice these days is to pass an array of structs built from the query results. Lots of ways to do that… in Lucee you can set a returnFormat to queryExecute() for example that returns an array of structs for you. There are also widely available query to array of struct functions floating around the interwebs, such as at cflib.org. Using any of those, you could format dates as you wish when looping over the query data before doing serializeJSON(). Generally, if I’m exposing the JSON to be consumed by the outside world, I format dates as a UTC ISO time string (e.g. 20210130T162210Z) so they are easily consumable by other tech and other timezones.

Again, all of this spells rewrite of some sort, somewhere. Options are limited and building and maintaining an independent version of Lucee for your own flavor of date formatting in SerializeJson() is another challenge in and of itself (or, build it once and forever be stuck with that version of Lucee).

Hard to speak to any specific pattern I might suggest given how little I know about the code you’re working with, but based on your question I’d say some rewrite would both solve the issue and probably be beneficiary to the project in the long run.

HTH

– Denny

1 Like