In the previous post, I blogged about JSON serialization enhancements and covered a couple of
features around it. This post, a continuation from the last one, will cover the remaining features added in ColdFusion Splendor to serialize a ColdFusion object better. Before going into the details of the remaining improvements, here is the complete list of JSON features.
- Data type preservation for Query and CFC
- Case preservation of struct keys
- Added “Struct” as new QueryFormat
- Custom serializer
Added “Struct” as new QueryFormat
In ColdFusion, Row and column are the supported types to serialize a query object. However, none of these two formats are fit for the jQuery type JavaScript framework. These JavaScript frameworks work well with column-value (like struct) format and to facilitate easy integration with these frameworks, a new query format “struct” has been introduced in ColdFusion Splendor. So, starting from Splendor, 3 different format types row, column, and struct are now supported to serialize a query object to a JSON string. For example, to serialize a query object with 3 rows and 2 columns (colour,id), the serialized JSON string for row and struct query format will look like:
row format: {“ROWCOUNT”:3, “COLUMNS”:[“COLOUR”, “ID”], DATA:{“colour”: [“red”, “green”, “blue”], “id”: [1,2,3]}}
struct format: [{“colour”:”red”, “id”:1}, {“colour”:”green”, “id”:2}, {“colour”:”blue”, “id”:3}]
As you can notice, the struct format is based on the key-value pair and is quite simple. The only downside of this format is the repetition of the column names. Here, the column names are repeated for each and every row.
Custom serializer
ColdFusion Splendor has enhanced JSON serialization support to help you hook your own custom serializer for serializing a ColdFusion object. The custom serializer enables you to register your own handler for serializing and deserializing the complex object. If the serializer is not specified, ColdFusion will use the in-built serialization handler.
Steps to use the custom serializer: There are two steps involved to hook a custom serializer in ColdFusion.
1. Implement the custom serializer: The customSerializer handler, a CFC, has four functions for you to implement.
- canSerialize
- serialize
- canDeserialize
- deserialize
Application.cfc <cfset this.customSerializer="MyCustomSerializer" />
This custom serializer has been explained in great details in ColdFusion Documentation.
Looking at this example as well: within the serialize method it recursively calls serialize(arg, type) if type is neq “xml” or “json”. This will cause an infinite loop and is very poor coding logic.
I also have a problem with having a global serializationHandler for an application. When do you ever have an application that works with a one-size-fits-all method to contain your data, or that you only work with one custom data type.
You must be logged in to post a comment.