FORGEBOX Enterprise 🚀 - Take your ColdFusion (CFML) Development to Modern Times! Learn More...
Bens Json Serializer For Wheelsv0.1.7 CFWheels Plugins
Author: Brandon Shea
CFWheel use's serializeJSON when rendering content as JSON. The results vary based on the Coldfusion engine in use. In order to have more control of the resulting data, this plugin overrides core.renderWith() to use Ben Nadel's JsonSerializer instead of serializeJson().
Below, I've used several config files to define my mappings, which are called by an index file that initializes a serializer and uses those configs to define the mappings. This setup is not requried, but it simplifies the setup imo. If there is no serializer defined in params or application.wheels scope, a default one will be initialized for the serialization attempt.
<cfscript> writeLog("Initializing serializer mappings...");
include "./anys.cfm"; include "./booleans.cfm"; include "./dates.cfm"; include "./floats.cfm"; include "./integers.cfm"; include "./strings.cfm"; include "./excludes.cfm"; serializer = initBenSerializerDefault(); for (key in anys) serializer.asAny(key); for (key in booleans) serializer.asBoolean(key); for (key in dates) serializer.asDate(key); for (key in floats) serializer.asFloat(key); for (key in integers) serializer.asInteger(key); for (key in strings) serializer.asString(key); for (key in excludes) serializer.exclude(key); writeLog("Serializer mappings initialized.");
The keys are defined using an all-or-nothing approach. By that, I mean that the serializer doesn't care where it encounters a key - if it matches, it will be given the explicitly defined casing. So, if you want to use "id" in one place and "ID" in another place within the same data-structure, you're out of luck. Both keys will match "id" and will be given the same case.
This is primarily intended to be used to return data from a server-side API. As part of that use-case, some of my philosophy is baked into it. Namely, an API usually returns a top-level struct / hash-map that defines the API result. This is why the serialization process is driven by the name of keys.
box install cfwheels-bens-json-serializer