Created
September 20, 2013 04:28
-
-
Save ricardobeat/6633306 to your computer and use it in GitHub Desktop.
JSON serialize to multipart form data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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