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:
- Script support for tags
- Member functions for CF data type/data structure
- Improved JSON serialization
- Easy to use CF functions for Query tag
- Elvis operator (?:)
- Promoting built-in CF function to first class
- Miscellaneous new functions: QueryGetRow, ListEach and others.
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:
- Data type preservation for Query and CFC
- Case preservation of struct keys
- Added “Struct” as new QueryFormat
- Custom serializer
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.