Skip to content

Instantly share code, notes, and snippets.

@AlbertoMonteiro
Created September 26, 2012 21:00
Show Gist options
  • Save AlbertoMonteiro/3790557 to your computer and use it in GitHub Desktop.
Save AlbertoMonteiro/3790557 to your computer and use it in GitHub Desktop.
Converter to urlformencoded to JSON
module("dado um deserializador de url encoded posso criar objetos");
//"Nome=Alberto&Idade=22&Endereco.Logradouro=Rua%20Paulino%20Nogueira&Endereco.Numero=283&Telefones[0]=30211083&Telefones[1]=96234779&Linguagens[0].Nome=C#&Linguagens[0].Tipo=FortementeTipada&Linguagens[1].Nome=Javascript&Linguagens[1].Tipo=DinamicamenteTipada";
test("com so uma propriedade a partir de uma url encoded", function () {
var url = "Nome=Alberto&Idade=22";
var obj = form2json(url);
equal(obj.Nome, "Alberto");
equal(obj.Idade, "22");
});
test("com propriedades que sejam um array", function () {
var url = "Nome=Alberto&Idade=22&Telefones[0]=30211083&Telefones[1]=96234779";
var obj = form2json(url);
equal(obj.Nome, "Alberto");
equal(obj.Idade, "22");
equal(obj.Telefones[0], "30211083");
equal(obj.Telefones[1], "96234779");
});
test("com um objeto dentro dele", function () {
var url = "Nome=Alberto&Idade=22&Endereco.Logradouro=Rua%20Paulino%20Nogueira&Endereco.Numero=283&Telefones[0]=30211083&Telefones[1]=96234779";
var obj = form2json(url);
equal(obj.Nome, "Alberto");
equal(obj.Idade, "22");
equal(obj.Telefones[0], "30211083");
equal(obj.Telefones[1], "96234779");
equal(obj.Endereco.Logradouro, "Rua Paulino Nogueira");
equal(obj.Endereco.Numero, "283");
});
test("com um array de objetos", function () {
var url = "Nome=Alberto&Idade=22&Endereco.Logradouro=Rua%20Paulino%20Nogueira&Endereco.Numero=283&Telefones[0]=30211083&Telefones[1]=96234779&Linguagens[0].Nome=C#&Linguagens[0].Tipo=FortementeTipada&Linguagens[1].Nome=Javascript&Linguagens[1].Tipo=DinamicamenteTipada";
var obj = form2json(url);
equal(obj.Nome, "Alberto");
equal(obj.Idade, "22");
equal(obj.Telefones[0], "30211083");
equal(obj.Telefones[1], "96234779");
equal(obj.Endereco.Logradouro, "Rua Paulino Nogueira");
equal(obj.Endereco.Numero, "283");
equal(obj.Linguagens[0].Nome, "C#");
equal(obj.Linguagens[0].Tipo, "FortementeTipada");
equal(obj.Linguagens[1].Nome, "Javascript");
equal(obj.Linguagens[1].Tipo, "DinamicamenteTipada");
});
function form2json(str) {
var split = decodeURI(str).split("&");
var obj = {};
for (var i = 0; i < split.length; i++) {
var propName = split[i].split("=")[0].toString();
var propValue = split[i].split("=")[1];
var isSimpleArray = /[\[\]]/g.test(propName);
var isObject = /\.+/g.test(propName);
var isObjectArray = /\](?=\.).+$/g.test(propName);
if (isObjectArray) {
var actualPropName = propName.match(/[A-Za-z]+(?=\[)/g);
var valueIndex = parseInt(propName.match(/\d+(?=])/g));
var objectHierarchy = propName.match(/(?=\.).+$/g)[0].split(".").filter(function (item) { return item; })
if (!obj[actualPropName])
obj[actualPropName] = [];
if (!obj[actualPropName][valueIndex])
obj[actualPropName][valueIndex] = {};
var currentObject = obj[actualPropName][valueIndex];
//var j = currentObject ? 1 : 0;
for (var j = 0; j < objectHierarchy.length - 1; j++) {
if (!currentObject)
currentObject = obj[actualPropName][valueIndex][objectHierarchy[j]] = {};
else if (!currentObject[objectHierarchy[j]])
currentObject = currentObject[objectHierarchy[j]] = {};
else
currentObject = currentObject[objectHierarchy[j]];
}
currentObject[objectHierarchy[j]] = propValue;
} else if (isObject) {
var objectHierarchy = propName.split(".");
var currentObject = obj[objectHierarchy[0]];
var j = currentObject ? 1 : 0;
for (; j < objectHierarchy.length - 1; j++) {
if (!currentObject)
currentObject = obj[objectHierarchy[j]] = {};
else if (!currentObject[objectHierarchy[j]])
currentObject = currentObject[objectHierarchy[j]] = {};
else
currentObject = currentObject[objectHierarchy[j]];
}
currentObject[objectHierarchy[j]] = propValue;
} else if (isSimpleArray) {
var actualPropName = propName.match(/[A-Za-z]+(?=\[)/g);
var valueIndex = parseInt(propName.match(/\d+(?=])/g));
if (!obj[actualPropName])
obj[actualPropName] = [];
obj[actualPropName][valueIndex] = propValue;
} else {
obj[propName] = propValue;
}
}
return obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment