If you are looking for a JavaScript code for the compress/decompress algorithm then you have come to the right place.In this post, I will explain you the following points:-
- Compress JSON at client side and decompress using C#
- Compress data in PHP and uncompress in javascript
- Compress XML, String, Variables in Client Sided.
- Compress your JSON data up to 80%
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Get JSON Data From Controller</title>
</head>
<body>
<div>
<input type="button" id="compresss" value="compresss" onclick="GetcompressData();" />
<input type="button" id="uncompress" value="uncompress" onclick="Getuncompressed();" />
</div>
</body>
</html>
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script type="text/javascript">
var LZW = {
compress: function (uncompressed) {
"use strict";
var i,
dictionary = {},
c,
wc,
w = "",
result = [],
dictSize = 256;
for (i = 0; i < 256; i += 1) {
dictionary[String.fromCharCode(i)] = i;
}
for (i = 0; i < uncompressed.length; i += 1) {
c = uncompressed.charAt(i);
wc = w + c;
if (dictionary.hasOwnProperty(wc)) {
w = wc;
} else {
result.push(dictionary[w]);
dictionary[wc] = dictSize++;
w = String(c);
}
}
if (w !== "") {
result.push(dictionary[w]);
}
return result;
},
decompress: function (compressed) {
"use strict";
var i,
dictionary = [],
w,
result,
k,
entry = "",
dictSize = 256;
for (i = 0; i < 256; i += 1) {
dictionary[i] = String.fromCharCode(i);
}
w = String.fromCharCode(compressed[0]);
result = w;
for (i = 1; i < compressed.length; i += 1) {
k = compressed[i];
if (dictionary[k]) {
entry = dictionary[k];
} else {
if (k === dictSize) {
entry = w + w.charAt(0);
} else {
return null;
}
}
result += entry;
dictionary[dictSize++] = w + entry.charAt(0);
w = entry;
}
return result;
}
}
var employees = [
{ "firstName": "John", "lastName": "Doe" },
{ "firstName": "Anna", "lastName": "Smith" },
{ "firstName": "Peter", "lastName": "Jones" }
];
function Getuncompressed() {
var str = JSON.stringify(employees)
var compressdata = LZW.compress(str)
var uncompressdata = LZW.decompress(compressdata)
alert(uncompressdata)
}
function GetcompressData() {
var str = JSON.stringify(employees)
var compressdata = LZW.compress(str)
alert(compressdata)
}
</script>
The post Client-side Data Compression and Decompression with JavaScript appeared first on Software Development | Programming Tutorials.
Read More Articles
- Write a value which contain comma to a CSV file in c#?
- Reading CSV File with cells containing commas c#
- Split CSV with columns may contain ‘,’ Comma C#
- [Simple Way]-Cascading DropDownList in Asp.Net Mvc Using Jquery Ajax
- [Simple Way]-How to get data from database using JQuery Ajax in asp net MVC
- [Simple Way]-ASP.NET Core Upload Multiple File Web API model
- [Simple Way]- Image Upload in .NET Core Web API
- [Easy Way]-Receive File and other form data together in ASP.NET Core Web API
- Replace image in word document using C#
- How to add new rows to an existing word document table in C#
- Best way for WinForms client to communicate with remote SQL Server and share data files
- Set Data With Document.InvokeScript And JavaScript Code
- Server side validation is done with data annotations instead of doing client side validation
- Client side check on video duration limit with javascript or jquery
- Get client time with out javascript and posts back
- How to Check if data exists with in the database and display error
- Mocking a class with both pure methods and ones with side effects
- SQLite: The right way to design a database and to access its data with an AUTOINCREMENT ID?
- Custom client side validation attribute with parameter in ASP.NET Core using IClientModelValidator
- C# - Connecting to database and inserting data with SQL Server stored procedure
- Convert object with list back to a class and find specific data from the object
- Issue with daylight saving between c# and javascript
- MVC: Keeping sensitive data on client side
- Storing data on client side - How to protect against manipulation?
- Data column(s) for axis #0 cannot be of type string in Google Charts with ASP.NET and SQL Server?
- Using SignalR to communicate between Server(asp mvc framework 4.5) and client (console app with framework 3.5))
- Import data to a temporary database and rename it when finished with MVC
- Keypress modifiers in a DataGridView with and without data
- Use server side and client side validation simultaneously on a single form in C#
- C# Console Application with a form on the side displaying live data
- Primary key constraint duplicate key exceptions with time series data and DATETIME2
- I want to return HttpStatusCode along with Json Object from MVC Controller to javascript client
- Accomplish gallery-type display with a Windows Forms data control and DataTable?
- Open Outlook 2013 New Appointment window at Client side with Microsoft.Office.Interop.Outlook C#
- Efficient way to parse large text file and work with data in it
- Client side keypress handling event, set focus function, and __doPostBack ASP.NET
- encrypt data in Javascript with "RSACryptoServiceProvider" Public Key -> Exceed Maximum Exception
- Bootstrap navpanel and date picker with javascript call to C# controller
- selectively hide client side validation error messages coming from Data Annotations attributes
- WPF TextBox Not Updating with Data Binding, iNotifyPropertyChanged, and PropertyChanged Trigger
- Printing an array in a GUI label in visual studio with C#
- Best way to pass List in query string HttpClient.GetAsync()
- Microsoft.Playwright.PlaywrightException:Connection closed (connection disposed) - getting this error using PoM Playwright c#, Visual studio 2022
- Unit Testing Monorail's RedirectToReferrer()
- LinkedResource in a css
- How to use common master page for C# and VB.NET WebForms sites
- Why doesn't GraphicsHeight exist in my Game class?
- How to print n numbers including (n-1) special Characters in asp.net?
- Error: A valid UTF32 value is between 0x000000 and 0x10ffff when getting result
- 2sxc method App.Data.Create returns System.NullReferenceException
- FluorineFX rtmp connection timeouts
- Database stored sentences for checking class status
- WPF:C#: The name "Interaction or eventtrigger or contolstroyboardaction" does not exist in the namespace " "
- Does RenderTexture works with Vuforia, Google Cardboard and Unity5?
- Hangfire dashboard authorization in Azure WorkerRole OR Self Hosted application
- ASMX webservice giving error - The data at the root level is invalid
- How do I carry over data between scenes in Unity?
- C# Interface with Enum Type Parameter
- Parser.Default.ParseArguments always returns false
- C#, WPF, MVVM, Creating a viewmodel for message dialogs/confirmation dialogs?