Skip to main content
Inspiring
January 14, 2022
Answered

CF2021 vs CF2016 JSON Response Data Types

  • January 14, 2022
  • 1 reply
  • 1066 views

Tried searching to see if anyone has ran across the following issue after upgrading to CF2021. 

 

In CF2016, it "seems" that CF is automatically converting data types based on the key values:

{"message":[],"code":"","success":true,"data":{"ID":15090910}}  Notice the ID is int

 

In CF2021, the data type for the same exact code and request comes back a string:

{"message":[],"code":"","success":true,"data":{"ID":"15090889"}} Notice the ID is a string

 

I've already fixed some that are supposed to be boolean (true/false) rather coming back as ("true" / "false")

 

Is there a new setting I'm not aware of?

    This topic has been closed for replies.
    Correct answer BKBK

     

    Is there a new setting I'm not aware of?


    By @--jojo--

    Yes. The setting this.serialization.structmetadata was later added to Application.cfc. For details see, for example, my comments in this previous forum thread on serializeJson.

     

    If you want "success" to be of boolean type and "ID" to be of integer type throughout the application, then use

     

     

    <!--- In Application.cfc --->
    <cfset this.serialization.structmetadata = {success:"boolean", ID:"integer">

     

     

    1 reply

    BKBK
    Community Expert
    BKBKCommunity ExpertCorrect answer
    Community Expert
    January 17, 2022

     

    Is there a new setting I'm not aware of?


    By @--jojo--

    Yes. The setting this.serialization.structmetadata was later added to Application.cfc. For details see, for example, my comments in this previous forum thread on serializeJson.

     

    If you want "success" to be of boolean type and "ID" to be of integer type throughout the application, then use

     

     

    <!--- In Application.cfc --->
    <cfset this.serialization.structmetadata = {success:"boolean", ID:"integer">

     

     

    --jojo--Author
    Inspiring
    January 19, 2022

    @BKBK - that's going to be a pain. Will it have to be just a structure of keys or will it have to match the exact structure of the object being serialized? (e.g. in my example above, ID is nested inside data)

    BKBK
    Community Expert
    Community Expert
    January 19, 2022

    Hi @--jojo-- ,

    I don't think you have to recreate the struct's tree in the setting. With x.y.z.myKey, just a mention of myKey will be sufficient. To be sure, test it yourself.

     

    For example, ensure the setting this.serialization.structmetadata is absent from your application. If it is present, comment it out.

     

    Run the following code as a CFM page

     

    <cfset x = {}>
    <cfset x.myVar1 = "1234">
    <cfset x.data.myVar2 = "5678">
    <cfset x.data.amount1 = "123.45">
    <cfset x.data.amount2 = "678.90">
    <cfset x.myBool = 'true'>
    <cfset x.myStr2 = 'false'>
    
    <cfdump var="#SerializeJSON(x)#" >

     

     

    The result should be a JSON in which every key and every value is a string.

     

    Now, add the following line to Application.cfc:

     

    <cfset this.serialization.structmetadata=
      { myVar2:"integer", 
      	myBool:"boolean", 
      	amount2: "numeric"
      	}>

     

     

    Rerun the CFM page.

     

    In the resulting JSON the data types of myVar2, myBool and amount2 have changed, respectively, to integer, boolean and numeric. The keys have different locations in the tree structure of the struct.