Description
Converts ColdFusion data into a JSON (JavaScript Object Notation) representation of the data.
Returns
A string that contains a JSON representation of the parameter value.
Category
Syntax
SerializeJSON(var [, serializeQueryByColumns, useCustomSerializer]) |
See also
DeserializeJSON, IsJSON, cfajaxproxy, Using data interchange formats in the Developing ColdFusion Applications, http://www.json.org
History
ColdFusion 11: Added the attribute. useCustomSerializer.
ColdFusion 8: Added function
Parameters
Parameter |
Description |
---|---|
var |
A ColdFusion data value or variable that represents one. |
serializeQueryByColumns |
A Boolean value that specifies how to serialize ColdFusion queries.
|
useCustomSerializer | true/false. Whether to use the customSerializer or not. The default value is true. Note that the custom serialize will always be used for serialization. If false, the JSON serialization will be done |
Usage
This function is useful for generating JSON format data to be consumed by an Ajax application.The SerializeJSON function converts ColdFusion dates and times into strings that can be easily parsed by the JavaScript Date object. The strings have the following format:
MonthName, DayNumber Year Hours:Minutes:Seconds |
The SerializeJSON function converts the ColdFusion date time object for October 3, 2007 at 3:01 PM, for example, into the JSON string "October, 03 2007 15:01:00".The SerializeJSON function with a false serializeQueryByColumns parameter (the default) converts a ColdFusion query into a row-oriented JSON Object with the following elements:
Element |
Description |
---|---|
COLUMNS |
An array of the names of the columns. |
DATA |
A two-dimensional array, where:
|
For example, the SerializeJSON function with a serializeQueryByColumns parameter value of false converts a ColdFusion query with two columns, City, and State, and two rows of data into following format:
{"COLUMNS":["CITY","STATE"],"DATA":[["Newton","MA"],["San Jose","CA"]]} |
The SerializeJSON function with a serializeQueryByColumns parameter value of true converts a ColdFusion query into a column-oriented JSON Object that is equivalent to the WDDX query representation. The JSON Object has three elements:
Element |
Description |
---|---|
ROWCOUNT |
The number of rows in the query. |
COLUMNS |
An array of the names of the columns. |
DATA |
An Object with the following:
|
The SerializeJSON function with a serializeQueryByColumns parameter value of true converts a ColdFusion query with two columns, City, and State, and two rows of data into following format:
{"ROWCOUNT":2, "COLUMNS":["CITY","STATE"],"DATA":{"City":["Newton","San Jose"],"State":["MA","CA"]}} |
Note: The SerializeJSON function generates an error if you try to convert binary data into JSON format.
The SerializeJSON function converts all other ColdFusion data types to the corresponding JSON types. It converts structures to JSON Objects, arrays to JSON Arrays, numbers to JSON Numbers, and strings to JSON Strings.
Note: ColdFusion internally represents structure key names using all-uppercase characters, and, therefore, serializes the key names to all-uppercase JSON representations. Any JavaScript that handles JSON representations of ColdFusion structures must use all-uppercase structure key names, such as CITY or STATE. You also use the all-uppercase names COLUMNS and DATA as the keys for the two arrays that represent ColdFusion queries in JSON format.
This example creates a JSON-format data feed with simple weather data for two cities. The data feed is in the form of a JavaScript application that consists of a single function call that has a JSON Object as its parameter. The example code does the following:
- Creates a query object with two rows of weather data. Each row has a city, the current temperature, and an array of forecast structures, with each with the high, low, and weather prediction for one day. Normally, datasource provides the data; to keep the example simple, the example uses the same prediction for all cites and days.
- Converts the query to a JSON format string and surrounds it in a JavaScript function call.
Writes the result to the output.
If you view this page in your browser, you see the resulting JavaScript function and JSON parameter. To use the results of this page in an application, put this file and the example for the DeserializeJSON function in an appropriate location under your ColdFusion web root, replace the URL in the DeserializeJSON example code with the correct URL for this page, and run the DeserializeJSONexample.
<!--- Generate a clean feed by suppressing white space and debugging |
ColdFusion 11 has enhanced the JSON serialization to support the following new features:
- Case preservation of struct keys
- Data type preservation
- Key-value serialization of CF Query
- Custom serializers
Case preservation of struct keys
Currently, the cases for struct keys are not preserved in ColdFusion. The struct keys get converted to upper case automatically.
For instance, consider the following code:
<cfscript> |
In ColdFusion 10 and earlier vesions, the output generated by the above code will be:
{'EMPNAME'='', 'AGE'=''} |
For the release after ColdFusion 11, the output generated will be:
{'empName'='', 'age'=''} |
To enable case preservation of struct keys at the application level, modify the application.cfc file by setting:
this.serialization.preservecaseforstructkey = true |
Note: Key preservation do not work on keys in application, session, or request scopes.
The default behavior is true. To switch back to the old behavior, set this value to false.
To enable case preservation of struct keys at the server level, perform the following tasks:
- From the ColdFusion Administrator page, click Server Settings > Settings
- Click Preserve case for Struct keys for Serialization