tracker issue : CF-3750730

select a category, or use search below
(searches all categories and all time range)
Title:

Custom serialiser: canSerialize() doesn't seem to function sensibly

| View in Tracker

Status/Resolution/Reason: Closed/Fixed/

Reporter/Name(from Bugbase): Adam Cameron / Adam Cameron (Adam Cameron)

Created: 04/27/2014

Components: REST Services

Versions: 11.0

Failure Type:

Found In Build/Fixed In Build: PublicBeta /

Priority/Frequency: Major / Some users will encounter

Locale/System: English / Platforms All

Vote Count: 2

Listed in the version 2016.0.0.297996 Issues Fixed doc
Verification notes: verified_fixed on September 05, 2019 using build 2016.0.01.298513
Long version:
http://cfmlblog.adamcameron.me/2014/04/coldfusion-11-custom-serialisers-more.html

TL;DR:
when trying to serialise an object, the canSerialize() function receives the argument value "XML" as its first argument. This makes no sense.

Repro:

Application.cfc:
component {

    this.name = "serialiser01";
    this.customSerializer="Serialiser";

}

Serialiser.cfc:
// Serialiser.cfc
component {

    public function canSerialize(){
        logArgs(args=arguments, from=getFunctionCalledName());
        return true;
    }

    public function canDeserialize(){
        logArgs(args=arguments, from=getFunctionCalledName());
        return true;
    }

    public function serialize(){
        logArgs(args=arguments, from=getFunctionCalledName());
        return "SERIALISED";
    }

    public function deserialize(){
        logArgs(args=arguments, from=getFunctionCalledName());
        return "DESERIALISED";
    }

    private function logArgs(required struct args, required string from){
        var dumpFile = getDirectoryFromPath(getCurrentTemplatePath()) & "dump_#from#.html";
        if (fileExists(dumpFile)){
            fileDelete(dumpFile);
        }
        writeDump(var=args, label=from, output=dumpFile, format="html");
    }
}

test.cfm:
o = new Basic();

serialised = serializeJson(o);
writeDump([serialised]);

deserialised = deserializeJson(serialised);
writeDump([deserialised]);

Basic.cfc
component {}

canSerialize() should receive the object type or something actually useful.

-- 
Adam

----------------------------- Additional Watson Details -----------------------------

Watson Bug ID:	3750730

External Customer Info:
External Company:  
External Customer Name: Adam Cameron.
External Customer Email:  
External Test Config: My Hardware and Environment details:

Attachments:

Comments:

canSerialize() and canDeserialize() actually has an argument. It is the type to/from which you want to serialize/deserialize. XML or JSON. Are you asking that, these methods should actually take 2 arguments? the type and the actual object to serialize/deserialize? Similar to serialize and deserialize methods? The idea behind the canSerialize and canDeserialize() was to provide a first level filtration (what format is supported by this serializer). Suppose you have a RESTful webservice, and the service returns different objects. For eg:- it returns a Person CFC, a Struct and a Query for different functions. XML and JSON is already supported by CF. You want to send the data in one more format. Say, YAML. When you say you want to support YAML, you want the serialization of all objects(Person, Struct and Query) to be handled by your Serializer. You would not want only the Person to be serialized to YAML and Struct and Query not supported for YAML clients. So canSerialize() can take only the format to which it needs to serialize to. canSerialize() can tell ColdFusion whether YAML is supported or not. You would not need the type of the object to tell CF whether YAML is supported or not.
Comment by Paul N.
12521 | May 06, 2014 07:44:38 AM GMT
You'd save us both a lot of time if you just read the ticket properly and ran the repro code. -- Adam
Comment by External U.
12522 | May 06, 2014 08:13:15 AM GMT
When trying to serialize data to JSON format, the canSerialize() method is getting "XML" as the input data. Fixed the issue. Now, will get JSON itself as the type, when JSON data is serialized.
Comment by Paul N.
12523 | November 17, 2014 02:04:59 AM GMT
+1 ......................
Vote by External U.
12526 | February 14, 2015 02:15:56 PM GMT
Which build is this fixed in?
Comment by External U.
12524 | June 18, 2015 05:18:44 AM GMT
on CF11 Update 7, does not seem fixed. Just tried serializing a custom object like Basic in the bug report using serializeJSON() , and canSerialize() still receives "XML" and never received "JSON", but CF still invokes "serialize()". It's easier to reproduce by function canSerialize(required type){ return type == 'json'; } Then serialize() is never called.
Comment by External U.
12525 | March 30, 2016 08:08:35 PM GMT
Not fixed yet, in CF11 update 7
Vote by External U.
12527 | March 30, 2016 08:08:57 PM GMT
Hi Adobe, I've verified this is fixed in CF2016 Update 1 (build 2016.0.01.298513). Thanks!, -Aaron
Comment by Aaron N.
31280 | September 05, 2019 02:31:47 AM GMT