Gabriel Villafuerte
Gabriel Villafuerte

Reputation: 67

Encoding problem between Coldfusion 11 and Coldfusion 2016

I currently use ColdFusion 11 as a local server for development.

I have a JSON string that is serialized with ColdFusion . Within the string I have to insert a code "99" in the object "Payment_form". The Api required in the response tells me that the Payment_form object be a string.

Knowing about problems with ColdFusion serialization with JSON string and the numerical representation, look here at StackOverflow and find a solution by placing a "chr(2)".

The problem was solved.... but only in ColdFusion 11. When I uploaded the code to the ColdFusion hosting (hostek.com/CF2016) it stopped working, because chr(2) returns the value "/u000299" and consequently the API responds with an error.

I assumed that ColdFusion 2016 changed the handling of chr(), but in the documentation of both versions there are no changes in the handling of this function.

It seems that CF/2016 is changing (Escaped Unicode) according to the ASCII table.

Image of representations of ASCII character 2

So?. Anyone know how to fix this?

I hope someone has some advice for this. Thank you

<cfset requestData = {"customer": "#customer#"
        , "items":#empStruct#
        , "payment_form":"#chr(2)#99"
        , "payment_method": "#payment_method#"
        , "use": "#use#"
        , "folio_number": "#folio_number#"
        , "series": "#series#"
    }>

Serliaze Exit Chain with Coldfusion 11 (local server My_pc)

 {
  "payment_form": "99",
  "series": "F",
  "use": "G03",
  "customer": "5d965ab97419177948b59a26",
  "payment_method": "PPD",
  "items": [
    {
      "quantity": 2,
      "product": "5d9639d27419177948b59a0b"
    },
    {
      "quantity": 15,
      "product": "5d9639e07419177948b59a0c"
    }
  ],
  "folio_number": 7400
}

Serialize Exit Chain with ColdFusion 2016 (Coldfusion Hosting Hostek.com)

 {
  "payment_form": "\\u000299",
  "series": "F",
  "use": "G03",
  "customer": "5d9691f17419177948b59a68",
  "payment_method": "PPD",
  "items": [
    {
      "quantity": 500,
      "product": "5d9d30bc08b8ad3f683e81bc"
    },
    {
      "quantity": 500,
      "product": "5d9d30c908b8ad3f683e81bd"
    }
  ],
  "folio_number": 7840
}

Upvotes: 3

Views: 150

Answers (1)

SOS
SOS

Reputation: 6550

TL;DR;

Instead of trying to "fix the fix", try using one of the newer serialization features instead

input = {"payment_form":"99"};
input.setMetadata( {payment_form: {type: "string"}} );
output = serializeJSON( input );

Using a different version in Development than Prod always leads to tears ... Take a look at CommandBox. It makes it extremely easy to use different versions of CF.

chr(2) returns the value "/u000299"

Anyway.. the serializeJSON() function is what's converting chr(2) into /u0002. However, this doesn't seem to be new behavior. CF 11 does the same thing. Instead of trying to "fix the fix", try using one of the newer serialization features instead:

Adobe ColdFusion (2016 release) Update 2 enables you to specify the datatype information for keys in a struct. This is known as metadata.

Explicitly declare the payment_form value as a string and CF will enclose it in quotes instead of guessing it's a number.

Input:

input = {"payment_form":"99"};
input.setMetadata( {payment_form: {type: "string"}} );
output = serializeJSON( input );
writeDump( output );

Results:

{"payment_form":"99"}

Also, in case you're building this from a query object, CF2016 added two new application settings that can override the wacky default for serialized queries, making it MUCH easier to generate a properly cased array of structures out of the box.

Upvotes: 1

Related Questions