Skip to content

Instantly share code, notes, and snippets.

@ricardobeat
Created September 20, 2013 04:28
Show Gist options
  • Save ricardobeat/6633306 to your computer and use it in GitHub Desktop.
Save ricardobeat/6633306 to your computer and use it in GitHub Desktop.
JSON serialize to multipart form data
function serializeMultipart (obj, previous) {
var pairs = []
_.each(obj, function (value, key) {
if (previous !== undefined) {
key = previous + '[' + key + ']'
}
if (_.isArray(value)) {
_.each(value, function (value) {
pairs.push([key + '[]', value])
})
return
}
if (_.isObject(value)) {
var children = serializeMultipart(value, key)
Array.prototype.push.apply(pairs, children)
return
}
if (value != null) {
pairs.push([key, value.toString()])
}
})
return pairs
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment