8

An unexpected decode results

 3 years ago
source link: https://github.com/KilledByAPixel/JSONCrush/issues/11
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

allex commented 3 days ago

edited
$ node -p '
var t=require("./JSONCrush.js"); 
var source=JSON.stringify({"c":"!()"})
var encode=t.JSONCrush(source);
var decode = decodeURIComponent(t.JSONUncrush(encode))
console.log({ source, encode, decode, result: source === decode })
'

-->

{
  source: '{"c":"!()"}',
  encode: "('c!''%3A%7B%7D')_",
  decode: '{"c":"":{}"}',
  result: false
}

allex

changed the title some unexpected result issue.

An unexpected decode results

3 days ago

Author

allex commented 3 days ago

edited

seems my problem, should decodeURIComponent the decoded string before uncrushing.

like this, ok.

$ node -p '
var t=require("./JSONCrush.js"); 
var source=JSON.stringify({"c":"!()"})
var encode=t.JSONCrush(source);
var decode = t.JSONUncrush(decodeURIComponent(encode))
console.log({ source, encode, decode, result: source === decode })
'

-->

{
  source: '{"c":"!()"}',
  encode: "('c!''%3A%7B%7D')_",
  decode: '{"c":"!()"}',
  result: true
}

btw, For the principle of I/O consistency, why not add decodeURIComponent in JSONUncrush?

please just close it.

Glad you figured it out...

btw, For the principle of I/O consistency, why not add decodeURIComponent in JSONUncrush?

It is a good question!

I wish it could work that way. The reason it can't is because searchParams.get() automatically calls decodeURIComponent on the string, and that is the most common use method. So if Uncrush also did it, then it would try to decode it a second time which could cause problems.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK