April 3, 2014
Language Enhancements in ColdFusion Splendor – Improved JSON serialization 2
Comments
(7)
April 3, 2014
Language Enhancements in ColdFusion Splendor – Improved JSON serialization 2
Newbie 8 posts
Followers: 0 people
(7)

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 rowcolumn, 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.

  1. canSerialize
  2. serialize
  3. canDeserialize
  4. deserialize
 2. Register the handler:  Handler needs to be registered by adding its entry to application.cfc as following:
Application.cfc
<cfset this.customSerializer="MyCustomSerializer" />

This custom serializer has been explained in great details in ColdFusion Documentation

 

7 Comments
2014-05-17 02:05:19
2014-05-17 02:05:19

Like
2014-04-03 19:56:06
2014-04-03 19:56:06

Thanks for your feedback. Have asked concerned person to rework the example code.

Like
2014-04-03 16:15:14
2014-04-03 16:15:14

Why do the functions in the example have access=”Remote”
That sounds like a really bad idea.

Like
2014-04-03 12:06:01
2014-04-03 12:06:01

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.

Like
2014-04-03 06:59:24
2014-04-03 06:59:24

@peter & @adam,

I have cleaned up that code example. If I had more time I would have done more.

Like
2014-04-03 05:57:19
2014-04-03 05:57:19

And – @Peter – I note that that is only the highlights.

That code is appalling.

How can anyone take you guys – Adobe – seriously as the caretakers of the CFML language if you have *no clue* how to write CFML? Serious question.


Adam

Like
2014-04-03 05:22:37
2014-04-03 05:22:37

From the linked documentation:


<cfset var result = result & "”>

<cfset var result = result & "”>

WOW! That really is terrible!

Why the hell doesn’t the CF team get someone who actually has a clue about CFML to write code examples!

Like
Add Comment