4

I am using CF11 with ORM and service layer to return ORM objects. My Angular app requires that JSON dates be returned in one of the two following formats:

'yyyy/MM/dd' (i.e., '2014/08/25')
'yyyy/MM/dd HH:mm:ss' (i.e., '2014/08/25 16:35:10')

My service CFC returns an array of objects as a JSON string, but Coldfusion serializes the dates in a string format like this: April, 21 2016 04:45:56. This seems to be a result of the SerializeJson() function that is called under the hood.

Is there a way to have an ORM CFC return a date object in a specific JSON string format? As a workaround I created a new property called startTimeAsJson that returns a string using a getter function. But I’d prefer to find a way to have CF just serialize the date object with the format I want.

Here is my ORM object.

component persistent="true" table="course" accessors="true"  
{
    property name="startTime" column="start_time" type="date" ormtype="timestamp" notnull="false";
}

My service layer object.

component output="false" hint="CFBuilder-Generated:test_date" 
{
  remote orm.course[] function getAllCourses(string SortColumn = "STARTDATE DESC")
    {
        return entityLoad("course", {}, arguments.sortcolumn);
    }
}

An example of the JSON returned by my service CFC.

[{"startTime":"May, 09 2016 08:25:24"}]
Sam M
  • 4,136
  • 4
  • 29
  • 42
  • 1
    CF11+ supports custom serializer. Have a look at the docs. – Alex Jun 21 '16 at 11:58
  • 1
    @Alex - If you are familiar with custom serializers, you should write that up as an answer along with an example. That would be helpful to others, since it is really the only option (aside from returning a string as they are already doing). – Leigh Jun 21 '16 at 13:48
  • @Leigh No, I personally do not rely on CF's serialization at all. I just happen to know it's how one is supposed to customize CF's integrated webservices. Just giving a hint how to deal with it, thus a comment. – Alex Jun 21 '16 at 20:03
  • 1
    @Alex - Heh, that makes two of us. I have only skimmed [the docs](https://helpx.adobe.com/coldfusion/developing-applications/changes-in-coldfusion/restful-web-services-in-coldfusion.html#RESTfulWebServicesinColdFusion-Supportforpluggableserializeranddeserializer), but it seems like it may be more trouble than it is worth for a single field. Unless you can make something generic and reusuable. – Leigh Jun 21 '16 at 20:48
  • @Alex - The custom serializer did the trick. But after reading several prominent CF bloggers' experiences with using it, I'm not sure I want to keep going down that path. Seems that its implementation is still lacking. Oh well. Temporary problem solved, on to better things. – Sam M Jun 27 '16 at 22:48
  • 1
    @SamM might be worth posting ur solution here anyways for future passersby – Sajjan Sarkar Jun 29 '16 at 20:49
  • 1
    One of you guys should post the final code as a "answer" to close out the thread, and help the next guy :) – Leigh Jul 07 '16 at 14:44
  • I plan to post the code but want to give credit to @Alex, since his suggestion got me where I need. Alex, if you'll post your suggestion to use a custom serializer as an answer, I'll mark it as accepted and then post my code as another answer. – Sam M Jul 08 '16 at 04:29
  • Just post your answer and mark it. No need to credit my suggestion separately. This is about solving a problem, not giving virtual points to people. ;) – Alex Jul 08 '16 at 12:06
  • The answer/solution would be great... @SamM – Pete Sep 28 '17 at 13:45

1 Answers1

0

Can you use CF's dateTimeFormat() to format the date how you want before returning the value? http://cfdocs.org/datetimeformat
Something like...

local.response = entityLoad("course", {}, arguments.sortcolumn);  
local.response = dateTimeFormat(local.response, "yyyy/mm/dd HH:nn:ss");  
return local.response;  

Or, in one line:

return dateTimeFormat(entityLoad("course", {}, arguments.sortcolumn), "yyyy/mm/dd HH:nn:ss");
Kevin Morris
  • 334
  • 1
  • 8