Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created February 14, 2014 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennadel/8986925 to your computer and use it in GitHub Desktop.
Save bennadel/8986925 to your computer and use it in GitHub Desktop.
Using ObjectSave() And ObjectLoad() With Non-ColdFusion-Component Data Types
<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