Q:

jQuery data() JSON Encoding issues

In my current jQuery Mobile application I trap click events on a variety of HTML elements, extract the whole set of data-* attributes for the element using
var data = $(element).data();

package it all into JSON string 
var json = JSON.stringify(data);

and send it off elsewhere for downstream processing.
The problem I have run into (and I am pretty sure it has only started with the recent versions of jQuery Mobile) is with some elements such as radios, checkboxes and buttons which have annonymous functions as part of the data() array. Serializing such functions is

not relevent to my application
more importantly, triggers a JavaScript along the lines of "An attempt was made to use an object that is no longer valid"...

By dint of some experiment I have found that the issue is to do with anonymous functions that turn up in the raw data() object as

buttonElements
mobile-checkboxradio

etc.  I then build my own object to serialize that is devoid of these offending entities.
This is OK but I have found another issue.  Change the jQM version and BOOM, the problem crops up all over again since the attribute name has changed (e.g. mobileCheckboxRadio in place of mobile-checkboxradio).
Question - could anyone suggest a more generic, attribute name agnostic, way of stripping out these offensive bits?  My own efforts and trying to figure out the typeof of the object attribute have failed since what I get is always a string.  I suppose I could always check for the phrase "annonymous function" in that string but that sounds clumsy.

A:

I figured out a way to do this.  One can do the following
var data = $(element).data();
$.each(data,function(key,prop){if ('object' != typeof(prop)) /*do something */)}

Here is a fiddle demonstrating what happens with a radio button.  As you will note two of the data-* attributes returned are objects.  One of them, mobileCheckboxradio is an anonymous function.  Try to serialize data as is and you get an exception - which is being trapped and reported.  With the safely cloned object, which is shorn of the anon function all goes well.
This has the benefit of being "version proof".  Whether it is better than simply checking for the small handful of attributes that are liable to cause such errors is debatable.

