Matthew J. Clemente

TIL: objectSave() and objectLoad() for Complex Data Storage, Transport, and Manipulation

Oct 01, 2021
4 minutes

This should be a short post, on a pair of complimentary functions that I just learned: objectSave and objectLoad. I'm not sure I've fully grasped their practical utility yet, but they're certainly worth knowing.

The Backstory

I stumbled across objectSave while reading the code of Brian Love's function to hash any ColdFusion type; it generates unique identifiers for CFML variables by breaking them down into simple values and then hashing the result.[1] The code that caught my eye was:

//serialize ColdFusion objects
if (IsObject(arguments.value)) {
return hashify(value=ObjectSave(arguments.value));
}

I had never seen objectSave before, which got me curious.

Docs and Definitions

So what exactly does objectSave do? According to CFDocs.org, it "Serializes an object to file or converts it to binary format". Well, I'm going to be honest, that didn't help me a lot, because I didn't actually know what "serialize" meant.

Serialization

While I use serializeJSON and deserializeJSON all the time, I've never actually had a firm grasp on what was meant by "serialize". I knew it had something to do with converting CFML objects to/from JSON, but honestly, I had a hard time even remembering which function did which.

The definition of objectSave finally prompted me to look up serialize and, wonderfully, things make a bit more sense now. As Wikipedia explains it, serialization is the process of converting data into a format that can be stored or transmitted (and then possibly be restored at a later point). It finally clicked for me - serializeJSON is just awkwardly named - it's serializing (converting) to JSON. (Making the definition of "serialize" the second thing I learned today.)

What objectSave Does

With my ignorance conquered, the definition of objectSave also made more sense - objectSave converts Coldfusion objects (arrays, structures, queries, CFCs, etc.) to a binary format, enabling them to be more easily stored or transmitted. The ability to store serialized objects is built into objectSave, as can be seen in its method signature:

objectSave( object, filepath );

When the optional filepath argument is provided, the binary object is saved to that location. You can then use objectLoad to retrieve the serialized data, restoring the ColdFusion object that was saved.[2]

Usage Examples

The Adobe docs for objectSave provide a detailed explanation of how it can be used, so I'll quote it here in full:

This function is useful for handling dynamic data that has a relatively long period of usefulness and takes substantial time or resources to obtain. It lets you save the data in a file and use it in multiple application instances. For example, you can create a CFC that stores a query that takes long time to run and retrieves infrequently updated data. If you use the ObjectSave function to initially save the CFC as a file, and deserialize the CFC file on future application starts, you can improve application performance.

To be clear, I've never used this approach for optimizing application startups; I didn't even know it was possible. But I am intrigued by it, and interested in hearing if anyone has used it, and how it's worked.

A second usage of objectSave can be found in the hashify function code, where it is part of a process that transforms complex objects into simple ones. There, objectSave first serializes data to binary, so that they can be transformed again, this time to base64. In this way, complex objects can be converted to simple strings. Here's an example of that process, converting a complex object into a simple one (and then back again):

books = queryNew(
"id,title",
"integer,varchar",
{"id":1,"title":"Where the Wild Things Are"});

// convert to binary
binary_books = objectSave(books);

// convert to string
simple_books = toBase64(binary_books);

// query object has been converted to a base64 string
writeDump(simple_books);

// string restored to binary
restored_binary_books = binaryDecode(simple_books,"base64");

// binary restored to query
restored_books = objectLoad(restored_binary_books);

writeDump(restored_books);

Curiosity and a search on Github lead me to find similar usage of objectSave and objectLoad in the ColdBox framework core, so it does seem to be a valid and practical use case.

Final Thoughts

I'm always happy to learn about another tool in the CFML toolbox. I'm not sure when or how I'm going to use objectSave and objectLoad, but knowledge, as they say, is power.

If you have thoughts on how these functions can be utilized, please let me know in the comments!


Footnotes

  1. If you're curious about the utility of this, he used it to generate unique identifiers for data being cached. ↩︎

  2. Objects don't need to be stored to files for objectLoad to deserialize them; it will also accept binary object variables. ↩︎