Skip to content

Commit

Permalink
Prepare #736 - change JSON serialization logic of string
Browse files Browse the repository at this point in the history
  • Loading branch information
samchon committed Aug 3, 2023
1 parent c29d31f commit cfd5d51
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 33 deletions.
18 changes: 9 additions & 9 deletions benchmark/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import { BenchmarkStream } from "./internal/BenhmarkStream";
async function main(): Promise<void> {
const stream: BenchmarkStream = await BenchmarkReporter.initialize();
for (const category of [
"is",
"assert",
"validate",
"assert-error",
"validate-error",
"optimizer",
// "is",
// "assert",
// "validate",
// "assert-error",
// "validate-error",
// "optimizer",
"stringify",
"server-assert",
"server-stringify",
"server-performance",
// "server-assert",
// "server-stringify",
// "server-performance",
])
await BenchmarkReporter.write(stream)(
await BenchmarkServer.measure(category),
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typia",
"version": "4.1.14",
"version": "4.1.15-dev.20230803",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -107,7 +107,7 @@
"d3": "^5.16.0",
"eslint-plugin-deprecation": "^1.4.1",
"express": "^4.18.2",
"fast-json-stringify": "^5.4.0",
"fast-json-stringify": "^5.8.0",
"fastify": "^4.9.2",
"io-ts": "^2.2.19",
"jsdom": "^21.1.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/typescript-json/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typescript-json",
"version": "4.1.14",
"version": "4.1.15-dev.20230803",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -68,7 +68,7 @@
},
"homepage": "https://typia.io",
"dependencies": {
"typia": "4.1.14"
"typia": "4.1.15-dev.20230803"
},
"peerDependencies": {
"typescript": ">= 4.7.4"
Expand Down
46 changes: 26 additions & 20 deletions src/functional/$string.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
/**
* In the past, name of `typia` was `typescript-json`, and supported
* JSON serialization by wrapping `fast-json-stringify. `typescript-json` was
* a helper library of `fast-json-stringify`, which can skip manual JSON schema
* definition just by putting pure TypeScript type.
*
* This `$string` function is a part of `fast-json-stringify` at that time, and
* still being used in `typia` for the string serialization.
*
* @internal
* @reference https://github.com/fastify/fast-json-stringify/blob/master/lib/serializer.js
* @blog https://dev.to/samchon/good-bye-typescript-is-ancestor-of-typia-20000x-faster-validator-49fi
*/
export const $string = (str: string): string => {
if (str.length > 41) return JSON.stringify(str);

const length = str.length;
const len = str.length;
let result = "";
let last = 0;
let found = false;
let surrogateFound = false;
let last = -1;
let point = 255;

// eslint-disable-next-line
for (let i = 0; i < length && point >= 32; i++) {
for (let i = 0; i < len; ++i) {
point = str.charCodeAt(i);
if (0xd800 <= point && point <= 0xdfff) {
if (point < 32) {
return JSON.stringify(str);
}
if (point >= 0xd800 && point <= 0xdfff) {
// The current character is a surrogate.
surrogateFound = true;
break;
return JSON.stringify(str);
}
if (point === 34 || point === 92) {
if (
point === 0x22 || // '"'
point === 0x5c // '\'
) {
last === -1 && (last = 0);
result += str.slice(last, i) + "\\";
last = i;
found = true;
}
}

if (!found) {
result = str;
} else {
result += str.slice(last);
}
return point < 32 || surrogateFound === true
? JSON.stringify(str)
: `"${result}"`;
return (
(last === -1 && '"' + str + '"') || '"' + result + str.slice(last) + '"'
);
};

0 comments on commit cfd5d51

Please sign in to comment.