A jQuery plug-in for transforming data between different formats:
- JavaScript objects
- html forms
- json strings
- query strings
$.io.form($('form')).object();
{
user: {
name: 'Sam',
interests: ['1', '2']
}
}
$.io.form($('form')).query();
'user%5Bname%5D=Sam&user%5Binterests%5D%5B%5D=1&user%5Binterests%5D%5B%5D=2'
// this is equivalent to:
$('form').serialize();
$.io.form($('form')).json();
'{"user":{"id":"1","name":"Sam","interests":["1","2","3"]}}'
var source = 'user%5Bname%5D=Sam&user%5Binterests%5D%5B%5D=1&user%5Binterests%5D%5B%5D=2';
$.io.query(source).object();
{
user: {
name: 'Sam',
interests: ['1', '2']
}
}
var source = 'user%5Bname%5D=Sam&user%5Binterests%5D%5B%5D=1&user%5Binterests%5D%5B%5D=2';
$.io.query(source).json();
'{"user":{"id":"1","name":"Sam","interests":["1","2","3"]}}'
var source = '{"user":{"id":"1","name":"Sam","interests":["1","2","3"]}}';
$.io.json(source).object();
{
user: {
name: 'Sam',
interests: ['1', '2']
}
}
// this is equivalent to:
JSON.parse(source)
var source = '{"user":{"id":"1","name":"Sam","interests":["1","2","3"]}}';
$.io.json(source).query();
'user%5Bname%5D=Sam&user%5Binterests%5D%5B%5D=1&user%5Binterests%5D%5B%5D=2'
var source = {
user: {
name: 'Sam',
interests: ['1', '2']
}
}
$.io.object(source).query();
'user%5Bname%5D=Sam&user%5Binterests%5D%5B%5D=1&user%5Binterests%5D%5B%5D=2'
var source = {
user: {
name: 'Sam',
interests: ['1', '2']
}
}
$.io.object(source).json();
'{"user":{"id":"1","name":"Sam","interests":["1","2","3"]}}'
// this is equivalent to:
JSON.stringify(source);
Given a query like &a=101, io at the moment doesn't try to figure out if 101 is a number or a string, so it will just return a string if converted to an object. e.g. {a: '101'}
npm init
grunt
- Bitovi (CanJS) - for some of the code I 'borrowed' for converting forms into objects.
Copyright (c) 2013 Sebastian Porto Licensed under the MIT license.