Tuesday, 2 July 2013

Example of serialising data using objectSave() and objectLoad()

G'day:
Bloody hell: fifth one today. And I've got a sixth one to come (another bloody stupid ColdFusion bug... stay tuned...).

My previous article described the mooted solution to our serialisation challenge, but it did not have an end-to-end code example.


So here's one:

// C.cfc... this is just so I can demonstrate serialising an object
component {

    public numeric function f(x){
        return x;
    }
}

<cfprocessingdirective pageencoding="utf-8">
<cfscript>
initialData = {
    string    = "Zachary",
    boolean    = 1 EQ 1,
    date    = now(),
    integer    = 84 \ 2,
    float    = pi(),
    struct    = {
        one        = "tahi",
        two        = "rua",
        three    = "toru",
        four    = "wha"
    },
    array    = ["Kohi-tātea","Hui-tanguru","Poutū-te-rangi","Paenga-whāwhā","Haratua","Pipiri","Hōngongoi","Here-turi-kōkā","Mahuru","Whiringa-ā-nuku","Whiringa-ā-rangi","Hakihea"],
    object    = new C()
};
writeDump(var=initialData, label="initialData");

savedObject = objectSave(initialData);
writeDump(var=savedObject, label="savedObject");

encoded = binaryEncode(savedObject, "HEX");
writeDump(var=encoded, label="encoded");

decoded = binaryDecode(encoded, "HEX");
writeDump(var=decoded, label="decoded");

loadedObject = objectLoad(decoded);
writeDump(var=loadedObject, label="loadedObject");
</cfscript>

Here we do the following things:
And the results are pleasing:

