Skip to content

Commit

Permalink
Update version 0.3.0
Browse files Browse the repository at this point in the history
Update examples
Update README.md
  • Loading branch information
tcorral committed Oct 27, 2013
1 parent c8bcb95 commit 530365d
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 18 deletions.
50 changes: 43 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,58 @@
JSONC
=====
# Update to version 0.1.0
# Update to version 0.3.0

[![Build Status](https://travis-ci.org/tcorral/JSONC.png)](https://travis-ci.org/tcorral/JSONC)

[Changelog](https://raw.github.com/tcorral/JSONC/master/changelog.txt)

## Background

JSON compressor-decompressor is a library to compress big JSON data.
One of the problems you can have developing RIA applications using Javascript is the amount of data being transported to
and from the server.
When data comes from server, this data could be GZipped, but this is not possible when the big amount of data comes from
the browser to the server.

This library should be used only when the improvement of compression is important is awful if you try to compress small json objects.
JSONC has two differents approaches to reduce the size of the amount of data to be transported:

The rate compression could variate from 7.5% to 32.81% depending of the type and values of data.
* *JSONC.compress* - Compress JSON objects using a map to reduce the size of the keys in JSON objects.
* Be careful with this method because it's really impressive if you use it with a JSON with a big amount of data, but it
could be awful if you use it to compress JSON objects with small amount of data because it could increase the final size.
* The rate compression could variate from 7.5% to 32.81% depending of the type and values of data.
* *JSONC.getLZWStringFromJSON* - Compress JSON objects using LZW compression algorithm, to make the job JSONC uses the
lz-string library from @pieroxy - https://github.com/pieroxy/lz-string/
* You can use getLZWStringFromJSON to compress any JSON objects even if these objects are not been compressed using JSONC
See Usage for more details.

Use lz-string library to compress more https://github.com/pieroxy/lz-string/
##Usage

###Compress a JSON object:

Ex_1.
var compressedJSON = JSONC.compress( json ); // Returns a JSON object but compressed.

###Decompress a JSON object:

var json = JSONC.decompress( compressedJSON ); // Returns the original JSON object.

###Compress a normal JSON object as a LZW string:

var lzwString = JSONC.getLZWStringFromJSON( json ); // Returns the LZW representation as string of the JSON object.

###Compress a JSON object as a LZW string after compress it using JSONC:

var lzwString = JSONC.getLZWStringFromJSON( json, true ); // Returns the LZW representation as string of the JSON object.

###Decompress a normal JSON object from a LZW string:

var json = JSONC.getJSONFromLZWString( lzwString ); // Returns the original JSON object.

###Decompress a JSON compressed object using JSONC from a LZW string:

var json = JSONC.getJSONFromLZWString( lzwString, true ); // Returns the original JSON object.

## Examples of compression

###Example data.js.

Original - 17331 bytes
Compressed using JSONC - 16025 bytes
Expand All @@ -30,7 +66,7 @@ Ex_1.

Compression rate from original to compressed using JSONC and lz-string - 78.71%

Ex_2.
###Example data2.js.

Original - 19031 bytes
Compressed using JSONC - 12787 bytes
Expand Down
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "JSONC",
"version": "0.2.0",
"name": "jsoncomp",
"version": "0.3.0",
"repository": {
"type": "git",
"url": "git:https://github.com/tcorral/JSONC.git"
Expand Down
3 changes: 3 additions & 0 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,12 @@ <h2>Decompress command</h2>
<script type="text/javascript" src="data/data2.js"></script>
<script type="text/javascript" src="../vendor/lz-string-1.0.2.js"></script>
<script type="text/javascript">
var json_cc = JSONC.getLZWStringFromJSON( obj, true );
var unjson_cc = JSONC.getJSONFromLZWString( json_cc );
var json_c = JSONC.compress( obj );
var json_unc = JSONC.decompress( json_c );
var json_c2 = JSONC.compress( obj2 );
var json_c3 = JSONC.compress( [{test:1,test2:2}, {test:3,test2:4}]);
var json_unc2 = JSONC.decompress( json_c2 );
var json_cComp = LZString.compress(JSON.stringify(json_c));
var json_uncComp = LZString.compress(JSON.stringify(json_unc));
Expand Down
1 change: 1 addition & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = function(config) {

// list of files / patterns to load in the browser
files: [
'vendor/lz-string-1.0.2.js',
'src/*.js',
'test/*.js'
],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsoncomp",
"version": "0.2.0",
"version": "0.3.0",
"description": "Compress your JSON to send and get a lot of data to/from server",
"homepage": "http:https://tcorral.github.com/JSONC/",
"author": "Tomas Corral",
Expand Down
26 changes: 24 additions & 2 deletions src/JSONC.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/*global LZString*/
(function ()
{

Expand Down Expand Up @@ -219,7 +220,6 @@
aKey = aKeys[nIndex];
str = str.replace( new RegExp( escapeRegExp( '"' + aKey[1] + '"' ), 'g' ), '"' + aKey[0] + '"' );
}

obj = JSON.parse( str );
obj._ = oKeys;
return obj;
Expand Down Expand Up @@ -289,6 +289,17 @@
}
return obj;
};
/**
* Use LZString to get the compressed string.
* @param json
* @param bCompress
* @returns {String}
*/
JSONC.getLZWStringFromJSON = function( json, bCompress )
{
var str = JSON.stringify( (bCompress ? JSONC.compress( json ): json) );
return LZString.compress( str );
};
/**
* Decompress a compressed JSON
* @param json
Expand All @@ -308,7 +319,18 @@
}
return str ? JSON.parse(str): jsonCopy ;
};

/**
* Returns the JSON object from the LZW string
* @param lzw
* @param bDecompress
* @returns {Object}
*/
JSONC.getJSONFromLZWString = function ( lzw, bDecompress )
{
var str = LZString.decompress( lzw ),
json = JSON.parse( str );
return bDecompress ? JSONC.decompress( json ) : json;
};
/*
* Expose Hydra to be used in node.js, as AMD module or as global
*/
Expand Down
92 changes: 92 additions & 0 deletions test/JSONC.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,96 @@ describe('JSONC.decompress', function (){

expect(retrieved).toEqual(expected);
});
describe('JSONC.getLZWStringFromJSON', function(){
it('should test that returns the expected string', function(){
var retrieved,
obj = {
'data': [
{
'test': 1,
'test2': 2,
'test3': 3
},
{
'test': 4,
'test2': 5,
'test3': 6
}
]
},
expected = "㞂…ࡠ⸒׀浑䂦ٲ밈쀚ኣ™ʠᦞਅ申耖㌞⸁壘쪸͡ꀗ䚐";

retrieved = JSONC.getLZWStringFromJSON( obj );

expect(retrieved).toEqual(expected);
});
it('should test that returns the expected string', function(){
var retrieved,
obj = {
'data': [
{
'test': 1,
'test2': 2,
'test3': 3
},
{
'test': 4,
'test2': 5,
'test3': 6
}
]
},
expected = "㞂•⁜ඪࠥȰڄȒ肙偡⠙聟既耖吴聖岠ඐ腵借䩓℀ᜁ䰆盢ኔḃ멦ሏ᜾菱䢅쀉聃溪䈔␀";

retrieved = JSONC.getLZWStringFromJSON( obj, true );

expect(retrieved).toEqual(expected);
});
});
describe('JSONC.getJSONFromLZWString', function(){
it('should test that returns the expected object', function(){
var retrieved,
obj = {
'data': [
{
'test': 1,
'test2': 2,
'test3': 3
},
{
'test': 4,
'test2': 5,
'test3': 6
}
]
},
lzw = "㞂…ࡠ⸒׀浑䂦ٲ밈쀚ኣ™ʠᦞਅ申耖㌞⸁壘쪸͡ꀗ䚐";

retrieved = JSONC.getJSONFromLZWString( lzw );

expect(retrieved).toEqual(obj);
});
it('should test that returns the expected object', function(){
var retrieved,
obj = {
'data': [
{
'test': 1,
'test2': 2,
'test3': 3
},
{
'test': 4,
'test2': 5,
'test3': 6
}
]
},
lzw = "㞂•⁜ඪࠥȰڄȒ肙偡⠙聟既耖吴聖岠ඐ腵借䩓℀ᜁ䰆盢ኔḃ멦ሏ᜾菱䢅쀉聃溪䈔␀";

retrieved = JSONC.getJSONFromLZWString( lzw, true );

expect(retrieved).toEqual(obj);
});
});
});
28 changes: 25 additions & 3 deletions versions/jsonc.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ var LZString = {
}
return result;
}
};;(function ()
};;/*global LZString*/
(function ()
{

var root,
Expand Down Expand Up @@ -422,7 +423,6 @@ var LZString = {
aKey = aKeys[nIndex];
str = str.replace( new RegExp( escapeRegExp( '"' + aKey[1] + '"' ), 'g' ), '"' + aKey[0] + '"' );
}

obj = JSON.parse( str );
obj._ = oKeys;
return obj;
Expand Down Expand Up @@ -492,6 +492,17 @@ var LZString = {
}
return obj;
};
/**
* Use LZString to get the compressed string.
* @param json
* @param bCompress
* @returns {String}
*/
JSONC.getLZWStringFromJSON = function( json, bCompress )
{
var str = JSON.stringify( (bCompress ? JSONC.compress( json ): json) );
return LZString.compress( str );
};
/**
* Decompress a compressed JSON
* @param json
Expand All @@ -511,7 +522,18 @@ var LZString = {
}
return str ? JSON.parse(str): jsonCopy ;
};

/**
* Returns the JSON object from the LZW string
* @param lzw
* @param bDecompress
* @returns {Object}
*/
JSONC.getJSONFromLZWString = function ( lzw, bDecompress )
{
var str = LZString.decompress( lzw ),
json = JSON.parse( str );
return bDecompress ? JSONC.decompress( json ) : json;
};
/*
* Expose Hydra to be used in node.js, as AMD module or as global
*/
Expand Down
4 changes: 2 additions & 2 deletions versions/jsonc.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified versions/jsonc.min.js.gz
Binary file not shown.
Loading

0 comments on commit 530365d

Please sign in to comment.