Whatever message this page gives is out now! Go check it out!
SerializeJSON(data[, queryFormat[, useSecureJSONPrefix[, useCustomSerializer]]])Parameter | Description |
data | A ColdFusion data value or variable that represents one. |
queryFormat | This parameter can be a Boolean value that specifies how to serialize ColdFusion queries or a string with possible values "row", "column", or "struct". |
useSecureJSONPrefix | False by default. When Prefix Serialized JSON is enabled in the ColdFusion Administrator, then by default this function inserts the secure json prefix at the beginning of the json . |
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 using the default ColdFusion behavior. |
MonthName, DayNumber Year Hours:Minutes:SecondsElement | Description |
COLUMNS | An array of the names of the columns. |
DATA | A two-dimensional array, where:
|
{"COLUMNS":["CITY","STATE"],"DATA":[["Newton","MA"],["San Jose","CA"]]}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:
|
{"ROWCOUNT":2, "COLUMNS":["CITY","STATE"],"DATA":{"City":["Newton","San Jose"],"State":["MA","CA"]}}<cfscript>
data = {empName="James", age="26"};
serializedStr = serializeJSON(data);
writeoutput(serializedStr);
</cfscript>component
{
this.name='serializeJSON'
this.serialization.preservecaseforstructkey = true
}| Element | Description |
| COLUMNS | An array of the names of the columns. |
DATA | A two-dimensional array, where:
|
{"COLUMNS":["CITY","STATE"],"DATA":[["Newton","MA"],["San Jose","CA"]]}<cfscript>
myquery=QueryNew([
{"Id":101,"Name":"John Adams","Paid":FALSE},
{"Id":102,"Name":"Samuel Jackson","Paid":TRUE},
{"Id":103,"Name":"Jack Michaels","Paid":TRUE},
{"Id":104,"Name":"Tony Stark","Paid":FALSE}
])
//writeDump(myquery)
serializedQuery=serializeJSON(data=myquery,queryformat="row")
writeOutput(serializedQuery)
</cfscript>{"COLUMNS":["PAID","ID","NAME"],"DATA":[[false,101,"John Adams"],[true,102,"Samuel Jackson"],[true,103,"Jack Michaels"],[false,104,"Tony Stark"]]}<cfscript>
myquery=QueryNew([
{"Id":101,"Name":"John Adams","Paid":FALSE},
{"Id":102,"Name":"Samuel Jackson","Paid":TRUE},
{"Id":103,"Name":"Jack Michaels","Paid":TRUE},
{"Id":104,"Name":"Tony Stark","Paid":FALSE}
])
//writeDump(myquery)
serializedQuery=serializeJSON(data=myquery,queryformat="column")
writeOutput(serializedQuery)
</cfscript>{"ROWCOUNT":4,"COLUMNS":["PAID","ID","NAME"],"DATA":{"PAID":[false,true,true,false],"ID":[101,102,103,104],"NAME":["John Adams","Samuel Jackson","Jack Michaels","Tony Stark"]}}<cfscript>
myquery=QueryNew([
{"Id":101,"Name":"John Adams","Paid":FALSE},
{"Id":102,"Name":"Samuel Jackson","Paid":TRUE},
{"Id":103,"Name":"Jack Michaels","Paid":TRUE},
{"Id":104,"Name":"Tony Stark","Paid":FALSE}
])
//writeDump(myquery)
serializedQuery=serializeJSON(data=myquery,queryformat="struct")
writeOutput(serializedQuery)
</cfscript>[{"PAID":false,"ID":101,"NAME":"John Adams"},{"PAID":true,"ID":102,"NAME":"Samuel Jackson"},{"PAID":true,"ID":103,"NAME":"Jack Michaels"},{"PAID":false,"ID":104,"NAME":"Tony Stark"}]<cfscript>
currentdate = now()
datetimeobj = arrayNew(1)
datetimeobj[1] = currentdate
datetimeobj[2] = "8/11/2006"
datetimeobj[3] = CreateDate(2006,8,11)
datetimeobj[4] = CreateDateTime(2006,8,11,15,30,30)
datetimeobj[5] = CreateODBCDate(datetimeobj[4])
datetimeobj[5] = CreateODBCDateTime(datetimeobj[4])
// table in a loop
writeOutput('<table border="1">');
writeOutput('<tr><th>Input Date/Time value</th><th>JSON representation</th></tr>');
for (i=1;i<=arrayLen(datetimeobj);i++){
jsonstring = serializeJSON(datetimeobj[i])
writeOutput('<tr>')
writeOutput('<td>#datetimeobj[i]#</td>')
writeOutput('<td>#jsonstring#</td>')
writeOutput('</tr>')
}
// Close the table HTML
writeOutput('</table>')
</cfscript><cfscript>
response={}
response.result="Success"
response.error="Error"
//writeDump(response)
response.error=javacast("null","")
serializedResponse = serializeJSON(response)
writeOutput(serializedResponse)
</cfscript>{"error":null,"result":"Success"}component
{
this.name='serializeJSON';
this.serialization.preservecaseforstructkey = true
this.enableNullSupport = true
}<cfscript>
response={}
response.result="Success"
response.error="Error"
//writeDump(response)
//response.error=javacast("null","")
response.error=null
serializedResponse = serializeJSON(response)
writeOutput(serializedResponse)
</cfscript>{"error":null,"result":"Success"}<cfquery name="qry_Users" datasource="#DSN#">
select * from users
</cfquery>
<cfoutput>#SerializeJSON(qry_Users)#</cfoutput><br>Component accessors="true"
{
property string empName;
property numeric age;
property string dept;
}<cfscript>
emp = new Employee({empName="James", age=26, dept="000"});
writeOutput(SerializeJSON(emp));
</cfscript>this.serialization.serializeQueryAs = [row|column|struct][
{"colour":"red","id":1},
{"colour":"green","id":2},
{"colour":"blue","id":3}
]SerializeJSON( Object o, Object serializeQueryByColumns, boolean secure, boolean useCustomSerializer);Attribute | Description |
Type | The datatype of the struct key. |
Name | While serializing a key in a struct, instead of using the key name as JSON key, the specified name in this is used. |
Keys | A struct that contains metadata information for nested structs. |
Items | Array of datatypes when setting the metadata for serializing elements in arrays or arrays inside structs. |
Ignore | When “true”, ignores specified keys in struct . Default is “false”. |
<cfscript>
example = structnew();
example.firstname = "Yes";
example.lastname = "Man";
// Default serialization converting ctring Yes to true
writeoutput(SerializeJSON(example));
</cfscript>{"LASTNAME":"Man","FIRSTNAME":true}metadata = {firstname: "string"}};metadata = {firstname: {type: "string"}};<cfscript>
example = structnew();
example.firstname = "Yes";
example.lastname = "Man";
// changing the default serialization by specifying the type of "firstname" as string
metadata = {firstname: {type:"string"}};
example.setMetadata(metadata);
writeoutput(SerializeJSON(example));
</cfscript>{"LASTNAME":"Man","FIRSTNAME":"Yes"}<cfscript>
example = structnew();
example.firstname = "Yes";
example.lastname = "Man";
writeoutput("<b>After serialization</b>:");
// change the JSON key firstname to fname
metadata = {firstname: {type:"string",name:"fname"}};
example.setMetadata(metadata);
writeoutput(SerializeJSON(example));
</cfscript>{"LASTNAME":"Man","fname":"Yes"}<cfscript>
employee = structnew();
// define the struct key-value pairs
employee.firstname = "Yes";
employee.lastname = "Man";
// define a nested struct for the key address
employee.address = {"doorno": "148", "street":"10 Down Street", "country": "UK"};
metadata = {firstname: {type: "string", name: "fname"}, address: {keys:
// set the metadata for a key in the nested struct
{
"doorno": {type: "string", name: "DoorNo"},
"street": "string",
"country": "string"
}}};
employee.setmetadata(metadata);
writeoutput(SerializeJSON(employee));
</cfscript>{"LASTNAME":"Man","ADDRESS":{"country":"UK","DoorNo":"148","street":"10 Down Street"}," fname ":"Yes"}{"LASTNAME":"Man","ADDRESS":{"country":"UK","DoorNo":"148-a","street":"10 Down Street"}," fname ":"Yes"}<cfscript>
employee = structnew();
employee.firstname = "Yes";
employee.lastname = "Man";
employee.address = {"doorno": "148", "street":"10 Downing Street", "country": "UK"};
employee.address.setmetadata({"doorno": {type: "string", name: "DoorNo"},"street": "string","country": "string"});
// changing the default serialization by specifying the type of firstname as string and
// changing JSON key firstname to fname
metadata = {firstname: {type: "string", name: "fname"}};
employee.setmetadata(metadata);
writeoutput(SerializeJSON(employee));
</cfscript>{"LASTNAME":"Man","ADDRESS":{"country":"UK","DoorNo":"148","street":"10 Downing Street"}," fname ":"Yes"}{title: {type:"string",name:"title"}, tags:{items:"string",name:"keywords"}};<cfscript>
blogPost = structnew();
blogPost.Title = "Struct Serialization";
blogPost.referenceURL = "http://www.example.com";
// define an array for a struct key. In the array all elements are of type string, except 2016
blogPost.tags = ["struct", "json", "serialization", "2016", "HF2", "metadata"];
// specify all elements as string in the metadata
metadata = {title: {type: "string", name: "title"}, tags: {items: "string", name: "keywords"},referenceURL:{name:"url"}};
blogPost.setmetadata(metadata);
writeoutput(SerializeJSON(blogPost));
</cfscript><cfscript>
example = structnew();
example.firstname = "Yes";
example.lastname = "Man";
// define an array for a struct key. Elements are of different datatypes
example.inputs = ["2500.12", 4.0, "Yes", "False", "339090", {"q1": "Yes"}, ["1","2","3"]];
// set datatypes of first element as numeric, second element as integer, and so on
example.setmetadata({firstname: "string", inputs: {items: ["numeric", "integer", "string", "boolean", "string",
{q1: "boolean"}, {items: "string"}]}});
writeoutput(serializeJSON(example));
</cfscript>{"LASTNAME":"Man","FIRSTNAME":"Yes","INPUTS":[2500.12,4,"Yes",false,"339090",{"q1":true},["1","2","3"]]}{key:{ignore:true}}<cfscript>
employee = structnew();
employee.firstname = "Yes";
employee.lastname = "Man";
employee.salary = "100000";
employee.salarygrade = "D";
// ignore salary and salarygrade keys
profileView = {firstname: {type:"string",name:"fname"}, salary: {ignore: true}, salarygrade: {ignore: true}};
employee.setmetadata(profileView);
writeoutput(serializeJSON(employee));
</cfscript>{"LASTNAME":"Man","fname":"Yes"}<cfscript>
employee = structnew();
employee.firstname = "Yes";
employee.lastname = "Man";
employee.salary = "100000";
employee.salarygrade = "D";
profileView = {firstname: {type:"string",name:"fname"}, salary: {ignore: true}, salarygrade: {ignore: true}};
employee.setmetadata(profileView);
writedump(employee.getMetadata());
</cfscript><cfscript>
tags = ["struct", "json", "serialization", "2016", "HF2", "metadata"];
WriteOutput(serializejSON(tags));
</cfscript>["struct"," json ","serialization",2016,"HF2","metadata"]<cfscript>
tags = ["struct", "json", "serialization", "2016", "HF2", "metadata"];
tags.setmetadata({items: "string"});
writeoutput(serializejSON(tags));
</cfscript>["struct"," json ","serialization","2016","HF2","metadata"]myArray=["ColdFusion",2016,"true"];myArray.setMetadata({items:["string","integer","boolean"]});<cfscript>
inputs = ["2500.12", 4.0, "Yes", "False", "339090", {"q1": "Yes"}, ["1","2","3"]];
inputs.setmetadata({items: ["string", "integer", "string", "boolean", "string", {q1: "string"}, {items: "string"}]});
writeoutput(serializeJSON(inputs));
</cfscript>["2500.12",4,"Yes",false,"339090",{"q1":"Yes"},["1","2","3"]]<cfscript>
inputs = ["2500.12", 4.0, "Yes", "False", "339090", {"q1": "Yes"}, ["1","2","3"]];
inputs.setmetadata({items: ["string", "integer", "string", "boolean", "string", {q1: "string"}, {items: "string"}]});
writedump(inputs.getMetadata());
</cfscript>