1/* Convert field values into json object. 2 * Usage: $('#editForm').find('select, textarea, input').serializeFormJSON(); */ 3(function ($) { 4 $.fn.serializeFormJSON = function () { 5 var o = {}; 6 var a = this.serializeArray(); 7 $.each(a, function () { 8 if (o[this.name]) { 9 if (!o[this.name].push) { 10 o[this.name] = [o[this.name]]; 11 } 12 o[this.name].push(this.value || ''); 13 } else { 14 o[this.name] = this.value || ''; 15 } 16 }); 17 return o; 18 }; 19})(jQuery); 20 21function jsonToFormData(inJSON, inTestJSON, inFormData, parentKey) { 22 // http://stackoverflow.com/a/22783314/260665 23 // Raj: Converts any nested JSON to formData. 24 var form_data = inFormData || new FormData(); 25 var testJSON = inTestJSON || {}; 26 for (var key in inJSON) { 27 // 1. If it is a recursion, then key has to be constructed like "parent.child" where parent JSON contains a child JSON 28 // 2. Perform append data only if the value for key is not a JSON, recurse otherwise! 29 var constructedKey = key; 30 if (parentKey) { 31 constructedKey = parentKey + "." + key; 32 } 33 34 var value = inJSON[key]; 35 if (value && value.constructor === {}.constructor) { 36 // This is a JSON, we now need to recurse! 37 jsonToFormData(value, testJSON, form_data, constructedKey); 38 } else { 39 form_data.append(constructedKey, inJSON[key]); 40 testJSON[constructedKey] = inJSON[key]; 41 } 42 } 43 return form_data; 44}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.