jquery.serializejson.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*!
  2. SerializeJSON jQuery plugin.
  3. https://github.com/marioizquierdo/jquery.serializeJSON
  4. version 2.9.0 (Jan, 2018)
  5. Copyright (c) 2012-2018 Mario Izquierdo
  6. Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
  7. and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
  8. */
  9. (function (factory) {
  10. if (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module.
  11. define(['jquery'], factory);
  12. } else if (typeof exports === 'object') { // Node/CommonJS
  13. var jQuery = require('jquery');
  14. module.exports = factory(jQuery);
  15. } else { // Browser globals (zepto supported)
  16. factory(window.jQuery || window.Zepto || window.$); // Zepto supported on browsers as well
  17. }
  18. }(function ($) {
  19. "use strict";
  20. // jQuery('form').serializeJSON()
  21. $.fn.serializeJSON = function (options) {
  22. var f, $form, opts, formAsArray, serializedObject, name, value, parsedValue, _obj, nameWithNoType, type, keys, skipFalsy;
  23. f = $.serializeJSON;
  24. $form = this; // NOTE: the set of matched elements is most likely a form, but it could also be a group of inputs
  25. opts = f.setupOpts(options); // calculate values for options {parseNumbers, parseBoolens, parseNulls, ...} with defaults
  26. // Use native `serializeArray` function to get an array of {name, value} objects.
  27. formAsArray = $form.serializeArray();
  28. f.readCheckboxUncheckedValues(formAsArray, opts, $form); // add objects to the array from unchecked checkboxes if needed
  29. // Convert the formAsArray into a serializedObject with nested keys
  30. serializedObject = {};
  31. $.each(formAsArray, function (i, obj) {
  32. name = obj.name; // original input name
  33. value = obj.value; // input value
  34. _obj = f.extractTypeAndNameWithNoType(name);
  35. nameWithNoType = _obj.nameWithNoType; // input name with no type (i.e. "foo:string" => "foo")
  36. type = _obj.type; // type defined from the input name in :type colon notation
  37. if (!type) type = f.attrFromInputWithName($form, name, 'data-value-type');
  38. f.validateType(name, type, opts); // make sure that the type is one of the valid types if defined
  39. if (type !== 'skip') { // ignore inputs with type 'skip'
  40. keys = f.splitInputNameIntoKeysArray(nameWithNoType);
  41. parsedValue = f.parseValue(value, name, type, opts); // convert to string, number, boolean, null or customType
  42. skipFalsy = !parsedValue && f.shouldSkipFalsy($form, name, nameWithNoType, type, opts); // ignore falsy inputs if specified
  43. if (!skipFalsy) {
  44. f.deepSet(serializedObject, keys, parsedValue, opts);
  45. }
  46. }
  47. });
  48. return serializedObject;
  49. };
  50. // Use $.serializeJSON as namespace for the auxiliar functions
  51. // and to define defaults
  52. $.serializeJSON = {
  53. defaultOptions: {
  54. checkboxUncheckedValue: undefined, // to include that value for unchecked checkboxes (instead of ignoring them)
  55. parseNumbers: false, // convert values like "1", "-2.33" to 1, -2.33
  56. parseBooleans: false, // convert "true", "false" to true, false
  57. parseNulls: false, // convert "null" to null
  58. parseAll: false, // all of the above
  59. parseWithFunction: null, // to use custom parser, a function like: function(val){ return parsed_val; }
  60. skipFalsyValuesForTypes: [], // skip serialization of falsy values for listed value types
  61. skipFalsyValuesForFields: [], // skip serialization of falsy values for listed field names
  62. customTypes: {}, // override defaultTypes
  63. defaultTypes: {
  64. "string": function(str) { return String(str); },
  65. "number": function(str) { return Number(str); },
  66. "boolean": function(str) { var falses = ["false", "null", "undefined", "", "0"]; return falses.indexOf(str) === -1; },
  67. "null": function(str) { var falses = ["false", "null", "undefined", "", "0"]; return falses.indexOf(str) === -1 ? str : null; },
  68. "array": function(str) { return JSON.parse(str); },
  69. "object": function(str) { return JSON.parse(str); },
  70. "auto": function(str) { return $.serializeJSON.parseValue(str, null, null, {parseNumbers: true, parseBooleans: true, parseNulls: true}); }, // try again with something like "parseAll"
  71. "skip": null // skip is a special type that makes it easy to ignore elements
  72. },
  73. useIntKeysAsArrayIndex: false // name="foo[2]" value="v" => {foo: [null, null, "v"]}, instead of {foo: ["2": "v"]}
  74. },
  75. // Merge option defaults into the options
  76. setupOpts: function(options) {
  77. var opt, validOpts, defaultOptions, optWithDefault, parseAll, f;
  78. f = $.serializeJSON;
  79. if (options == null) { options = {}; } // options ||= {}
  80. defaultOptions = f.defaultOptions || {}; // defaultOptions
  81. // Make sure that the user didn't misspell an option
  82. validOpts = ['checkboxUncheckedValue', 'parseNumbers', 'parseBooleans', 'parseNulls', 'parseAll', 'parseWithFunction', 'skipFalsyValuesForTypes', 'skipFalsyValuesForFields', 'customTypes', 'defaultTypes', 'useIntKeysAsArrayIndex']; // re-define because the user may override the defaultOptions
  83. for (opt in options) {
  84. if (validOpts.indexOf(opt) === -1) {
  85. throw new Error("serializeJSON ERROR: invalid option '" + opt + "'. Please use one of " + validOpts.join(', '));
  86. }
  87. }
  88. // Helper to get the default value for this option if none is specified by the user
  89. optWithDefault = function(key) { return (options[key] !== false) && (options[key] !== '') && (options[key] || defaultOptions[key]); };
  90. // Return computed options (opts to be used in the rest of the script)
  91. parseAll = optWithDefault('parseAll');
  92. return {
  93. checkboxUncheckedValue: optWithDefault('checkboxUncheckedValue'),
  94. parseNumbers: parseAll || optWithDefault('parseNumbers'),
  95. parseBooleans: parseAll || optWithDefault('parseBooleans'),
  96. parseNulls: parseAll || optWithDefault('parseNulls'),
  97. parseWithFunction: optWithDefault('parseWithFunction'),
  98. skipFalsyValuesForTypes: optWithDefault('skipFalsyValuesForTypes'),
  99. skipFalsyValuesForFields: optWithDefault('skipFalsyValuesForFields'),
  100. typeFunctions: $.extend({}, optWithDefault('defaultTypes'), optWithDefault('customTypes')),
  101. useIntKeysAsArrayIndex: optWithDefault('useIntKeysAsArrayIndex')
  102. };
  103. },
  104. // Given a string, apply the type or the relevant "parse" options, to return the parsed value
  105. parseValue: function(valStr, inputName, type, opts) {
  106. var f, parsedVal;
  107. f = $.serializeJSON;
  108. parsedVal = valStr; // if no parsing is needed, the returned value will be the same
  109. if (opts.typeFunctions && type && opts.typeFunctions[type]) { // use a type if available
  110. parsedVal = opts.typeFunctions[type](valStr);
  111. } else if (opts.parseNumbers && f.isNumeric(valStr)) { // auto: number
  112. parsedVal = Number(valStr);
  113. } else if (opts.parseBooleans && (valStr === "true" || valStr === "false")) { // auto: boolean
  114. parsedVal = (valStr === "true");
  115. } else if (opts.parseNulls && valStr == "null") { // auto: null
  116. parsedVal = null;
  117. } else if (opts.typeFunctions && opts.typeFunctions["string"]) { // make sure to apply :string type if it was re-defined
  118. parsedVal = opts.typeFunctions["string"](valStr);
  119. }
  120. // Custom parse function: apply after parsing options, unless there's an explicit type.
  121. if (opts.parseWithFunction && !type) {
  122. parsedVal = opts.parseWithFunction(parsedVal, inputName);
  123. }
  124. return parsedVal;
  125. },
  126. isObject: function(obj) { return obj === Object(obj); }, // is it an Object?
  127. isUndefined: function(obj) { return obj === void 0; }, // safe check for undefined values
  128. isValidArrayIndex: function(val) { return /^[0-9]+$/.test(String(val)); }, // 1,2,3,4 ... are valid array indexes
  129. isNumeric: function(obj) { return obj - parseFloat(obj) >= 0; }, // taken from jQuery.isNumeric implementation. Not using jQuery.isNumeric to support old jQuery and Zepto versions
  130. optionKeys: function(obj) { if (Object.keys) { return Object.keys(obj); } else { var key, keys = []; for(key in obj){ keys.push(key); } return keys;} }, // polyfill Object.keys to get option keys in IE<9
  131. // Fill the formAsArray object with values for the unchecked checkbox inputs,
  132. // using the same format as the jquery.serializeArray function.
  133. // The value of the unchecked values is determined from the opts.checkboxUncheckedValue
  134. // and/or the data-unchecked-value attribute of the inputs.
  135. readCheckboxUncheckedValues: function (formAsArray, opts, $form) {
  136. var selector, $uncheckedCheckboxes, $el, uncheckedValue, f, name;
  137. if (opts == null) { opts = {}; }
  138. f = $.serializeJSON;
  139. selector = 'input[type=checkbox][name]:not(:checked):not([disabled])';
  140. $uncheckedCheckboxes = $form.find(selector).add($form.filter(selector));
  141. $uncheckedCheckboxes.each(function (i, el) {
  142. // Check data attr first, then the option
  143. $el = $(el);
  144. uncheckedValue = $el.attr('data-unchecked-value');
  145. if (uncheckedValue == null) {
  146. uncheckedValue = opts.checkboxUncheckedValue;
  147. }
  148. // If there's an uncheckedValue, push it into the serialized formAsArray
  149. if (uncheckedValue != null) {
  150. if (el.name && el.name.indexOf("[][") !== -1) { // identify a non-supported
  151. throw new Error("serializeJSON ERROR: checkbox unchecked values are not supported on nested arrays of objects like '"+el.name+"'. See https://github.com/marioizquierdo/jquery.serializeJSON/issues/67");
  152. }
  153. formAsArray.push({name: el.name, value: uncheckedValue});
  154. }
  155. });
  156. },
  157. // Returns and object with properties {name_without_type, type} from a given name.
  158. // The type is null if none specified. Example:
  159. // "foo" => {nameWithNoType: "foo", type: null}
  160. // "foo:boolean" => {nameWithNoType: "foo", type: "boolean"}
  161. // "foo[bar]:null" => {nameWithNoType: "foo[bar]", type: "null"}
  162. extractTypeAndNameWithNoType: function(name) {
  163. var match;
  164. if (match = name.match(/(.*):([^:]+)$/)) {
  165. return {nameWithNoType: match[1], type: match[2]};
  166. } else {
  167. return {nameWithNoType: name, type: null};
  168. }
  169. },
  170. // Check if this input should be skipped when it has a falsy value,
  171. // depending on the options to skip values by name or type, and the data-skip-falsy attribute.
  172. shouldSkipFalsy: function($form, name, nameWithNoType, type, opts) {
  173. var f = $.serializeJSON;
  174. var skipFromDataAttr = f.attrFromInputWithName($form, name, 'data-skip-falsy');
  175. if (skipFromDataAttr != null) {
  176. return skipFromDataAttr !== 'false'; // any value is true, except if explicitly using 'false'
  177. }
  178. var optForFields = opts.skipFalsyValuesForFields;
  179. if (optForFields && (optForFields.indexOf(nameWithNoType) !== -1 || optForFields.indexOf(name) !== -1)) {
  180. return true;
  181. }
  182. var optForTypes = opts.skipFalsyValuesForTypes;
  183. if (type == null) type = 'string'; // assume fields with no type are targeted as string
  184. if (optForTypes && optForTypes.indexOf(type) !== -1) {
  185. return true
  186. }
  187. return false;
  188. },
  189. // Finds the first input in $form with this name, and get the given attr from it.
  190. // Returns undefined if no input or no attribute was found.
  191. attrFromInputWithName: function($form, name, attrName) {
  192. var escapedName, selector, $input, attrValue;
  193. escapedName = name.replace(/(:|\.|\[|\]|\s)/g,'\\$1'); // every non-standard character need to be escaped by \\
  194. selector = '[name="' + escapedName + '"]';
  195. $input = $form.find(selector).add($form.filter(selector)); // NOTE: this returns only the first $input element if multiple are matched with the same name (i.e. an "array[]"). So, arrays with different element types specified through the data-value-type attr is not supported.
  196. return $input.attr(attrName);
  197. },
  198. // Raise an error if the type is not recognized.
  199. validateType: function(name, type, opts) {
  200. var validTypes, f;
  201. f = $.serializeJSON;
  202. validTypes = f.optionKeys(opts ? opts.typeFunctions : f.defaultOptions.defaultTypes);
  203. if (!type || validTypes.indexOf(type) !== -1) {
  204. return true;
  205. } else {
  206. throw new Error("serializeJSON ERROR: Invalid type " + type + " found in input name '" + name + "', please use one of " + validTypes.join(', '));
  207. }
  208. },
  209. // Split the input name in programatically readable keys.
  210. // Examples:
  211. // "foo" => ['foo']
  212. // "[foo]" => ['foo']
  213. // "foo[inn][bar]" => ['foo', 'inn', 'bar']
  214. // "foo[inn[bar]]" => ['foo', 'inn', 'bar']
  215. // "foo[inn][arr][0]" => ['foo', 'inn', 'arr', '0']
  216. // "arr[][val]" => ['arr', '', 'val']
  217. splitInputNameIntoKeysArray: function(nameWithNoType) {
  218. var keys, f;
  219. f = $.serializeJSON;
  220. keys = nameWithNoType.split('['); // split string into array
  221. keys = $.map(keys, function (key) { return key.replace(/\]/g, ''); }); // remove closing brackets
  222. if (keys[0] === '') { keys.shift(); } // ensure no opening bracket ("[foo][inn]" should be same as "foo[inn]")
  223. return keys;
  224. },
  225. // Set a value in an object or array, using multiple keys to set in a nested object or array:
  226. //
  227. // deepSet(obj, ['foo'], v) // obj['foo'] = v
  228. // deepSet(obj, ['foo', 'inn'], v) // obj['foo']['inn'] = v // Create the inner obj['foo'] object, if needed
  229. // deepSet(obj, ['foo', 'inn', '123'], v) // obj['foo']['arr']['123'] = v //
  230. //
  231. // deepSet(obj, ['0'], v) // obj['0'] = v
  232. // deepSet(arr, ['0'], v, {useIntKeysAsArrayIndex: true}) // arr[0] = v
  233. // deepSet(arr, [''], v) // arr.push(v)
  234. // deepSet(obj, ['arr', ''], v) // obj['arr'].push(v)
  235. //
  236. // arr = [];
  237. // deepSet(arr, ['', v] // arr => [v]
  238. // deepSet(arr, ['', 'foo'], v) // arr => [v, {foo: v}]
  239. // deepSet(arr, ['', 'bar'], v) // arr => [v, {foo: v, bar: v}]
  240. // deepSet(arr, ['', 'bar'], v) // arr => [v, {foo: v, bar: v}, {bar: v}]
  241. //
  242. deepSet: function (o, keys, value, opts) {
  243. var key, nextKey, tail, lastIdx, lastVal, f;
  244. if (opts == null) { opts = {}; }
  245. f = $.serializeJSON;
  246. if (f.isUndefined(o)) { throw new Error("ArgumentError: param 'o' expected to be an object or array, found undefined"); }
  247. if (!keys || keys.length === 0) { throw new Error("ArgumentError: param 'keys' expected to be an array with least one element"); }
  248. key = keys[0];
  249. // Only one key, then it's not a deepSet, just assign the value.
  250. if (keys.length === 1) {
  251. if (key === '') {
  252. o.push(value); // '' is used to push values into the array (assume o is an array)
  253. } else {
  254. o[key] = value; // other keys can be used as object keys or array indexes
  255. }
  256. // With more keys is a deepSet. Apply recursively.
  257. } else {
  258. nextKey = keys[1];
  259. // '' is used to push values into the array,
  260. // with nextKey, set the value into the same object, in object[nextKey].
  261. // Covers the case of ['', 'foo'] and ['', 'var'] to push the object {foo, var}, and the case of nested arrays.
  262. if (key === '') {
  263. lastIdx = o.length - 1; // asume o is array
  264. lastVal = o[lastIdx];
  265. if (f.isObject(lastVal) && (f.isUndefined(lastVal[nextKey]) || keys.length > 2)) { // if nextKey is not present in the last object element, or there are more keys to deep set
  266. key = lastIdx; // then set the new value in the same object element
  267. } else {
  268. key = lastIdx + 1; // otherwise, point to set the next index in the array
  269. }
  270. }
  271. // '' is used to push values into the array "array[]"
  272. if (nextKey === '') {
  273. if (f.isUndefined(o[key]) || !$.isArray(o[key])) {
  274. o[key] = []; // define (or override) as array to push values
  275. }
  276. } else {
  277. if (opts.useIntKeysAsArrayIndex && f.isValidArrayIndex(nextKey)) { // if 1, 2, 3 ... then use an array, where nextKey is the index
  278. if (f.isUndefined(o[key]) || !$.isArray(o[key])) {
  279. o[key] = []; // define (or override) as array, to insert values using int keys as array indexes
  280. }
  281. } else { // for anything else, use an object, where nextKey is going to be the attribute name
  282. if (f.isUndefined(o[key]) || !f.isObject(o[key])) {
  283. o[key] = {}; // define (or override) as object, to set nested properties
  284. }
  285. }
  286. }
  287. // Recursively set the inner object
  288. tail = keys.slice(1);
  289. f.deepSet(o[key], tail, value, opts);
  290. }
  291. }
  292. };
  293. }));