Have a look at the serialize() API method, as well as the serialize_params options attribute.
It's what we use at Ducksboard to let users arrange widgets in their dashboards.