If you cfdump the output of the fetter do you get an error that the "var" attribute was not provided?
Hi Brad,
When I simply dump this out in a view it returns an empty variable as the db field is null. No error occurs. It's only when I try to serialize to JSON via renderData(). The getter is the implicit getter from loading the entity through CF ORM.
Thanks.
Nolan
Load *.*,8,1
I specifically asked what happens when you use the cfdump tag.
If your "dump" looks something like this:
<cfoutput>#qUser.getHomeTelephone()#</cfoutput>
then that's not what I'm talking about.
That is simply appending it to the page's output buffer which treats it as an empty string without erroring. I asked you to try cfdumping to confirm if the getter was actually returning an null value.
Alternatively, you can do this:
<cfset foobar = qUser.getHomeTelephone>
#isdefined("foobar")#
If it outputs "NO", then getHomeTelephone is returning NULL.
If it is, then you need to modify the getter to always return something, or add conditional logic to ensure that the user.home_telephone key always gets defined.
Thanks!
~Brad
Hi Brad,
Sorry for the confusion.
I did as you suggested and #isDefined(“foobar”)# returns a YES. If I run the same on the array variable that was set - i.e user.home_telephone = “#qUser.getHomeTelephone#”; - isDefined(); returns a NO. The value changes from and [empty string] to undefined once the array is populated.
Thoughts?
Thank you.
Nolan
Nolan,
If you are using CF9, you can use the new isNull() function. An entity the doesn’t exist or a blank property with no default will return NULL in CF9
http://cfquickdocs.com/cf9/#isnull
Hope that helps,
Curt Gratz
Computer Know How
Hi guys,
Thanks for your feedback on this. It's all greatly appreciated.
So, the set variable that is coming back returns a YES if I run the isNull() function, as the database field is a NULL, and not an empty string. It I run a serializeJSON() function on my object I'm able to see that the variable 'hometelephone' is actually null in the serialized JSON. This is exactly what I want. The problem seems to occur when event.renderData(data=mystruct,format=json);.
JSON can allow null values, so I'm guessing that event.renderData() is blowing up on the null variable, when it should just serialize the object with the null.
Has anyone else experienced this or found a fix to resolve?
Thanks.
Nolan
Hi nolan,
Ok, so the problem is the null value, where is this blowing up in the conversion process? Can we start again on this, since we now know the culprit is the null value.
Thanks.
Luis F. Majano
President
Ortus Solutions, Corp
ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com
Thanks Luis,
My handler has code like this:
//create new user struc
var rc.user = structNew();
//render format
rc.format=“JSON”;
//get the user record by pk
var qUser = userService.getUser(1,true);
//populate the user struct
rc.user.firstName = qUser.getFirstName();
rc.user.lastName = qUser.getLastName()
rc.user.home_telephone = qUser.getHomeTelephone() // this field is NULL value in the database , isNull = YES
At this point if I were to serialize the struct using serializeJSON(rc.user) and dump to screen I see that the home_telephone var serialized to NULL successfully i.e home_telephone:null
As soon as I pass rc.user to event.renderData… i.e event.renderData(data=rc.user,format=rc.format)… I receive an error indicating home_telephone doesn’t exist. this makes sense because it’s NULL, however I still want to return the NULL using renderData(). So serializeJSON() works but renderData() doesn’t, which I assume is using serializeJSON under the hood.
I have a second question while it’s top of mind. Is there a way to send back a custom JSON representation?
i.e I don’t want to have key’s for data, error, or messages. i.e. I just want to send the rc.user back …
{
"firstname"``: ``"nolan"``,`
“lastname”:
“dubeau”``,`
“home_telephone” : null
}
Many thanks.
Nolan
ColdBox is using the json plugin to render json not the CF-function
serializeJSON.
Ernst
Thanks Ernst. That was an incorrect assumption on my part.
Upon further investigation the error appears to happening at line 311 of coldbox.system.core.conversion.json.cfc
<cfelseif IsStruct(_data)>
<cfset dJSONString = createObject(‘java’,‘java.lang.StringBuffer’).init("") />
//line 311 <cfset tempVal = encode( _data[ arKeys[i] ], arguments.queryFormat, arguments.queryKeyCase, arguments.stringNumbers, arguments.formatDates, arguments.columnListFormat ) />
<cfif arguments.keyCase EQ “lower”>
<cfif dJSONString.toString() EQ “”>
<cfset dJSONString.append(’"’ & arKey & ‘":’ & tempVal) />
<cfset dJSONString.append("," & ‘"’ & arKey & ‘":’ & tempVal) />
<cfreturn “{” & dJSONString.toString() & “}” />
Luis, would there be a way to check if the key is NULL and still do the encode/insertion?
Thanks.
Nolan
Yes definitely actually you can try it out and submit it to me verified. Anybody can fork coldbox now on github and collaborate and I actually encourage it
fixed.
Luis F. Majano
President
Ortus Solutions, Corp
ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com
Hi Luis,
Thanks so much for getting this fix in! Does this mean that the fix is applied in the build posted on GIT Hub?
Cheers,
Nolan
on rc1 released yesterday.
Luis F. Majano
President
Ortus Solutions, Corp
ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com
Thanks Luis,
I think I may have uncovered another bug. I tried passing an array of structs and an error is being thrown at line 302 of Json.cfc indicating that the simple value may be of comlex type. Is the json serialized capable of serializing an array of structs?
Thanks.
Nolan Dubeau
Load .,8,1
oops, fixed and re-releasedx.
Luis F. Majano
President
Ortus Solutions, Corp
ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com
Just tested and it serialized successfully.
You the man Luis! Thanks!
Nolan
Hi Luis,
I was just testing running an XML conversion in a REST request and it appears that the XML convertor may have the same issue trying to parse null values. Would you like me to log the bug on assembla?
Thanks.
N
Yes, please do, I am guessing I will have to apply the same approach also.
Thanks for the testing, you rock!! I am really happy when the community helps out
Luis F. Majano
President
Ortus Solutions, Corp
ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com
Hi Luis,
Your welcome! I’ll add a bug to assembla, and yes, the same approach would apply to the XML convertor.
Cheers,
Nolan