Skip to content

Commit

Permalink
changing the name from serializeArrayToJson to serializeObject
Browse files Browse the repository at this point in the history
  • Loading branch information
Villareal, Neil K committed Aug 3, 2015
1 parent 5e1ac0f commit d6e3c73
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 101 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
# serializeArrayToJson
# serializeObject
This jquery plugin will map the serialized form data array to JSON format.



##### Dependency
1. [jquery](https://github.com/citnvillareal/serializeArrayToJson/blob/master/js/jquery-1.11.3.min.js) library.
1. [jquery](https://github.com/citnvillareal/serializeObject/blob/master/js/jquery-1.11.3.min.js) library.



##### To Get Started
1. import the [jquery](https://github.com/citnvillareal/serializeArrayToJson/blob/master/js/jquery-1.11.3.min.js) library.
2. import the [jquery.serializeArrayToJson.js](https://github.com/citnvillareal/serializeArrayToJson/blob/master/js/jquery.serializeArrayToJson.js) plugin.
1. import the [jquery](https://github.com/citnvillareal/serializeObject/blob/master/js/jquery-1.11.3.min.js) library.
2. import the [jquery.serializeObject.js](https://github.com/citnvillareal/serializeObject/blob/master/js/jquery.serializeObject.js) plugin.



##### First Usage
$( "form" ).serializeArrayToJson();
$( "form" ).serializeObject();



Expand All @@ -25,7 +25,7 @@ This jquery plugin will map the serialized form data array to JSON format.
name: "John Doe"
};

$( "form" ).serializeArrayToJson( { requiredFormat: rFormat } );
$( "form" ).serializeObject( { requiredFormat: rFormat } );



Expand All @@ -43,7 +43,7 @@ This jquery plugin will map the serialized form data array to JSON format.
$( "form" ).submit( function( e ) {
e.preventDefault();

var jsonObject = $( this ).serializeArrayToJson();
var jsonObject = $( this ).serializeObject();
console.log( jsonObject );
} );
} );
Expand Down Expand Up @@ -72,7 +72,7 @@ This jquery plugin will map the serialized form data array to JSON format.
$( "form" ).submit( function( e ) {
e.preventDefault();

var jsonObject = $( this ).serializeArrayToJson();
var jsonObject = $( this ).serializeObject();
console.log( jsonObject );
} );
} );
Expand Down Expand Up @@ -106,7 +106,7 @@ This jquery plugin will map the serialized form data array to JSON format.
$( "form" ).submit( function( e ) {
e.preventDefault();

var jsonObject = $( this ).serializeArrayToJson();
var jsonObject = $( this ).serializeObject();
console.log( jsonObject );
} );
} );
Expand Down Expand Up @@ -151,7 +151,7 @@ This jquery plugin will map the serialized form data array to JSON format.
$( "form" ).submit( function( e ) {
e.preventDefault();

var jsonObject = $( this ).serializeArrayToJson();
var jsonObject = $( this ).serializeObject();
console.log( jsonObject );
} );
} );
Expand Down Expand Up @@ -201,7 +201,7 @@ This jquery plugin will map the serialized form data array to JSON format.
company: "ACN"
};

var jsonObject = $( this ).serializeArrayToJson( { requiredFormat: reqFormat } );
var jsonObject = $( this ).serializeObject( { requiredFormat: reqFormat } );
console.log(jsonObject);
} );
} );
Expand Down
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<head>
<title>Serialize Array to Json Object</title>
<script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="js/jquery.serializeArrayToJson.js"></script>
<script type="text/javascript" src="js/jquery.serializeObject.js"></script>
</head>
<body>
<form>
Expand Down Expand Up @@ -90,7 +90,7 @@
$(document).ready(function(){
$("form").submit(function(event){
event.preventDefault();
var jsonObject = $(this).serializeArrayToJson();
var jsonObject = $(this).serializeObject();
console.log(jsonObject);
$("#result").html(JSON.stringify(jsonObject));
});
Expand Down
88 changes: 0 additions & 88 deletions js/jquery.serializeArrayToJson.js

This file was deleted.

88 changes: 88 additions & 0 deletions js/jquery.serializeObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*!
* jQuery serializeObject plugin
* Original author: Neil K. Villareal
*
* The MIT License (MIT)
*
* Copyright (c) 2015 Neil K. Villareal
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software
* is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*/
;
(function($, window, document, undefined) {

var pluginName = "serializeObject";
var defaults = {
requiredFormat: {}
};

function Plugin(element, options) {
this.element = element;

this.options = $.extend({}, defaults, options);

this._defaults = defaults;
this._name = pluginName;
}

Plugin.prototype.toJsonObject = function() {
var _this = this;
var keyValues = _this.replaceEmptyBracketWithNumericBracket($(_this.element).serializeArray());

var jsonObject = _this.options.requiredFormat;

$.each(keyValues, function(key, obj) {
var keys = obj.name.replace(/]/g, '').split("[");
var vObject = _this.stringArrayKeyToVariable(keys, obj.value);

jsonObject = $.extend(true, {}, jsonObject, vObject);
});

return jsonObject;
}

Plugin.prototype.replaceEmptyBracketWithNumericBracket = function(keyValues) {
var lastIndexes = {};
var newKeyValues = keyValues;

$.each(newKeyValues, function(key, obj) {

var keyName = obj.name;
var hasSquareBrackets = keyName.indexOf("[]");

if (hasSquareBrackets > -1) {
lastIndexes[keyName] = (typeof lastIndexes[keyName] == "undefined") ? 0 : ++lastIndexes[keyName];
newKeyValues[key].name = obj.name.replace("[]", "[" + (lastIndexes[keyName]) + "]");
}
});

return newKeyValues;
}

Plugin.prototype.stringArrayKeyToVariable = function(stringVars, value) {
var _this = this;
var stringVars = stringVars.reverse();

var variable;
if (stringVars.length > 0) {
var stringVar = stringVars.pop().trim();
variable = (isNaN(stringVar) && stringVar != "") ? {} : [];
variable[stringVar] = (stringVars.length > 0) ? _this.stringArrayKeyToVariable(stringVars.reverse(), value) : value;
}

return variable;
}

$.fn[pluginName] = function(options) {
return new Plugin(this, options).toJsonObject();
}

})(jQuery, window, document);

0 comments on commit d6e3c73

Please sign in to comment.