Skip to content

Made JSON.stringify comptible with javascript JSON#266

Merged
willemneal merged 6 commits into
near:mainfrom
gagdiez:main
Aug 6, 2021
Merged

Made JSON.stringify comptible with javascript JSON#266
willemneal merged 6 commits into
near:mainfrom
gagdiez:main

Conversation

@gagdiez
Copy link
Copy Markdown
Contributor

@gagdiez gagdiez commented Jul 25, 2021

The current JSON.stringify method escaped characters in a way that
was NOT compatible with the current javascript JSON implementation.
For example, the string single-character "\n" was being translated
into:

"\"\
\""

Instead of being stringifyed as: "\"\n\"". This is because the
character \n was being added, instead of adding an "n" preceeded
by an "\".

This commit fixes such problem by using a direct mapping from
escapable characters into their escaped stringified version.

gagdiez added 2 commits July 25, 2021 23:28
The current JSON.stringify method escaped characters in a way that
was NOT compatible with the current javascript JSON implementation.
For example, the string single-character "\n" was being translated
into:

"\"\
\""

Instead of being stringifyed as: "\"\n\"". This is because the
character \n was being added, instead of adding an "n" preceeded
by an "\\".

This commit fixes such problem by using a direct mapping from
escapable characters into their escaped stringified version.
@willemneal willemneal linked an issue Jul 26, 2021 that may be closed by this pull request
Copy link
Copy Markdown
Contributor

@willemneal willemneal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Just my two comments!

Comment thread assembly/JSON.ts Outdated
Comment thread assembly/JSON.ts
Comment thread assembly/JSON.ts Outdated
Co-authored-by: Max Graey <maxgraey@gmail.com>
Copy link
Copy Markdown
Contributor

@willemneal willemneal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks!

Comment thread assembly/JSON.ts Outdated
Comment thread assembly/JSON.ts Outdated
Comment thread assembly/JSON.ts Outdated
}
escaped.push(charCode);
const char = this._str.at(i);
escaped[i] = this.escapeChar(char);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this shouldn't be an instance method since we don't access this inside of it and we have to pass the reference every time.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved it outside the class

Comment thread assembly/JSON.ts
@gagdiez
Copy link
Copy Markdown
Contributor Author

gagdiez commented Aug 6, 2021

@willemneal do you think this is ready now?

@willemneal willemneal merged commit 1edab43 into near:main Aug 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incompatible stringify of escaped characters

3 participants