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

I started a series of blog posts on language enhancements and so far covered Script support for tags and Member functions. Today, I will be blogging about the new features added in ColdFusion Splendor to improve JSON serialization.
Before I dive into the details of JSON serialization enhancements, here is the
list of the language features:

JSON serialization was first introduced in ColdFusion 8 and we constantly worked on improving this feature continously over the past several ColdFusion versions.

The complete list of JSON-level enhancements added to ColdFusion Splendor are:

  1. Data type preservation for Query and CFC 
  2. Case preservation of struct keys
  3. Added “Struct” as new QueryFormat
  4. Custom serializer
Data type preservation for Query and CFC
It has always been a challenge to rightly identify the underlying data type of a CFC property or of a column data of a query object. Prior to Splendor, ColdFusion used to check whether a data type can be converted to a number or boolean. If it can be converted, ColdFusion will automatically convert the property value to number/boolean irrespective of whether the property values needs conversion. Starting from Splendor, the metadata of the query, retrieved from the underlying database, will be used to identify the right data type of a column and the column data will be serialized based on that. Similarly, for a CFC, the CFC metadata will be retrieved and used to detect the right data type of the CFC property to serialize into.
Entity.cfc
 <cfcomponent persistent="true" table="partners" ... >
     <cfproperty name='zipcode' type="string" />
</cfcomponent>
index.cfm
   <cfscript>
        t = createObject("component", "Entity");
        t.setData("01003");
        writeoutput(serializeJSON(t));
   </cfscript>  


CF10 output      : {“zipcode”: 1003}
Splendor output : {“zipcode”: “01003”}

In the above example, unlike in ColdFusion10, the zipcode value is serialized as a string  and not as a number. Though the value looks like a numeric one, it will not be serialized as a number rather the data type information is retrieved from the underlying CFC metadata and  will be used to serialize it. 

Case preservation of struct keys

Prior to Splendor, ColdFusion did not preserve the case of a struct key but it converted it to the uppercase. Starting from Splendor, ColdFusion will preserve the original case of the struct key in which it is defined. But in case if you want to go back to the old ColdFusion behavior, you can use either the application setting or ColdFusion Administrator setting to switch back to the old behavior.  The following code will help you understand how this behavior works in ColdFusion10 and now:

<cfscript>
   data = {};
   data.empName ="John";
   data.age = 21;
   writeoutput(serialize(data));
</cfscript> 

Output in ColdFusion10   : {‘EMPNAME’= ‘John’, ‘AGE’=21}
Output in Splendor          :  {’empName ‘= ‘John’, ‘age’=21}

To keep this post short, I will be blogging the remaining JSON features in a separate post. Please refer the ColdFusion Splender public beta documentation for more details on JSON enhancement.   


4 Comments
2014-04-03 10:52:57
2014-04-03 10:52:57

@Awdhesh, the bug I’m referring to is a bug introduced in CF10, so to get the fix for it, we have to upgrade to CF11? Sounds legit.

Like
2014-04-02 21:34:17
2014-04-02 21:34:17

@Scott,
Backporting JSON fixes to CF10 has not yet planned. But definitely will be a good addition to CF10 updater. Will let you know if something gets planned.

Like
2014-04-02 08:11:18
2014-04-02 08:11:18

Well out of all those enhancements, the best is the struct key case preservation. Finally..

Like
2014-04-02 05:38:01
2014-04-02 05:38:01

Are you going to be backporting some of these fixes to CF10? The fact that CF will serialize a = “+0”; to {a:+0} then is unable to deserialize it back into a struct, because +0 is not a valid number is ridiculous.

Like
Add Comment