Learning ColdFusion 8: Javascript Object Notation (JSON) Part II - Remote Method Calls

<cffunction
	name="GetWeekDatesAsJSON"
	access="remote"
	returntype="string"
	returnformat="plain"
	output="false"
	hint="Given a date, it will return all dates in that week in a day-keyed struct in JSON format.">
 
	<!--- Define arguments. --->
	<cfargument
		name="Date"
		type="date"
		required="false"
		default="#Now()#"
		/>
 
	<!--- Define the local scope. --->
	<cfset var LOCAL = {} />
 
	<!--- Get first day of week. --->
	<cfset LOCAL.Sunday = (
		Fix( ARGUMENTS.Date ) -
		DayOfWeek( ARGUMENTS.Date ) +
		1
		) />
 
	<!--- Create week object. --->
	<cfset LOCAL.Week = {
		Sunday = LOCAL.Sunday,
		Monday = (LOCAL.Sunday + 1),
		Tuesday = (LOCAL.Sunday + 2),
		Wednesday = (LOCAL.Sunday + 3),
		Thursday = (LOCAL.Sunday + 4),
		Friday = (LOCAL.Sunday + 5),
		Saturday = (LOCAL.Sunday + 6)
		} />
 
 
	<!---
		At this point, we have the Week object filled with
		numeric date (data conversion caused from the date
		math we are using. Let's loop over the struct and
		convert these back to actual dates). This is vital
		since we might be returning this remotely to a
		system that has a differen "Zero Date" on which our
		numeric date conversions are based.
	--->
	<cfloop
		item="LOCAL.Day"
		collection="#LOCAL.Week#">
 
		<!---
			Convert back to standard, ColdFusion date/time
			formatting (that can be parsed by remote system.
		--->
		<cfset LOCAL.Week[ LOCAL.Day ] = CreateDate(
			Year( LOCAL.Week[ LOCAL.Day ] ),
			Month( LOCAL.Week[ LOCAL.Day ] ),
			Day( LOCAL.Week[ LOCAL.Day ] )
			) />
 
	</cfloop>
 
	<!--- Return week as JSON data. --->
	<cfreturn SerializeJSON( LOCAL.Week ) />
</cffunction>

For Cut-and-Paste