JSONCrush

repository·master·Indexed 23 days ago

https://github.com/killedbyapixel/jsoncrush

A lightweight system to compress JSON strings into compact, URL-friendly formats. Optimized for data under 10KB, it uses substring elimination and character swapping to reduce size compared to standard URI encoding or Base64. Provides `crush()` for compression and `uncrush()` for decompression.

Tokens
499
Snippets
1
Records
5
Agent score
32%

What's inside jsoncrush

  1. How the JSCrush algorithm works

    master

    The JSCrush algorithm is optimized for URI-encoded JSON strings. It achieves compression through several mechanisms:

    • Substring Elimination: Similar to the zip algorithm, it eliminates repeated substrings.
    • Character Swapping: It processes strings to swap common JSON characters with characters that do not require escaping in a URL.
    • Delimiter Usage: It uses the \u0001 (start of heading) character as a delimiter, which is removed during processing.

    Performance Considerations:

    • The algorithm is highly efficient for most use cases.
    • It may become slow when processing very long strings (exceeding 10,000 characters).
  2. Compress and decompress JSON strings with JSONCrush

    master

    JSONCrush provides two primary methods for handling JSON compression: crush() to compress a JSON string and uncrush() to restore it.

    Important Note on URLs: If you intend to use the crushed string in a URL, you must wrap the result in encodeURIComponent() to ensure special characters are properly escaped for the browser.

  3. Compress JSON with crush()

    master

    The crush method compresses a JSON string into a shorter, URL-friendly string by replacing repeated substrings with single characters and swapping common JSON characters for less frequent ones. This is ideal for reducing the size of data passed in URLs.

    Parameters:

    • string (string): The JSON string to compress.
    • maxSubstringLength (number, optional): The maximum length of substrings to consider for replacement. Defaults to 50.

    Returns:

    • string: A compressed, URL-friendly string. Note that the output string is appended with an underscore _ to help with link recognition.