@jswork/next-json2base64

Json and base64 encode/decode.

Usage no npm install needed!

<script type="module">
  import jsworkNextJson2base64 from 'https://cdn.skypack.dev/@jswork/next-json2base64';
</script>

README

next-json2base64

Json and base64 encode/decode.

version license size download

installation

npm install -S @jswork/next-json2base64

apis

api params description
decode (string) Transform base64 string to js object.
decodeURI (string) Transform base64 string to js object when is in url case.
encode (object) Transform object to base64 string.
encodeURI (object) Transform object to base64 string when is in url case.

usage

import NxJson2base64 from '@jswork/next-json2base64';

// code goes here:
NxJson2base64.decode('eyJuYW1lIjoiYWZlaSIsImFnZSI6MTA4fQ==');
// { name: 'afei', age: 108 }

url query

  1. 特别注意: 在 url 中使用的时候,一定要提前 encodeURIComponent(DATA_ENCODE_JSON)
  2. BUG(浏览器默认行为): O+8iSIsI== -> O%208iSIsI%3D%3D
  3. 实际我们的数据为: O+8iSIsI== -> O%2B8iSIsI%3D%3D
  • https://url?token=xxx&data=DATA_ENCODE_JSON

DATA_ENCODE_JSON 传放参数会用 base64加码

data = encodeURIComponent(
  base64.decode(
    JSON.stringify({ q1: xx, q2: yy, q3: zz }
  )
);

接收到的 data,用base64解码

// !!! 这里特别提醒:如果在一些库中,取 queryString 的时候,有可能已经自带了 `decodeURIComponent`,所以这一步并不是必须的
JSON.parse(
  base64.decode(decodeURIComponent(data))
);

注意 window.atob/btoa 对中文处理有可能有兼容问题,所以这里建议使用 js-base64 这个库

resources

license

Code released under the MIT license.