Skip to content
Chris Phillips edited this page Jan 25, 2019 · 3 revisions

Why use components with properties for API request/response in CFML?

Documentation

If you create a set of related components and properties, you can navigate that code later to determine the expected shape of both requests and responses. This can include hints or comments on the components and the properties. Anyone that works on the code in the future (including you) will appreciate the documentation.

JSON Serialization

If you use standard structs and arrays in CF, the values are mostly type-less. Meaning SerializeJson will often decide to add or remove double-quotes, express things it thinks are numbers in scientific notation, express things it assumes are booleans (but aren't) as unquoted JSON booleans. There are lots of issues with SerializeJson. You are probably already aware of them.

We have found that if you use nested components with properties of correct specified types, you will get much cleaner, more consistent and predictable serialization.

Why did you make cfc_loader?

Where I work we have been using components for a while that are nested types with component properties, etc. We were loading them with a system where all objects extend a BaseValueObject that contains a loadFromStruct() method that dynamically and recursively loads the component and all of its children. But, when we started using them for APIs that respond with deeply nested types, it got very, very slow.

I created cfc_loader out of necessity. An example large payload was taking 1,300 milliseconds to populate a response component (containing thousands of deeply nested components and values). By writing a system that generates clean efficient code and converting to simple, non-extended CFCs, I was able to shave about 95% of that time off. Now, that "worst case" example takes about 60 milliseconds.

What if it throws an error?

The "component loaders" that are generated are straightforward, readable code. The stacktrace should make it clear which component loader the error occurred in. And it should be easy to read the code and determine the cause of the error. If this happens to you, please log an issue against the repo and include the generated loader (and example data) as an attachment.

Clone this wiki locally