initialData - struct
ARRAY
initialData - array
1Kohi-tātea
2Hui-tanguru
3Poutū-te-rangi
4Paenga-whāwhā
5Haratua
6Pipiri
7Hōngongoi
8Here-turi-kōkā
9Mahuru
10Whiringa-ā-nuku
11Whiringa-ā-rangi
12Hakihea
BOOLEANYES
DATE{ts '2013-07-02 12:14:42'}
FLOAT3.14159265359
INTEGER42
OBJECT
initialData - component cf.cfml.functions.conversion.objectSave.tostring.C
METHODS
STRINGZachary
STRUCT
initialData - struct
FOURwha
ONEtahi
THREEtoru
TWOrua
savedObject - binary
-84-190511511403299111108100102117115105111110461141171101161051091014683116114117991168711497112112101114113105935586-7568492017603109971121160157610697118974711711610510847779711259120112115114017106971189746117116105108467297115104779711257-38-63-612296-4730270010108111971007097991161111147309116104114101115104111108100120112636400000121198000160008116077378846971698211511401710697118974610897110103467311011610110310111418-30-96-92-9-127-1215620173051189710811710112011401610697118974610897110103467811710998101114-122-84-1072911-108-32-11720012011200042116068384828567841151130126001151130126036364000001211980001600041160379786911604116971041051160384877911603114117971160584728269691160411611111411711604707985821160311910497120116076679797669657811511401710697118974610897110103466611111110810197110-5132114-128-43-100-6-18201900511897108117101120112111605707679658411511401610697118974610897110103466811111798108101-128-77-627441107-5420168051189710811710112011301260764933-5846845241160468658469115114037991111081001021171151051111104611411711011610510910146791081016897116101841051091018711497112112101114106-48-110108121-55-582201740411610510910112011200163-972311939116067966746967841151140399911110810010211711510511111046114117110116105109101468410110911210897116101801141111201218711497112112101114-94-90-9826-70158612112001201121151130126036364000001211980001600001201151130126036364000001211980001600001201160509910246991021091084610211711099116105111110115469911111011810111411510511111046111981061019911683971181014611611111511611410511010346671191012011605658282658911511403199111108100102117115105111110461141171101161051091014665114114971218711497112112101114158-128-2311877-124-3520273091001051091011101151051111107609118971081171017610511511611602276106971189747117116105108476711110810810199116105111110591201120001115114016106971189746117116105108468610199116111114-39-10512591-12859-8113037301799971129799105116121731109911410110910111011673012101108101109101110116671111171101169101110110810110910111011668971169711601991761069711897471089711010347799810610199116591201120000000121171140199176106971189746108971101034679981061019911659-112-5088-971611541108200120112000121160117511110410545116-60-1271161019711601172117105451169711010311711411711601580111117116-59-85451161014511497 
[truncated]
ACED000573720020636F6C64667573696F6E2E72756E74696D652E5374727563745772617070657271695D3756B544310200014C00036D617074000F4C6A6176612F7574696C2F4D61703B7870737200116A6176612E7574696C2E486173684D61700507DAC1C31660D103000246000A6C6F6164466163746F724900097468726573686F6C6478703F4000000000000C77080000001000000008740007494E5445474552737200116A6176612E6C616E672E496E746567657212E2A0A4F781873802000149000576616C7565787200106A6176612E6C616E672E4E756D62657286AC951D0B94E08B02000078700000002A7400065354525543547371007E00007371007E00033F4000000000000C770800000010000000047400034F4E457400047461686974000354574F7400037275617400055448524545740004746F7275740004464F555274000377686178740007424F4F4C45414E737200116A6176612E6C616E672E426F6F6C65616ECD207280D59CFAEE0200015A000576616C7565787001740005464C4F4154737200106A6176612E6C616E672E446F75626C6580B3C24A296BFB0402000144000576616C75657871007E0007400921FB54442D187400044441544573720025636F6C64667573696F6E2E72756E74696D652E4F6C654461746554696D65577261707065726AD0926C79C9FB520200014A000474696D6578700000013F9F1777277400064F424A45435473720027636F6C64667573696F6E2E72756E74696D652E54656D706C61746550726F787957726170706572A2A69E1ABA0F56790C000078707371007E00033F4000000000000C77080000001000000000787371007E00033F4000000000000C770800000010000000007874003263662E63666D6C2E66756E6374696F6E732E636F6E76657273696F6E2E6F626A656374536176652E746F737472696E672E437701007874000541525241597372001F636F6C64667573696F6E2E72756E74696D652E4172726179577261707065720F0880E9764D84DD02000249000964696D656E73696F6E4C000976616C75654C6973747400164C6A6176612F7574696C2F436F6C6C656374696F6E3B787000000001737200106A6176612E7574696C2E566563746F72D9977D5B803BAF010300034900116361706163697479496E6372656D656E7449000C656C656D656E74436F756E745B000B656C656D656E74446174617400135B4C6A6176612F6C616E672F4F626A6563743B7870000000000000000C757200135B4C6A6176612E6C616E672E4F626A6563743B90CE589F1073296C02000078700000000C74000B4B6F68692D74C48174656174000B4875692D74616E6775727574000F506F7574C5AB2D74652D72616E676974000F5061656E67612D7768C4817768C4817400074861726174756174000650697069726974000A48C58D6E676F6E676F69740010486572652D747572692D6BC58D6BC4817400064D616875727574001057686972696E67612DC4812D6E756B7574001157686972696E67612DC4812D72616E676974000748616B6968656178740006535452494E477400075A61636861727978
decoded - binary
-84-190511511403299111108100102117115105111110461141171101161051091014683116114117991168711497112112101114113105935586-7568492017603109971121160157610697118974711711610510847779711259120112115114017106971189746117116105108467297115104779711257-38-63-612296-4730270010108111971007097991161111147309116104114101115104111108100120112636400000121198000160008116077378846971698211511401710697118974610897110103467311011610110310111418-30-96-92-9-127-1215620173051189710811710112011401610697118974610897110103467811710998101114-122-84-1072911-108-32-11720012011200042116068384828567841151130126001151130126036364000001211980001600041160379786911604116971041051160384877911603114117971160584728269691160411611111411711604707985821160311910497120116076679797669657811511401710697118974610897110103466611111110810197110-5132114-128-43-100-6-18201900511897108117101120112111605707679658411511401610697118974610897110103466811111798108101-128-77-627441107-5420168051189710811710112011301260764933-5846845241160468658469115114037991111081001021171151051111104611411711011610510910146791081016897116101841051091018711497112112101114106-48-110108121-55-582201740411610510910112011200163-972311939116067966746967841151140399911110810010211711510511111046114117110116105109101468410110911210897116101801141111201218711497112112101114-94-90-9826-70158612112001201121151130126036364000001211980001600001201151130126036364000001211980001600001201160509910246991021091084610211711099116105111110115469911111011810111411510511111046111981061019911683971181014611611111511611410511010346671191012011605658282658911511403199111108100102117115105111110461141171101161051091014665114114971218711497112112101114158-128-2311877-124-3520273091001051091011101151051111107609118971081171017610511511611602276106971189747117116105108476711110810810199116105111110591201120001115114016106971189746117116105108468610199116111114-39-10512591-12859-8113037301799971129799105116121731109911410110910111011673012101108101109101110116671111171101169101110110810110910111011668971169711601991761069711897471089711010347799810610199116591201120000000121171140199176106971189746108971101034679981061019911659-112-5088-971611541108200120112000121160117511110410545116-60-1271161019711601172117105451169711010311711411711601580111117116-59-85451161014511497 
[truncated]
loadedObject - struct
ARRAY
loadedObject - array
1Kohi-tātea
2Hui-tanguru
3Poutū-te-rangi
4Paenga-whāwhā
5Haratua
6Pipiri
7Hōngongoi
8Here-turi-kōkā
9Mahuru
10Whiringa-ā-nuku
11Whiringa-ā-rangi
12Hakihea
BOOLEANYES
DATE{ts '2013-07-02 12:14:42'}
FLOAT3.14159265359
INTEGER42
OBJECT
loadedObject - component cf.cfml.functions.conversion.objectSave.tostring.C
METHODS
STRINGZachary
STRUCT
loadedObject - struct
FOURwha
ONEtahi
THREEtoru
TWOrua

And that's it. Except for the bloody bug I found in objectSave()...

--
Adam