Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

json-uri

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-uri

Small and browser-compatible url-friendly JSON translator

  • 0.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

json-uri

json-uri is a simple uri-compatible json translator with notable advantages:

  • Browser-compatible with no dependencies and size of less than 1kB
  • 25% ~ 40% size deduction when uri-encoded, comparing to uri-encoded JSON string

json-uri searches for unused characters in provided string, and then replaces most commonly seen characters ({ } . : etc.) and sequences ("," ":" etc.) in JSON strings with them. This provides significant improvement on encoded string length with reasonable cost.

json-uri has some difference from similar packages like json-url and juri:

  • It doesn't concern about readability. JSON strings are rarely readable without multi-line formatting, so as a single-line string, trying to keep encoded string readable is commonly useless.
  • It doesn't apply compressing algorithm or other complex methods to further reduce encoded length, for following reasons:
    • most common usage of uri-compatible json translating is to pass JSON objects through web urls, and
    • url parameters longer than 2kB or 4kB are usually considered unsafe due to browser and server limitations, so
    • it is necessary to reduce a 1000-byte encoded string to 700 byte, but not from 700 byte to 680 byte with a lot more code.
  • It doesn't accept custom dictionaries, because it introduces extra coupling into your code. Just use shorter parameter names if you need.

Usage

Encode:

var ju = require('json-uri');
var jsonStr = JSON.stringify(obj);
var encodedStr = ju.encode(jsonStr);
window.location.href = 'http://example.com/?' + encodeURIComponent(encodedStr);

Decode:

var param = window.location.search.slice(1);
var encodedStr = decodeURIComponent(param);
var decodedStr = ju.decode(encodedStr);
var obj = JSON.parse(decodedStr);

API

var ju = require('json-uri');

  • ju.encode(str)

    Encode JSON string to json-uri string.

    • str string JSON string to be encoded
  • ju.decode(str)

    Decode json-uri string to JSON string.

    • str string json-uri string to be decoded
    • throws SyntaxError when the given string is of wrong format

TODO

  • Add more test cases.

Keywords

FAQs

Package last updated on 30 Oct 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc