Created
February 14, 2014 00:27
-
-
Save bennadel/8986925 to your computer and use it in GitHub Desktop.
Using ObjectSave() And ObjectLoad() With Non-ColdFusion-Component Data Types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
// Set up some non-Component data that will be serialized, saved, | |
// loaded, and then deserialized. Notice that we are including | |
// "ineresting" data types, including TimeStamps and Queries. | |
data = { | |
friends = [ | |
{ | |
name = "Tricia", | |
age = 47 | |
}, | |
{ | |
name = "Joanna", | |
age = 52 | |
} | |
], | |
enemies = [ | |
{ | |
name = "Amanda", | |
age = 38 | |
} | |
], | |
createdAt = now(), | |
utc = dateConvert( "local2utc", now() ), | |
metaData = queryNew( "id, key, value" ) | |
}; | |
// ------------------------------------------------------ // | |
// JavaScript Object Notation Test. | |
// ------------------------------------------------------ // | |
startedAt = getTickCount(); | |
jsonFile = expandPath( "./data.json" ); | |
for ( i = 0 ; i <= 1000 ; i++ ) { | |
// Serialize and save. | |
fileWrite( jsonFile, serializeJson( data ) ); | |
// Load and deserialize. | |
replicated = deserializeJson( fileRead( jsonFile ) ); | |
} | |
writeDump( replicated ); | |
writeOutput( "JSON: " & numberFormat( getTickCount() - startedAt ) ); | |
// ------------------------------------------------------ // | |
// Binary Representation Test. | |
// ------------------------------------------------------ // | |
startedAt = getTickCount(); | |
binFile = expandPath( "./data.bin" ); | |
for ( i = 0 ; i <= 1000 ; i++ ) { | |
// Serialize and save. | |
objectSave( data, binFile ); | |
// Load and deserialize. | |
replicated = objectLoad( binFile ); | |
} | |
writeDump( replicated ); | |
writeOutput( "Binary: " & numberFormat( getTickCount() - startedAt ) ); | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment